Skip to content

Commit d5ef487

Browse files
authored
fix(examples): clean up unnecessary code (#1321)
1 parent 9ad12c6 commit d5ef487

File tree

2 files changed

+7
-27
lines changed

2 files changed

+7
-27
lines changed

examples/custom_source_hn/main.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class HackerNewsSource(SourceSpec):
5454
"""Source spec for HackerNews API."""
5555

5656
tag: str | None = None
57-
max_results: int = 100
57+
max_results: int = 200
5858

5959

6060
@source_connector(
@@ -77,24 +77,16 @@ async def create(spec: HackerNewsSource) -> "HackerNewsConnector":
7777
"""Create a HackerNews connector from the spec."""
7878
return HackerNewsConnector(spec, aiohttp.ClientSession())
7979

80-
async def _ensure_session(self) -> aiohttp.ClientSession:
81-
"""Ensure we have an active HTTP session."""
82-
if self._session is None or self._session.closed:
83-
self._session = aiohttp.ClientSession()
84-
return self._session
85-
8680
async def list(
8781
self,
8882
) -> AsyncIterator[PartialSourceRow[_HackerNewsThreadKey, _HackerNewsThread]]:
8983
"""List HackerNews threads using the search API."""
90-
session = await self._ensure_session()
91-
9284
# Use HackerNews search API
9385
search_url = "https://hn.algolia.com/api/v1/search_by_date"
9486
params: dict[str, Any] = {"hitsPerPage": self._spec.max_results}
9587
if self._spec.tag:
9688
params["tags"] = self._spec.tag
97-
async with session.get(search_url, params=params) as response:
89+
async with self._session.get(search_url, params=params) as response:
9890
response.raise_for_status()
9991
data = await response.json()
10092
for hit in data.get("hits", []):
@@ -114,12 +106,10 @@ async def get_value(
114106
self, key: _HackerNewsThreadKey
115107
) -> PartialSourceRowData[_HackerNewsThread]:
116108
"""Get a specific HackerNews thread by ID using the items API."""
117-
session = await self._ensure_session()
118-
119109
# Use HackerNews items API to get full thread with comments
120110
item_url = f"https://hn.algolia.com/api/v1/items/{key.thread_id}"
121111

122-
async with session.get(item_url) as response:
112+
async with self._session.get(item_url) as response:
123113
response.raise_for_status()
124114
data = await response.json()
125115

@@ -183,7 +173,7 @@ def hackernews_flow(
183173

184174
# Add the custom source to the flow
185175
data_scope["threads"] = flow_builder.add_source(
186-
HackerNewsSource(tag="story", max_results=500),
176+
HackerNewsSource(tag="story", max_results=200),
187177
refresh_interval=timedelta(minutes=1),
188178
)
189179

examples/hn_trending_topics/main.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class HackerNewsSource(SourceSpec):
8787
"""Source spec for HackerNews API."""
8888

8989
tag: str | None = None
90-
max_results: int = 100
90+
max_results: int = 200
9191

9292

9393
@source_connector(
@@ -110,24 +110,16 @@ async def create(spec: HackerNewsSource) -> "HackerNewsConnector":
110110
"""Create a HackerNews connector from the spec."""
111111
return HackerNewsConnector(spec, aiohttp.ClientSession())
112112

113-
async def _ensure_session(self) -> aiohttp.ClientSession:
114-
"""Ensure we have an active HTTP session."""
115-
if self._session is None or self._session.closed:
116-
self._session = aiohttp.ClientSession()
117-
return self._session
118-
119113
async def list(
120114
self,
121115
) -> AsyncIterator[PartialSourceRow[_HackerNewsThreadKey, _HackerNewsThread]]:
122116
"""List HackerNews threads using the search API."""
123-
session = await self._ensure_session()
124-
125117
# Use HackerNews search API
126118
search_url = "https://hn.algolia.com/api/v1/search_by_date"
127119
params: dict[str, Any] = {"hitsPerPage": self._spec.max_results}
128120
if self._spec.tag:
129121
params["tags"] = self._spec.tag
130-
async with session.get(search_url, params=params) as response:
122+
async with self._session.get(search_url, params=params) as response:
131123
response.raise_for_status()
132124
data = await response.json()
133125
for hit in data.get("hits", []):
@@ -147,12 +139,10 @@ async def get_value(
147139
self, key: _HackerNewsThreadKey
148140
) -> PartialSourceRowData[_HackerNewsThread]:
149141
"""Get a specific HackerNews thread by ID using the items API."""
150-
session = await self._ensure_session()
151-
152142
# Use HackerNews items API to get full thread with comments
153143
item_url = f"https://hn.algolia.com/api/v1/items/{key.thread_id}"
154144

155-
async with session.get(item_url) as response:
145+
async with self._session.get(item_url) as response:
156146
response.raise_for_status()
157147
data = await response.json()
158148

0 commit comments

Comments
 (0)