openclimatefix / pv-site-api

Site specific API for PV forecasting
6 stars 11 forks source link

Create HTTP Basic Auth Flow and Token Caching #74

Open ericcccsliu opened 1 year ago

ericcccsliu commented 1 year ago

Enode requires us to obtain an access token from their API in order to use the main endpoints in their API. The way they do this is through "Basic" HTTP Authentication scheme. What this involves is sending our Enode client credentials (client ID as username and client secret as password), over to Enode's access token fetch endpoint via that auth scheme. Fortunately, the HTTP Python library we'll be using (https://www.python-httpx.org/), handles Basic authentication for us (https://www.python-httpx.org/quickstart/#authentication).

Once we've got a token, it makes sense to continue to reuse it over the course of the hour it's useable, until it must be refreshed. To do this, we'll store the token in OCF's database under a new table, found here. This table has a helper function you can use to actually get the token without accessing SQL models.

See here for our current implementation in TypeScript.