Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Merge pull request #7 from Jeonghun-Ban/fix/Error-unable-to-load-api-key
Browse files Browse the repository at this point in the history
fix: Error unable to load api key
  • Loading branch information
junghoon-vans committed Dec 11, 2020
2 parents f77f23a + c6448ac commit 4a60b7d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
3 changes: 3 additions & 0 deletions assets/secrets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"theja_apiKey": "NQWN1Uvf9i6UD%2Bo%2FphXROudMPlIx%2BYEk0lR1lZiO0I4J9up%2F4bbxRM1UJvQabT4sLUYgmUvpiA%2FUg2eQACq6Bg%3D%3D"
}
25 changes: 18 additions & 7 deletions lib/auth/keys.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,33 @@ import 'dart:convert' show json;
import 'package:flutter/services.dart' show rootBundle;

class Secret {
final String apiKey;
Secret({this.apiKey = ""});
final String thejaApiKey;

Secret({this.thejaApiKey = ""});

factory Secret.fromJson(Map<String, dynamic> jsonMap) {
return new Secret(apiKey: jsonMap["api_key"]);
return new Secret(thejaApiKey: jsonMap["theja_apiKey"]);
}
}

class SecretLoader {
final String secretPath;

SecretLoader({this.secretPath});
Future<Secret> load() {
return rootBundle.loadStructuredData<Secret>(this.secretPath,

static Secret _secret;

const SecretLoader({
this.secretPath = 'assets/secrets.json',
});

Future<Secret> load() async {
if (_secret != null) {
return _secret;
}
_secret = await rootBundle.loadStructuredData<Secret>(secretPath,
(jsonStr) async {
final secret = Secret.fromJson(json.decode(jsonStr));
return secret;
});
return _secret;
}
}
25 changes: 18 additions & 7 deletions lib/utils/bus_info_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import 'package:theja/auth/keys.dart';
import 'package:theja/strings.dart';

class BusInfoParser {
BusInfoParser._();
final SecretLoader secretLoader;
BusInfoParser._({this.secretLoader = const SecretLoader()});
static final BusInfoParser bus = BusInfoParser._();

Future<List<Vehicle>> searchStation(String keyword) async {
Secret secret = await SecretLoader(secretPath: "secrets.json").load();
final apiKey = await _apiKey();
String xmlString = await fetchDocument(Strings.searchStationUri +
"?serviceKey=" +
secret.apiKey +
apiKey +
"&keyword=" +
keyword);
var raw = xml.parse(xmlString);
Expand All @@ -31,10 +32,10 @@ class BusInfoParser {
}

Future<List<Vehicle>> searchVehicle(String stationId) async {
Secret secret = await SecretLoader(secretPath: "secrets.json").load();
final apiKey = await _apiKey();
String xmlString = await fetchDocument(Strings.searchRouteUri +
"?serviceKey=" +
secret.apiKey +
apiKey +
"&stId=" +
stationId +
"&");
Expand All @@ -55,10 +56,10 @@ class BusInfoParser {
}

Future<Map<int, String>> getArr(String stationId, String routeId) async {
Secret secret = await SecretLoader(secretPath: "secrets.json").load();
final apiKey = await _apiKey();
String xmlString = await fetchDocument(Strings.searchRouteUri +
"?serviceKey=" +
secret.apiKey +
apiKey +
"&stId=" +
stationId +
"&");
Expand All @@ -80,4 +81,14 @@ class BusInfoParser {
http.Response response = await http.get(uri);
return response.body;
}

Future<String> _apiKey() async {
try {
final secret = await secretLoader.load();
return secret.thejaApiKey;
} catch (exception) {
print(exception);
return "";
}
}
}
5 changes: 4 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ dev_dependencies:
sdk: flutter

flutter:
uses-material-design: true
uses-material-design: true

assets:
- assets/

0 comments on commit 4a60b7d

Please sign in to comment.