-
-
Notifications
You must be signed in to change notification settings - Fork 10
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
Add Enode authentication class #81
Conversation
Codecov Report
@@ Coverage Diff @@
## h4i/enode #81 +/- ##
=============================================
- Coverage 91.63% 84.82% -6.82%
=============================================
Files 8 9 +1
Lines 311 336 +25
=============================================
Hits 285 285
- Misses 26 51 +25
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
pv_site_api/enode_auth.py
Outdated
|
||
|
||
def get_enode_access_token() -> str: | ||
# Replace these with your actual credentials |
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.
Remove this comment perhaps—seems outdated
pv_site_api/enode_auth.py
Outdated
client_id = os.getenv("CLIENT_ID") | ||
client_secret = os.getenv("CLIENT_SECRET") | ||
|
||
url = "https://oauth.sandbox.enode.io/oauth2/token" |
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.
we might want to put this in an env variable?
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.
Nice work, a few comments.
pv_site_api/enode_auth.py
Outdated
|
||
if response.status_code == 401: | ||
# The access token is no longer valid, refresh it | ||
self.access_token = get_enode_access_token() |
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.
I'm not entirely sure if this will work in the way HTTPX wants it to... consider the example they have in the docs: https://www.python-httpx.org/advanced/#customizing-authentication:~:text=refresh_response%20%3D%20yield%20self.build_refresh_request()
The way they do this is have the function they use to fetch the new token return a Request object, which they then call yield
on. I think we'll want to do the same thing here. This also means that you'll need to grab the .json()
and the right key from the response in this code rather than your other function.
Also, I think we may want to add tests for this logic... Here's a library that allows you to mock HTTPX requests: https://github.com/Colin-b/pytest_httpx Feel free to look into this, I will also. We can work on adding tests to this before merging. |
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.
Very nice, last few things.
pv_site_api/enode_auth.py
Outdated
def __init__(self, access_token): | ||
self.access_token = access_token | ||
|
||
def auth_flow(self, request): |
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.
Add a type hint
def auth_flow(self, request): | |
def auth_flow(self, request: httpx.Request): |
Pull Request
Description
Adds httpx auth class for enode api requests in
enode_auth.py
get_enode_access_token()
retrieves enode api tokenEnodeAuth
class hasauth_flow
that refreshes api token using the functionPlease delete the italicised instruction text!
Please include a summary of the change and which issue is fixed.
Please also include relevant motivation and context. List any dependencies that are required for this change.
Fixes #73, #74
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce.
Please also list any relevant details for your test configuration
If your changes affect data processing, have you plotted any changes? i.e. have you done a quick sanity check?
Checklist: