Skip to content

Commit 9f58df5

Browse files
cmaloneyclaude
andcommitted
Refactor repeated context setup into util.get_app_context()
Three route handlers each built the same client_id/app_url context dict. Extracting to a helper also adds APP_PROTOCOL support so the GitHub OAuth redirect URL works over http:// for local development. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 73c398a commit 9f58df5

5 files changed

Lines changed: 32 additions & 11 deletions

File tree

blurb_it/__main__.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ async def handle_get(request: Request) -> Response:
3131
# data = request.query_string
3232
# data2 = await request.rel_url.query['']
3333
request_session = await get_session(request)
34-
context = {}
35-
context["client_id"] = os.environ.get("GH_CLIENT_ID")
36-
context["app_url"] = os.environ.get("APP_URL")
34+
context = util.get_app_context()
3735
if request_session.get("username") and request_session.get("token"):
3836
context["username"] = request_session["username"]
3937
location = request.app.router["add_blurb"].url_for()
@@ -48,9 +46,7 @@ async def handle_get(request: Request) -> Response:
4846
@routes.get("/howto", name="howto")
4947
async def handle_howto_get(request: Request) -> Response:
5048
"""Render a page explaining how to use blurb_it"""
51-
context = {}
52-
context["client_id"] = os.environ.get("GH_CLIENT_ID")
53-
context["app_url"] = os.environ.get("APP_URL")
49+
context = util.get_app_context()
5450
response = aiohttp_jinja2.render_template("howto.html", request, context=context)
5551
return response
5652

@@ -60,9 +56,7 @@ async def handle_install(request: Request) -> Response:
6056
"""Render a page, ask user to install blurb_it"""
6157
# data = request.query_string
6258
# data2 = await request.rel_url.query['']
63-
context = {}
64-
context["client_id"] = os.environ.get("GH_CLIENT_ID")
65-
context["app_url"] = os.environ.get("APP_URL")
59+
context = util.get_app_context()
6660
if await util.has_session(request):
6761
context.update(await util.get_session_context(request, context))
6862

blurb_it/util.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import base64
22
import hashlib
3+
import os
34
import time
45
import secrets
56

@@ -22,6 +23,14 @@ async def nonceify(body):
2223
return base64.urlsafe_b64encode(digest)[0:6].decode("ascii")
2324

2425

26+
def get_app_context() -> dict:
27+
return {
28+
"client_id": os.environ.get("GH_CLIENT_ID"),
29+
"app_url": os.environ.get("APP_URL"),
30+
"app_protocol": os.environ.get("APP_PROTOCOL", "https"),
31+
}
32+
33+
2534
async def get_session_context(request, context=None):
2635
context = context or {}
2736
if await has_session(request):

templates/base.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<h5 class="my-0 mr-md-auto font-weight-normal"><a href="/">📜🤖 Blurb it!</a></h5>
2121
<h5 class="my-0 mr-md-auto font-weight-normal"><a href="/howto">How do I use Blurb It?</a></h5>
2222
{% if not username %}
23-
<a class="btn btn-outline-primary btn-social btn-github" href="https://github.com/login/oauth/authorize?client_id={{ client_id }}&scope=repo&redirect_uri=https://{{ app_url }}/add_blurb">
23+
<a class="btn btn-outline-primary btn-social btn-github" href="https://github.com/login/oauth/authorize?client_id={{ client_id }}&scope=repo&redirect_uri={{ app_protocol }}://{{ app_url }}/add_blurb">
2424
<span class="fa fa-github"></span>
2525
Sign in with GitHub
2626
</a>

templates/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ <h1 class="display-4">📜🤖 Blurb it!</h1>
88
</div>
99

1010
<p class="lead text-center">
11-
<a class="btn btn-social btn-github" href="https://github.com/login/oauth/authorize?client_id={{ client_id }}&scope=repo&redirect_uri=https://{{ app_url }}/add_blurb">
11+
<a class="btn btn-social btn-github" href="https://github.com/login/oauth/authorize?client_id={{ client_id }}&scope=repo&redirect_uri={{ app_protocol }}://{{ app_url }}/add_blurb">
1212
<span class="fa fa-github"></span>
1313
Sign in with GitHub
1414
</a>

tests/test_util.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@ async def getiter(self, url, jwt=None, accept=None):
2323
yield item
2424

2525

26+
def test_get_app_context(monkeypatch):
27+
monkeypatch.setenv("GH_CLIENT_ID", "test_client_id")
28+
monkeypatch.setenv("APP_URL", "localhost:8080")
29+
monkeypatch.setenv("APP_PROTOCOL", "http")
30+
ctx = util.get_app_context()
31+
assert ctx == {
32+
"client_id": "test_client_id",
33+
"app_url": "localhost:8080",
34+
"app_protocol": "http",
35+
}
36+
37+
38+
def test_get_app_context_defaults_to_https(monkeypatch):
39+
monkeypatch.delenv("APP_PROTOCOL", raising=False)
40+
ctx = util.get_app_context()
41+
assert ctx["app_protocol"] == "https"
42+
43+
2644
async def test_nonceify():
2745
body = (
2846
"Lorem ipsum dolor amet flannel squid normcore tbh raclette enim"

0 commit comments

Comments
 (0)