bare implementation of comuncation with the api
This commit is contained in:
@@ -1,16 +1,19 @@
|
||||
import "dart:convert";
|
||||
import "dart:developer";
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:anyway/constants.dart';
|
||||
import "package:anyway/structs/landmark.dart";
|
||||
import "package:anyway/structs/trip.dart";
|
||||
import "package:anyway/structs/preferences.dart";
|
||||
|
||||
import "package:anyway/structs/linked_landmarks.dart";
|
||||
|
||||
Dio dio = Dio(
|
||||
BaseOptions(
|
||||
baseUrl: API_URL_BASE,
|
||||
// baseUrl: 'http://localhost:8000',
|
||||
connectTimeout: const Duration(seconds: 5),
|
||||
receiveTimeout: const Duration(seconds: 60),
|
||||
receiveTimeout: const Duration(seconds: 120),
|
||||
// api is notoriously slow
|
||||
// headers: {
|
||||
// HttpHeaders.userAgentHeader: 'dio',
|
||||
@@ -18,19 +21,25 @@ Dio dio = Dio(
|
||||
// },
|
||||
contentType: Headers.jsonContentType,
|
||||
responseType: ResponseType.json,
|
||||
|
||||
),
|
||||
);
|
||||
|
||||
Future<Trip> fetchTrip(
|
||||
Future<Trip>? fetchTrip(
|
||||
List<double> startPoint,
|
||||
UserPreferences preferences,
|
||||
Future<UserPreferences> preferences,
|
||||
) async {
|
||||
UserPreferences prefs = await preferences;
|
||||
Map<String, dynamic> data = {
|
||||
"preferences": prefs.toJson(),
|
||||
"start": startPoint
|
||||
};
|
||||
String dataString = jsonEncode(data);
|
||||
log(dataString);
|
||||
|
||||
final response = await dio.post(
|
||||
"/trip/new",
|
||||
data: {
|
||||
'preferences': preferences.toJson(),
|
||||
'start': startPoint
|
||||
}
|
||||
data: data
|
||||
);
|
||||
|
||||
// handle errors
|
||||
@@ -40,6 +49,37 @@ Future<Trip> fetchTrip(
|
||||
if (response.data["error"] != null) {
|
||||
throw Exception(response.data["error"]);
|
||||
}
|
||||
return Trip.fromJson(response.data);
|
||||
log(response.data.toString());
|
||||
Map<String, dynamic> json = response.data;
|
||||
|
||||
// only fetch the trip "meta" data for now
|
||||
Trip trip = Trip.fromJson(json);
|
||||
|
||||
String? nextUUID = json["first_landmark_uuid"];
|
||||
while (nextUUID != null) {
|
||||
var (landmark, newUUID) = await fetchLandmark(nextUUID);
|
||||
trip.landmarks.add(landmark);
|
||||
nextUUID = newUUID;
|
||||
}
|
||||
return trip;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Future<(Landmark, String?)> fetchLandmark(String uuid) async {
|
||||
final response = await dio.get(
|
||||
"/landmark/$uuid"
|
||||
);
|
||||
|
||||
// handle errors
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('Failed to load landmark');
|
||||
}
|
||||
if (response.data["error"] != null) {
|
||||
throw Exception(response.data["error"]);
|
||||
}
|
||||
log(response.data.toString());
|
||||
Map<String, dynamic> json = response.data;
|
||||
String? nextUUID = json["next_uuid"];
|
||||
return (Landmark.fromJson(json), nextUUID);
|
||||
}
|
||||
|
Reference in New Issue
Block a user