-
Notifications
You must be signed in to change notification settings - Fork 0
/
location_search_api.dart
57 lines (48 loc) · 1.75 KB
/
location_search_api.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import 'package:weather_app/networking/async_compute.dart';
import 'package:weather_app/networking/http.dart';
import 'package:weather_app/networking/http_client_provider.dart';
import 'package:weather_app/networking/json_decoder.dart';
final class ApiLocation {
final String name;
final String region;
final double latitude;
final double longitude;
const ApiLocation({
required this.name,
required this.region,
required this.latitude,
required this.longitude,
});
factory ApiLocation.fromJson(JsonDecoder json) => ApiLocation(
name: json.field('name'),
region: json.field('admin1'),
latitude: json.field('latitude'),
longitude: json.field('longitude'),
);
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is ApiLocation &&
runtimeType == other.runtimeType &&
name == other.name &&
region == other.region &&
latitude == other.latitude &&
longitude == other.longitude;
@override
int get hashCode => name.hashCode ^ region.hashCode ^ latitude.hashCode ^ longitude.hashCode;
}
const searchApiUrl = 'https://geocoding-api.open-meteo.com/v1/search';
HttpFuture<Iterable<ApiLocation>> searchLocation(
HttpClientProvider httpClientProvider,
AsyncCompute asyncCompute,
String name,
) async {
final nameParam = Uri.encodeComponent(name);
final url = Uri.parse('$searchApiUrl?name=$nameParam&count=10&language=en&format=json');
return httpClientProvider.withHttpClient((client) async {
final httpResult = await client.sendRequest(HttpMethod.get, url);
return httpResult
.expectStatusCode(200)
.tryParseJson(asyncCompute, (json) => json.objectArray('results', ApiLocation.fromJson));
});
}