diff options
| author | Aldrik Ramaekers <aldrik@amftech.nl> | 2022-11-07 17:49:36 +0100 |
|---|---|---|
| committer | Aldrik Ramaekers <aldrik@amftech.nl> | 2022-11-07 17:49:36 +0100 |
| commit | 40c13a4bb122ee36631a60f738f85fcdbfd233f3 (patch) | |
| tree | ed45e71e2cc1b7cc7be95976275b9afd9d25f1d7 /lib/services | |
| parent | e938da92fa92f6c7036934dd5e673c5b9df68f4f (diff) | |
work
Diffstat (limited to 'lib/services')
| -rw-r--r-- | lib/services/authentication_service.dart | 6 | ||||
| -rw-r--r-- | lib/services/istoregear_api_service.dart | 9 | ||||
| -rw-r--r-- | lib/services/storegear_api_service.dart | 57 |
3 files changed, 70 insertions, 2 deletions
diff --git a/lib/services/authentication_service.dart b/lib/services/authentication_service.dart index ede8808..ef9113e 100644 --- a/lib/services/authentication_service.dart +++ b/lib/services/authentication_service.dart @@ -1,8 +1,10 @@ +import 'package:training_planner/services/istoregear_api_service.dart'; + class AuthenticationService { bool isAuthenticated = false; String apiKey = ''; - String storedPNumber = ''; - String storedDaycode = ''; + String storedPNumber = '639174'; + String storedDaycode = '424'; Future<bool> authenticate(String username, String password) async { isAuthenticated = true; diff --git a/lib/services/istoregear_api_service.dart b/lib/services/istoregear_api_service.dart new file mode 100644 index 0000000..adc07e0 --- /dev/null +++ b/lib/services/istoregear_api_service.dart @@ -0,0 +1,9 @@ +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'; + +abstract class IStoregearApiService { + Future<LoginResponse> login(LoginRequest req); + Future<RouteList> getRoutes(); +} 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'); + } + } +} |
