mirror of https://github.com/flutter/samples.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.6 KiB
50 lines
1.6 KiB
import 'dart:convert';
|
|
import 'dart:io';
|
|
import 'package:compass_model/model.dart';
|
|
|
|
import '../../utils/result.dart';
|
|
|
|
// TODO: Basic auth request
|
|
// TODO: Configurable baseurl/host/port
|
|
class ApiClient {
|
|
Future<Result<List<Continent>>> getContinents() async {
|
|
final client = HttpClient();
|
|
try {
|
|
final request = await client.get('localhost', 8080, '/continent');
|
|
final response = await request.close();
|
|
if (response.statusCode == 200) {
|
|
final stringData = await response.transform(utf8.decoder).join();
|
|
final json = jsonDecode(stringData) as List<dynamic>;
|
|
return Result.ok(
|
|
json.map((element) => Continent.fromJson(element)).toList());
|
|
} else {
|
|
return Result.error(const HttpException("Invalid response"));
|
|
}
|
|
} on Exception catch (error) {
|
|
return Result.error(error);
|
|
} finally {
|
|
client.close();
|
|
}
|
|
}
|
|
|
|
Future<Result<List<Destination>>> getDestinations() async {
|
|
final client = HttpClient();
|
|
try {
|
|
final request = await client.get('localhost', 8080, '/destination');
|
|
final response = await request.close();
|
|
if (response.statusCode == 200) {
|
|
final stringData = await response.transform(utf8.decoder).join();
|
|
final json = jsonDecode(stringData) as List<dynamic>;
|
|
return Result.ok(
|
|
json.map((element) => Destination.fromJson(element)).toList());
|
|
} else {
|
|
return Result.error(const HttpException("Invalid response"));
|
|
}
|
|
} on Exception catch (error) {
|
|
return Result.error(error);
|
|
} finally {
|
|
client.close();
|
|
}
|
|
}
|
|
}
|