From 40c13a4bb122ee36631a60f738f85fcdbfd233f3 Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Mon, 7 Nov 2022 17:49:36 +0100 Subject: work --- lib/services/storegear_api_service.dart | 57 +++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 lib/services/storegear_api_service.dart (limited to 'lib/services/storegear_api_service.dart') 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 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 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'); + } + } +} -- cgit v1.2.3-70-g09d2