-
Notifications
You must be signed in to change notification settings - Fork 3
/
example.dart
204 lines (182 loc) · 6.26 KB
/
example.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import 'dart:convert' as convert;
import 'package:http/http.dart';
import 'package:spacex_api/models/capsule.dart';
import 'package:spacex_api/models/company/company.dart';
import 'package:spacex_api/models/crew.dart';
import 'package:spacex_api/models/dragon/dragon.dart';
import 'package:spacex_api/models/history/history.dart';
import 'package:spacex_api/models/landpad.dart';
import 'package:spacex_api/models/launch/launch.dart';
import 'package:spacex_api/models/launchpad.dart';
import 'package:spacex_api/models/pagenated_response.dart';
import 'package:spacex_api/models/payload.dart';
import 'package:spacex_api/models/query/options.dart';
import 'package:spacex_api/models/roadster.dart';
import 'package:spacex_api/models/rocket/rocket.dart';
import 'package:spacex_api/models/ship.dart';
import 'package:spacex_api/models/starlink/starlink.dart';
import 'package:spacex_api/spacex_api.dart';
import 'package:spacex_api/utils.dart';
void main() async {
final api = SpaceXApi();
// Fetch by ID
_fetchSingleCapsule(api);
// Get all objects
_fetchAllStarlink(api);
// Query objects
_queryStarlinks(api);
}
Future<void> _fetchSingleCapsule(SpaceXApi api) async {
final response = await api.getCapsuleById(id: "5e9e2c5cf359183bb73b266e");
if (response.statusCode == 200) {
final json = Utils.parseResponseAsJson(response);
final capsule = Capsule.fromJson(json);
print("Fetch Single Capsule serial -> ${capsule.serial}");
}
}
Future<void> _fetchAllStarlink(SpaceXApi api) async {
final response = await api.getAllStarlinks();
if (response.statusCode == 200) {
List<Starlink> data = Utils.convertResponseToList<Starlink>(
response, (e) => Starlink.fromJson(e));
print("Fetch Starlinks ${data.length}");
}
}
Future<void> _queryStarlinks(SpaceXApi api) async {
final query = Options();
query.limit = 25;
query.page = 1;
query.pagination = true;
query.select = [
"version",
"height_km",
"longitude",
];
// var queryJson = convert.jsonEncode(query.toJson());
final response = await api.queryStarlinks(query: query.toJson());
if (response.statusCode == 200) {
final jsonResp = Utils.parseResponseAsJson(response);
PagenatedResponse pagenatedResponse = PagenatedResponse.fromJson(jsonResp);
List<Starlink> data = pagenatedResponse.docs
.map((e) => Starlink.fromJson(e))
.cast<Starlink>()
.toList();
print("Qeury Starlinks ${data.length}");
}
// Alternative solution
// final json = _parseResponse(response);
// if (json != null) {
// PagenatedResponse resp = PagenatedResponse.fromJson(json);
// List<Starlink> data =
// resp.docs.map((e) => Starlink.fromJson(e)).cast<Starlink>().toList();
// print("Qeury Starlinks ${data.length}");
// }
}
// More examples
_parseResponse(Response response) {
if (response.statusCode == 200) {
final jsonResponse = convert.jsonDecode(response.body);
return jsonResponse;
} else {
print('Request failed with status: ${response.statusCode}.');
}
}
fromJsonList<T>(List json, Function func) {
List caps = json.map((e) => func(e)).cast<T>().toList();
return caps;
}
Future<void> _fetchShips(SpaceXApi api) async {
final response = await api.getAllShips();
final json = _parseResponse(response);
if (json != null) {
List<Ship> data = json.map((e) => Ship.fromJson(e)).cast<Ship>().toList();
print("Fetch Ship ${data[0].name}");
}
}
Future<void> _fetchRocket(SpaceXApi api) async {
final response = await api.getAllRockets();
final json = _parseResponse(response);
if (json != null) {
List<Rocket> data =
json.map((e) => Rocket.fromJson(e)).cast<Rocket>().toList();
print("Fetch Rocket ${data[0].name}");
}
}
Future<void> _fetchRoadster(SpaceXApi api) async {
final response = await api.getRoadster();
final json = _parseResponse(response);
if (json != null) {
Roadster data = Roadster.fromJson(json);
print("Fetch Roadster ${data.earthDistanceKm}");
}
}
Future<void> _fetchPayloads(SpaceXApi api) async {
final response = await api.getAllPayloads();
final json = _parseResponse(response);
if (json != null) {
List<Payload> data =
json.map((e) => Payload.fromJson(e)).cast<Payload>().toList();
print("Fetch Payload ${data[0].name}");
}
}
Future<void> _fetchLaunches(SpaceXApi api) async {
final response = await api.getAllLaunches();
final json = _parseResponse(response);
if (json != null) {
List<Launch> data =
json.map((e) => Launch.fromJson(e)).cast<Launch>().toList();
print("Fetch Launch ${data[0].details}");
}
}
Future<void> _fetchLaunchPads(SpaceXApi api) async {
final response = await api.getAllLaunchPads();
final json = _parseResponse(response);
if (json != null) {
List<Launchpad> data =
json.map((e) => Launchpad.fromJson(e)).cast<Launchpad>().toList();
print("Fetch Launch ${data[0].details}");
}
}
Future<void> _fetchLandpads(SpaceXApi api) async {
final response = await api.getAllLandpads();
final json = _parseResponse(response);
if (json != null) {
List<Landpad> data =
json.map((e) => Landpad.fromJson(e)).cast<Landpad>().toList();
print("Fetch Landpad ${data[0].fullName}");
}
}
Future<void> _fetchHistory(SpaceXApi api) async {
final response = await api.getAllHistory();
final json = _parseResponse(response);
if (json != null) {
List<History> data =
json.map((e) => History.fromJson(e)).cast<History>().toList();
print("Fetch History ${data[0].title}");
}
}
Future<void> _fetchDragons(SpaceXApi api) async {
final response = await api.getAllDragons();
final json = _parseResponse(response);
if (json != null) {
List<Dragon> data =
json.map((e) => Dragon.fromJson(e)).cast<Dragon>().toList();
print("Fetch Dragon ${data[0].name}");
}
}
Future<void> _fetchCrew(SpaceXApi api) async {
final response = await api.getAllCrews();
final json = _parseResponse(response);
if (json != null) {
List<Crew> data = json.map((e) => Crew.fromJson(e)).cast<Crew>().toList();
print("Fetch Crew ${data[0].name}");
}
}
Future<void> _fetchCompany(SpaceXApi api) async {
final response = await api.getCompany();
final json = _parseResponse(response);
if (json != null) {
Company data = Company.fromJson(json);
print("Fetch Company ${data.name}");
}
}