-
Notifications
You must be signed in to change notification settings - Fork 157
feat: Add optional URL return to get_flights and fetch_flights_html functions #104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,4 +1,4 @@ | ||||||||||||||||||||||
| from typing import overload | ||||||||||||||||||||||
| from typing import Literal, overload | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| from primp import Client | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
@@ -10,7 +10,13 @@ | |||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| @overload | ||||||||||||||||||||||
| def get_flights(q: str, /, *, proxy: str | None = None) -> MetaList: | ||||||||||||||||||||||
| def get_flights( | ||||||||||||||||||||||
| q: str, | ||||||||||||||||||||||
| /, | ||||||||||||||||||||||
| *, | ||||||||||||||||||||||
| proxy: str | None = None, | ||||||||||||||||||||||
| return_url: Literal[False] = False, | ||||||||||||||||||||||
| ) -> MetaList: | ||||||||||||||||||||||
| """Get flights using a str query. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Examples: | ||||||||||||||||||||||
|
|
@@ -19,7 +25,28 @@ def get_flights(q: str, /, *, proxy: str | None = None) -> MetaList: | |||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| @overload | ||||||||||||||||||||||
| def get_flights(q: Query, /, *, proxy: str | None = None) -> MetaList: | ||||||||||||||||||||||
| def get_flights( | ||||||||||||||||||||||
| q: str, | ||||||||||||||||||||||
| /, | ||||||||||||||||||||||
| *, | ||||||||||||||||||||||
| proxy: str | None = None, | ||||||||||||||||||||||
| return_url: Literal[True], | ||||||||||||||||||||||
| ) -> tuple[MetaList, str]: | ||||||||||||||||||||||
| """Get flights using a str query, returning both flights and URL. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Examples: | ||||||||||||||||||||||
| - *Flights from TPE to MYJ on 2025-12-22 one way economy class* | ||||||||||||||||||||||
| """ | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| @overload | ||||||||||||||||||||||
| def get_flights( | ||||||||||||||||||||||
| q: Query, | ||||||||||||||||||||||
| /, | ||||||||||||||||||||||
| *, | ||||||||||||||||||||||
| proxy: str | None = None, | ||||||||||||||||||||||
| return_url: Literal[False] = False, | ||||||||||||||||||||||
| ) -> MetaList: | ||||||||||||||||||||||
| """Get flights using a structured query. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Example: | ||||||||||||||||||||||
|
|
@@ -44,21 +71,65 @@ def get_flights(q: Query, /, *, proxy: str | None = None) -> MetaList: | |||||||||||||||||||||
| """ | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| @overload | ||||||||||||||||||||||
| def get_flights( | ||||||||||||||||||||||
| q: Query, | ||||||||||||||||||||||
| /, | ||||||||||||||||||||||
| *, | ||||||||||||||||||||||
| proxy: str | None = None, | ||||||||||||||||||||||
| return_url: Literal[True], | ||||||||||||||||||||||
| ) -> tuple[MetaList, str]: | ||||||||||||||||||||||
| """Get flights using a structured query, returning both flights and URL. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Example: | ||||||||||||||||||||||
| ```python | ||||||||||||||||||||||
| get_flights( | ||||||||||||||||||||||
| query( | ||||||||||||||||||||||
| flights=[ | ||||||||||||||||||||||
| FlightQuery( | ||||||||||||||||||||||
| date="2025-12-22", | ||||||||||||||||||||||
| from_airport="TPE", | ||||||||||||||||||||||
| to_airport="MYJ", | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
| ], | ||||||||||||||||||||||
| seat="economy", | ||||||||||||||||||||||
| trip="one-way", | ||||||||||||||||||||||
| passengers=Passengers(adults=1), | ||||||||||||||||||||||
| language="en-US", | ||||||||||||||||||||||
| currency="", | ||||||||||||||||||||||
| ), | ||||||||||||||||||||||
| return_url=True | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
| ``` | ||||||||||||||||||||||
| """ | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def get_flights( | ||||||||||||||||||||||
| q: Query | str, | ||||||||||||||||||||||
| /, | ||||||||||||||||||||||
| *, | ||||||||||||||||||||||
| proxy: str | None = None, | ||||||||||||||||||||||
| integration: Integration | None = None, | ||||||||||||||||||||||
| ) -> MetaList: | ||||||||||||||||||||||
| return_url: bool = False, | ||||||||||||||||||||||
| ) -> MetaList | tuple[MetaList, str]: | ||||||||||||||||||||||
| """Get flights. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Args: | ||||||||||||||||||||||
| q: The query. | ||||||||||||||||||||||
| proxy (str, optional): Proxy. | ||||||||||||||||||||||
| return_url (bool, optional): If True, returns a tuple of (MetaList, url). | ||||||||||||||||||||||
| If False (default), returns only MetaList for backward compatibility. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Returns: | ||||||||||||||||||||||
| MetaList if return_url is False (default), or | ||||||||||||||||||||||
| tuple of (MetaList, url) if return_url is True. | ||||||||||||||||||||||
| """ | ||||||||||||||||||||||
| html = fetch_flights_html(q, proxy=proxy, integration=integration) | ||||||||||||||||||||||
| return parse(html) | ||||||||||||||||||||||
| html, url = fetch_flights_html(q, proxy=proxy, integration=integration) | ||||||||||||||||||||||
| flights = parse(html) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if return_url: | ||||||||||||||||||||||
| return flights, url | ||||||||||||||||||||||
| return flights | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def fetch_flights_html( | ||||||||||||||||||||||
|
|
@@ -67,12 +138,16 @@ def fetch_flights_html( | |||||||||||||||||||||
| *, | ||||||||||||||||||||||
| proxy: str | None = None, | ||||||||||||||||||||||
| integration: Integration | None = None, | ||||||||||||||||||||||
| ) -> str: | ||||||||||||||||||||||
| """Fetch flights and get the **HTML**. | ||||||||||||||||||||||
| ) -> tuple[str, str]: | ||||||||||||||||||||||
| """Fetch flights and get the **HTML** and **URL**. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Args: | ||||||||||||||||||||||
| q: The query. | ||||||||||||||||||||||
| proxy (str, optional): Proxy. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Returns: | ||||||||||||||||||||||
| A tuple of (html, url) where html is the response text | ||||||||||||||||||||||
| and url is the final URL of the request. | ||||||||||||||||||||||
| """ | ||||||||||||||||||||||
| if integration is None: | ||||||||||||||||||||||
| client = Client( | ||||||||||||||||||||||
|
|
@@ -90,7 +165,8 @@ def fetch_flights_html( | |||||||||||||||||||||
| params = {"q": q} | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| res = client.get(URL, params=params) | ||||||||||||||||||||||
| return res.text | ||||||||||||||||||||||
| return res.text, str(res.url) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| else: | ||||||||||||||||||||||
| return integration.fetch_html(q) | ||||||||||||||||||||||
| html = integration.fetch_html(q) | ||||||||||||||||||||||
| return html, URL | ||||||||||||||||||||||
|
Comment on lines
170
to
+172
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Integration branch returns incomplete URL without query parameters. The direct fetch path (line 168) returns This inconsistency undermines the debugging/sharing purpose of returning the URL. Users would expect the full request URL, not just the base endpoint. Suggested fix else:
html = integration.fetch_html(q)
- return html, URL
+ if isinstance(q, Query):
+ return html, q.url()
+ else:
+ return html, f"{URL}?q={q}"This mirrors how 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: AWeirdDev/flights
Length of output: 1515
🏁 Script executed:
Repository: AWeirdDev/flights
Length of output: 1478
Breaking change:
fetch_flights_htmlreturn type changed fromstrtotuple[str, str].The function is publicly exported and now returns a tuple of (html, url) instead of just the HTML string. External callers expecting a string will break:
The return type is documented in the function's docstring, but this breaking change lacks a release note or migration guide. Consider either:
return_urlparameter to maintain backward compatibility, or🤖 Prompt for AI Agents