Skip to content

Commit 74aa1b0

Browse files
committed
ch04: build user-agent header from metadata
1 parent 5182138 commit 74aa1b0

File tree

1 file changed

+10
-2
lines changed
  • chapter04-dependency-management/src/random_wikipedia_article

1 file changed

+10
-2
lines changed

chapter04-dependency-management/src/random_wikipedia_article/__init__.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1+
from importlib.metadata import metadata
2+
13
import httpx
24
from rich.console import Console
35

6+
47
API_URL = "https://en.wikipedia.org/api/rest_v1/page/random/summary"
5-
USER_AGENT = "random-wikipedia-article/0.1 (Contact: you@example.com)"
8+
USER_AGENT = "{Name}/{Version} (Contact: {Author-email})"
9+
10+
11+
def build_user_agent():
12+
fields = metadata("random-wikipedia-article")
13+
return USER_AGENT.format_map(fields)
614

715

816
def main():
9-
headers = {"User-Agent": USER_AGENT}
17+
headers = {"User-Agent": build_user_agent()}
1018

1119
with httpx.Client(headers=headers, http2=True) as client:
1220
response = client.get(API_URL, follow_redirects=True)

0 commit comments

Comments
 (0)