openclimatefix / openclimatefix/pv-site-api

Create HTTPX Custom Auth to handle token refresh

Open
#73 0 comments 0 reactions 2 assignees View on GitHub

@rohanvan123 is already working on this.

Since Apr 11, 2023.

Dominant language
Python
Stars
7
Forks
11
PR merge metrics
No merged PRs in 30d

Description

After creating a function to get the currently cached Enode access token and another to refresh it if necessary, we'll want to create a custom HTTPx Auth class to handle the logic for actually determining whether our current token needs to be refreshed. The way we do this is by, after someone makes a request to Enode's API, checking the response HTTP status code. If it's a 401, then we know our access token is no longer valid, and we must request another.

The way this is implemented in HTTPx is by creating a custom Auth class, which we'll pass into our HTTPx clients auth argument. See the example under this paragraph in the docs.

  • Create the custom Auth class to handle automatically determining token expiration and refreshing it, then re-requesting the resource

Here's how this code looks in TypeScript in our own mock API:

https://github.com/openclimatefix/pv-sites-mobile/blob/c5f47fde4c6c2cce6f145f58edbeddbca687be11/lib/enode.ts#L85-L108

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.