-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1219 from Foundation-Devices/ENV-1101-keys-endpoint
ENV-1101: fetch some API keys from Server endpoint
- Loading branch information
Showing
11 changed files
with
129 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// SPDX-FileCopyrightText: 2024 Foundation Devices Inc. | ||
// | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
import 'dart:async'; | ||
import 'package:envoy/util/console.dart'; | ||
import 'package:envoy/util/envoy_storage.dart'; | ||
import 'package:envoy/business/server.dart'; | ||
|
||
class KeysManager { | ||
ApiKeys? keys; | ||
|
||
static final KeysManager _instance = KeysManager._internal(); | ||
|
||
factory KeysManager() { | ||
return _instance; | ||
} | ||
|
||
static Future<KeysManager> init() async { | ||
var singleton = KeysManager._instance; | ||
await singleton._initAsync(); | ||
return singleton; | ||
} | ||
|
||
_initAsync() async { | ||
keys = await EnvoyStorage().getApiKeys(); | ||
} | ||
|
||
KeysManager._internal() { | ||
kPrint("Instance of KeysManager created!"); | ||
|
||
// Fetch from the Envoy Server | ||
_fetchKeysFromServer(); | ||
|
||
// Check once an hour | ||
Timer.periodic(const Duration(hours: 1), (_) { | ||
_fetchKeysFromServer(); | ||
}); | ||
} | ||
|
||
void _fetchKeysFromServer() { | ||
Server().fetchApiKeys().then((keys) => _store(keys)).catchError((e) { | ||
kPrint("Couldn't fetch API keys: $e"); | ||
}); | ||
} | ||
|
||
_store(ApiKeys newKeys) async { | ||
if (keys == null || | ||
keys!.mapsKey != newKeys.mapsKey || | ||
keys!.rampKey != newKeys.rampKey) { | ||
await EnvoyStorage().storeApiKeys(newKeys); | ||
keys = newKeys; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters