utils.dart
2.32 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
library google_maps_webservice.utils;
import 'dart:async';
import 'package:http/http.dart';
import 'package:meta/meta.dart';
final kGMapsUrl = Uri.parse('https://maps.googleapis.com/maps/api');
abstract class GoogleWebService {
@protected
final Client _httpClient;
@protected
late final Uri _url;
@protected
final String? _apiKey;
@protected
final Map<String, String>? _apiHeaders;
Uri get url => _url;
Client get httpClient => _httpClient;
String? get apiKey => _apiKey;
Map<String, String>? get apiHeaders => _apiHeaders;
GoogleWebService({
String? apiKey,
required String apiPath,
String? baseUrl,
Client? httpClient,
Map<String, String>? apiHeaders,
}) : _httpClient = httpClient ?? Client(),
_apiKey = apiKey,
_apiHeaders = apiHeaders {
var uri = kGMapsUrl;
if (baseUrl != null) {
uri = Uri.parse(baseUrl);
}
_url = uri.replace(path: '${uri.path}$apiPath');
}
@protected
String buildQuery(Map<String, dynamic> params) {
final query = [];
params.forEach((key, val) {
if (val != null) {
if (val is Iterable) {
query.add("$key=${val.map((v) => v.toString()).join("|")}");
} else {
query.add('$key=${val.toString()}');
}
}
});
return query.join('&');
}
void dispose() => httpClient.close();
@protected
Future<Response> doGet(String url, {Map<String, String>? headers}) {
return httpClient.get(Uri.parse(url), headers: headers);
}
@protected
Future<Response> doPost(
String url,
String body, {
Map<String, String>? headers,
}) {
final postHeaders = {
'Content-type': 'application/json',
};
if (headers != null) postHeaders.addAll(headers);
return httpClient.post(Uri.parse(url), body: body, headers: postHeaders);
}
}
DateTime dayTimeToDateTime(int day, String time) {
if (time.length < 4) {
throw ArgumentError(
"'time' is not a valid string. It must be four integers.");
}
day = day == 0 ? DateTime.sunday : day;
final now = DateTime.now();
final mondayOfThisWeek = now.day - now.weekday;
final computedWeekday = mondayOfThisWeek + day;
final hour = int.parse(time.substring(0, 2));
final minute = int.parse(time.substring(2));
return DateTime.utc(now.year, now.month, computedWeekday, hour, minute);
}