Skip to content
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

refine pagination code, other minor refinements #140

Merged
merged 1 commit into from
Feb 5, 2025

Conversation

philvarner
Copy link
Contributor

@philvarner philvarner commented Feb 5, 2025

Related Issue(s):

Proposed Changes:

  • add body to create-order link in Opportunities Search result
  • refine pagination code
  • rename OpportunityRequest to OpportunityPayload - this makes it so it won't be confused with the starlette/fastapi Request class and the Payload part aligns it with other POST body model names

PR Checklist:

  • I have added my changes to the CHANGELOG or a CHANGELOG entry is not required.

…s Search result, rename OpportunityRequest to OpportunityPayload
@philvarner philvarner changed the title refine pagination code, add body to create-order link in Opportunitie… refine pagination code Feb 5, 2025
self.pagination_link(request, search.model_dump(mode="json"))
)
links.append(self.order_link(request, search))
links.append(self.pagination_link(request, search, pagination_token))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this pushes the logic for how to construct the link down into the pagination_link method

@@ -224,7 +221,7 @@ async def create_order(
request,
):
case Success(order):
self.root_router.add_order_links(order, request)
order.links.extend(self.root_router.order_links(order, request))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

improves the semantics of the function to make it more functional -- e.g., it doesn't mutate it's parameters, and instead just returns an object that the caller can then use for mutation

return Link(
href=str(request.url.remove_query_params(keys=["next", "limit"])),
href=str(request.url),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there won't be query params on this, so they don't need to be removed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah! Right you are

return Link(
href=str(request.url.include_query_params(next=pagination_token)),
href=str(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

include the limit explicitly, so it's not just relying on whatever the default is

next_token = next_token.split("next=")[1]
params = {"next": next_token, "limit": limit}
res = stapi_client.get(endpoint, params=params)
o = urlparse(url)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-wrote this to actually parse the url, since a simple split on next= doesn't work if there are any other parameters, e.g., next=foo&limit=1, since the next_token value gets set to foo&limit=1

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh shoot wish I had written the tests that would've caught that case in the first place

order["links"].append(json_link)
self_link = copy.deepcopy(order["links"][0])
order["links"].append(self_link)
monitor_link = copy.deepcopy(order["links"][0])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was missing from the /orders endpoint Orders and was added, so the test was updated

@philvarner philvarner changed the title refine pagination code refine pagination code, other minor refinements Feb 5, 2025
@philvarner philvarner marked this pull request as ready for review February 5, 2025 18:37
Copy link
Contributor

@theodorehreuter theodorehreuter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good stuff, nice improvements and tidying.

def search_body(self) -> dict[str, Any]:
return self.model_dump(mode="json", include={"datetime", "geometry", "filter"})

def body(self) -> dict[str, Any]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like the addition of these small helper functions here to help clean up the endpoint business logic.

return Link(
href=str(request.url.remove_query_params(keys=["next", "limit"])),
href=str(request.url),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah! Right you are

next_token = next_token.split("next=")[1]
params = {"next": next_token, "limit": limit}
res = stapi_client.get(endpoint, params=params)
o = urlparse(url)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh shoot wish I had written the tests that would've caught that case in the first place

res = stapi_client.get(endpoint, params=params)
o = urlparse(url)
base_url = f"{o.scheme}://{o.netloc}{o.path}"
parsed_qs = parse_qs(o.query)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't know about this parse_qs function, that would've been helpful early only womp womp. The more you know.

@philvarner philvarner merged commit f3adb87 into main Feb 5, 2025
3 checks passed
@philvarner philvarner deleted the pv/refine-pagination branch February 5, 2025 21:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants