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

Build out http helper class #36

Merged
merged 3 commits into from
May 13, 2024
Merged

Conversation

mbiannaccone
Copy link
Collaborator

@mbiannaccone mbiannaccone commented May 9, 2024

https://canvasmedical.atlassian.net/browse/KOALA-1335

An abstraction on the requests library with the following methods:

  • get
  • post
  • put
  • patch

creates a requests session and then passes calls through session methods. also a statsd wrapper to measure the timing of each call.

still todo:

  • tests!!
  • documentation

return _decorator(fn) if fn else _decorator

@measure_time
def get(self, url: str, headers: dict = {}) -> requests.Response:
Copy link
Contributor

Choose a reason for hiding this comment

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

A nice usability enhancement for all of these would be to allow passing in an optional dictionary of query parameters that we could encode for them, rather than making the user do it each time. This would map to the params argument in requests.

return self.session.get(url, headers=headers)

@measure_time
def post(self, url: str, json: dict | None = None, headers: dict = {}) -> requests.Response:
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to allow post, put, and patch to send non-JSON request bodies?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

i think we want to keep it simple for now, and if there is a need to add in more other uses like those (files, etc), we'll add them. it'll be simple enough since we're just copying the same api as the requests library and passing in.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK. The specific non-JSON use case I was thinking about was for request bodies that are application/x-www-form-urlencoded -- like requesting an OAuth token.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

that's a good one! i'll add it

@mbiannaccone mbiannaccone requested a review from csande May 10, 2024 17:40
@mbiannaccone mbiannaccone merged commit b4bc617 into main May 13, 2024
2 checks passed
@mbiannaccone mbiannaccone deleted the michela/koala-1335-requests-util branch May 13, 2024 17:58
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.

3 participants