mirror of
https://github.com/urbit/shrub.git
synced 2024-12-21 01:41:37 +03:00
interface: storing the gcp token
This commit is contained in:
parent
0a23c0f4a4
commit
3c2ce636e7
@ -4,21 +4,38 @@ import {GcpToken} from '../types/gcp-state';
|
||||
|
||||
|
||||
export default class GcpApi extends BaseApi<StoreState> {
|
||||
#running = false;
|
||||
|
||||
startRefreshLoop() {
|
||||
// TODO: don't allow more than one
|
||||
this.refreshLoop();
|
||||
if (this.#running) {
|
||||
console.error('GcpApi startRefreshLoop: already running');
|
||||
} else {
|
||||
this.#running = true;
|
||||
this.refreshLoop();
|
||||
}
|
||||
}
|
||||
|
||||
private refreshLoop() {
|
||||
console.log("refreshing GCP token");
|
||||
console.log("GcpApi refreshLoop");
|
||||
this.refreshToken().then(({accessKey, expiresIn}: GcpToken) => {
|
||||
console.log("new token: ", accessKey);
|
||||
console.log("GcpApi new token");
|
||||
// XX maybe bad?
|
||||
this.store.state.gcp.accessKey = accessKey;
|
||||
const timeout = this.refreshInterval(expiresIn);
|
||||
console.log("GcpApi refreshing in", timeout);
|
||||
setTimeout(() => {
|
||||
this.refreshLoop();
|
||||
}, expiresIn);
|
||||
}, timeout);
|
||||
});
|
||||
}
|
||||
|
||||
private refreshInterval(expiresIn: number) {
|
||||
// Give ourselves 30 seconds for processing delays, but never refresh
|
||||
// sooner than 10 minute from now. (The expiry window should be about an
|
||||
// hour.)
|
||||
return Math.max(600_000, expiresIn - 30_000);
|
||||
}
|
||||
|
||||
private async refreshToken() {
|
||||
const token = await this.spider('noun', 'gcp-token', 'get-gcp-token', {});
|
||||
return token['gcp-token'];
|
||||
|
@ -71,7 +71,9 @@ export default class GlobalStore extends BaseStore<StoreState> {
|
||||
},
|
||||
weather: {},
|
||||
userLocation: null,
|
||||
gcp: {},
|
||||
gcp: {
|
||||
accessKey: null
|
||||
},
|
||||
s3: {
|
||||
configuration: {
|
||||
buckets: new Set(),
|
||||
|
@ -4,5 +4,5 @@ export interface GcpToken {
|
||||
};
|
||||
|
||||
export interface GcpState {
|
||||
token: GcpToken;
|
||||
accessKey: string;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user