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

Fix: allow unexpected fields in responses #69

Merged
merged 4 commits into from
Sep 17, 2024

Conversation

angela-tran
Copy link
Member

Closes #66

This PR makes it so GET requests will allow unexpected fields in the response JSON. The main code changes was for Client._get to expect that it can call from_kwargs to instantiate the response object, and to add from_kwargs as a class method on all response dataclasses.

Looking at our Client code and usages of _get, _post, and _put, we see that it is really only _get that tries to hydrate specific response dataclasses. Endpoints that use _put and _post all pass in dict as the response class to hydrate.

So it is a sufficient fix to only change Client._get.

@angela-tran angela-tran self-assigned this Sep 16, 2024
@angela-tran angela-tran requested a review from a team as a code owner September 16, 2024 21:59
Copy link

github-actions bot commented Sep 16, 2024

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  littlepay/api
  __init__.py
  client.py
  funding_sources.py
  groups.py
  products.py
Project Total  

This report was generated by python-coverage-comment-action

@angela-tran
Copy link
Member Author

Just noticed a missing return

this helper method allows for the presence of unexpected fields in the
response JSON.

this establishes the convention that response classes (at least the ones
that are used for GET responses) must define this class method.
@angela-tran angela-tran force-pushed the fix/unexpected-fields-in-responses branch from 397e0e4 to 859b316 Compare September 16, 2024 22:11
@angela-tran angela-tran marked this pull request as ready for review September 16, 2024 22:12
@angela-tran
Copy link
Member Author

Ready for review!

littlepay/api/__init__.py Outdated Show resolved Hide resolved
thekaveman
thekaveman previously approved these changes Sep 17, 2024
Copy link
Member

@thekaveman thekaveman left a comment

Choose a reason for hiding this comment

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

Very nice 👍

@angela-tran
Copy link
Member Author

I ran Benefits with this branch of littlepay and was able to get the log message for fields not defined in the dataclass:

[17/Sep/2024 10:35:08] INFO littlepay.api:39 Ran into an unexpected arg: created_date = 2023-10-05T00:15:19Z

Then I added created_date to the dataclass in b756c42... but I guess we're not actually using created_date anywhere, so maybe this change isn't actually needed. What are your thoughts @thekaveman?

@thekaveman
Copy link
Member

@angela-tran

Then I added created_date to the dataclass in b756c42... but I guess we're not actually using created_date anywhere, so maybe this change isn't actually needed.

I think this is fine. It's a valid field now available in the API response, so we can parse it and make it available in case others are using our library.

@angela-tran angela-tran merged commit 16e7150 into main Sep 17, 2024
3 checks passed
@angela-tran angela-tran deleted the fix/unexpected-fields-in-responses branch September 17, 2024 20:32
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.

Avoid errors when responses contain additional fields
3 participants