summaryrefslogtreecommitdiff
path: root/lib/services/storegear_api_service.dart
diff options
context:
space:
mode:
authorAldrik Ramaekers <aldrik@amftech.nl>2022-11-07 17:49:36 +0100
committerAldrik Ramaekers <aldrik@amftech.nl>2022-11-07 17:49:36 +0100
commit40c13a4bb122ee36631a60f738f85fcdbfd233f3 (patch)
treeed45e71e2cc1b7cc7be95976275b9afd9d25f1d7 /lib/services/storegear_api_service.dart
parente938da92fa92f6c7036934dd5e673c5b9df68f4f (diff)
work
Diffstat (limited to 'lib/services/storegear_api_service.dart')
-rw-r--r--lib/services/storegear_api_service.dart57
1 files changed, 57 insertions, 0 deletions
diff --git a/lib/services/storegear_api_service.dart b/lib/services/storegear_api_service.dart
new file mode 100644
index 0000000..b346292
--- /dev/null
+++ b/lib/services/storegear_api_service.dart
@@ -0,0 +1,57 @@
+import 'dart:convert';
+import 'package:flutter/cupertino.dart';
+import 'package:http/http.dart' as http;
+import 'package:training_planner/models/login_request.dart';
+import 'package:training_planner/models/login_response.dart';
+import 'package:training_planner/models/route_list.dart';
+import 'package:training_planner/route.dart';
+import 'package:training_planner/services/istoregear_api_service.dart';
+
+class StoregearApiService extends IStoregearApiService {
+ String apiKey = '';
+
+ @override
+ Future<LoginResponse> login(LoginRequest req) async {
+ final response = await http.post(
+ Uri.parse('http://dhlapis.com/delivery/v1/users/login?env_type=PROD'),
+ body: jsonEncode(req));
+ debugPrint(jsonEncode(req));
+ debugPrint(response.body);
+ if (response.statusCode == 200) {
+ // If the server did return a 200 OK response,
+ // then parse the JSON.
+ LoginResponse res = LoginResponse.fromJson(jsonDecode(response.body));
+ apiKey = res.apiKey!;
+ return res;
+ } else {
+ // If the server did not return a 200 OK response,
+ // then throw an exception.
+ throw Exception('Failed login');
+ }
+ }
+
+ @override
+ Future<RouteList> getRoutes() async {
+ debugPrint('WE GOT HERE!!! ' + apiKey);
+ final response = await http.get(
+ Uri.parse('http://dhlapis.com/delivery/v1/routes'),
+ headers: {'X-API-KEY': apiKey});
+
+ debugPrint(response.body);
+ if (response.statusCode == 200) {
+ // If the server did return a 200 OK response,
+ // then parse the JSON.
+
+ var content = jsonDecode(response.body);
+ if (content["message"] != null) {
+ content = jsonEncode(RouteList());
+ }
+
+ return RouteList.fromJson(content);
+ } else {
+ // If the server did not return a 200 OK response,
+ // then throw an exception.
+ throw Exception('Failed to load routes');
+ }
+ }
+}