1 | #!/usr/bin/python3 |
||
2 | # pylint: disable=broad-except |
||
3 | |||
4 | 1 | """ Get and update from the API """ |
|
5 | |||
6 | 1 | import os |
|
7 | 1 | import requests |
|
8 | |||
9 | 1 | from src.scooter import Scooter |
|
10 | |||
11 | |||
12 | 1 | url = os.environ.get("API_URL") |
|
13 | |||
14 | |||
15 | 1 | class ApiData(Scooter): |
|
16 | """ Api class """ |
||
17 | # API endpoint URL |
||
18 | 1 | _URL = "http://localhost:1337/api/v1/graphql" if url is None else url |
|
19 | |||
20 | # Set the headers |
||
21 | 1 | _HEADERS = { "Content-Type": "application/json"} |
|
22 | |||
23 | |||
24 | 1 | def __init__(self, user_id: int) -> None: |
|
25 | """ Initialize class """ |
||
26 | 1 | super().__init__() |
|
27 | 1 | self.user_id = user_id |
|
28 | 1 | self.station = "" |
|
29 | |||
30 | |||
31 | 1 | def get_scooter_data(self, scooter_id: int) -> dict: |
|
32 | """ Get scooter data from API. """ |
||
33 | # Create the GraphQL query |
||
34 | 1 | query = ''' query getScooterById($id: String!) { |
|
35 | getScooterById(id: $id) { |
||
36 | id |
||
37 | latitude |
||
38 | longitude |
||
39 | speed |
||
40 | battery |
||
41 | status { |
||
42 | id |
||
43 | status |
||
44 | } |
||
45 | station { |
||
46 | id |
||
47 | station_name |
||
48 | } |
||
49 | } |
||
50 | } ''' |
||
51 | |||
52 | 1 | payload = { |
|
53 | 'query': query, |
||
54 | 'variables': { |
||
55 | 'id': str(scooter_id) |
||
56 | } |
||
57 | } |
||
58 | |||
59 | 1 | try: |
|
60 | # Send the POST request |
||
61 | 1 | response = requests.post(self._URL, json=payload, headers=self._HEADERS) |
|
62 | |||
63 | 1 | return response.json()["data"]["getScooterById"][0] |
|
64 | 1 | except (Exception, ConnectionError): |
|
65 | 1 | return -1 |
|
66 | |||
67 | |||
68 | 1 | View Code Duplication | def update_scooter(self) -> None: |
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
69 | """ Update api with all scooters data. """ |
||
70 | 1 | mutation = ''' mutation updateScooterById( |
|
71 | $id: String!, |
||
72 | $battery: String!, |
||
73 | $status_id: String!, |
||
74 | $longitude: String!, |
||
75 | $latitude: String!, |
||
76 | $price_id: String!, |
||
77 | $speed: String!, |
||
78 | $station_id: String!) { |
||
79 | updateScooterById( |
||
80 | id: $id, |
||
81 | battery: $battery, |
||
82 | status_id: $status_id, |
||
83 | longitude: $longitude, |
||
84 | latitude: $latitude, |
||
85 | price_id: $price_id, |
||
86 | speed: $speed, |
||
87 | station_id: $station_id) { id } |
||
88 | } ''' |
||
89 | |||
90 | 1 | payload = { |
|
91 | 'query': mutation, |
||
92 | 'variables': { |
||
93 | 'id': self.data["id"], |
||
94 | 'status_id': str(self.data["status"]), |
||
95 | 'latitude': str(self.data["lat"]), |
||
96 | 'longitude': str(self.data["lon"]), |
||
97 | 'speed': str(self.data["speed"]), |
||
98 | 'battery': str(self.data["battery"]), |
||
99 | 'station_id': str(self.data["station"]), |
||
100 | 'price_id': "1" |
||
101 | } |
||
102 | } |
||
103 | |||
104 | 1 | try: |
|
105 | 1 | requests.post(self._URL, json=payload, headers=self._HEADERS) |
|
106 | 1 | except (Exception, ConnectionError) as error: |
|
107 | 1 | print(error) |
|
108 | |||
109 | |||
110 | 1 | View Code Duplication | def update_rented_scooter(self) -> None: |
0 ignored issues
–
show
|
|||
111 | """ Update api with scooter's new position, speed, status and battery level. """ |
||
112 | 1 | mutation = ''' mutation updateRentedScooterById( |
|
113 | $id: String!, |
||
114 | $battery: String!, |
||
115 | $status_id: String!, |
||
116 | $longitude: String!, |
||
117 | $latitude: String!, |
||
118 | $speed: String!) { |
||
119 | updateRentedScooterById( |
||
120 | id: $id, |
||
121 | battery: $battery, |
||
122 | status_id: $status_id, |
||
123 | longitude: $longitude, |
||
124 | latitude: $latitude, |
||
125 | speed: $speed) |
||
126 | { |
||
127 | id |
||
128 | } |
||
129 | } ''' |
||
130 | |||
131 | 1 | payload = { |
|
132 | 'query': mutation, |
||
133 | 'variables': { |
||
134 | 'id': self.data["id"], |
||
135 | 'status_id': str(self.data["status"]), |
||
136 | 'latitude': str(self.data["lat"]), |
||
137 | 'longitude': str(self.data["lon"]), |
||
138 | 'speed': str(self.data["speed"]), |
||
139 | 'battery': str(self.data["battery"]), |
||
140 | } |
||
141 | } |
||
142 | |||
143 | 1 | try: |
|
144 | 1 | requests.post(self._URL, json=payload, headers=self._HEADERS) |
|
145 | 1 | except (Exception, ConnectionError) as error: |
|
146 | 1 | print(error) |
|
147 | |||
148 | |||
149 | 1 | def rent_scooter(self) -> None: |
|
150 | """ |
||
151 | Create log. Data to be added is scooter's position, start date/time and scooter/user id. |
||
152 | """ |
||
153 | 1 | mutation = ''' mutation rentScooter( |
|
154 | $id: String!, |
||
155 | $user_id: String!, |
||
156 | $longitude: String!, |
||
157 | $latitude: String!) { |
||
158 | rentScooter( |
||
159 | id: $id, |
||
160 | user_id: $user_id, |
||
161 | longitude: $longitude, |
||
162 | latitude: $latitude) |
||
163 | { |
||
164 | id |
||
165 | success |
||
166 | } |
||
167 | } ''' |
||
168 | |||
169 | 1 | payload = { |
|
170 | 'query': mutation, |
||
171 | 'variables': { |
||
172 | 'id': self.data["id"], |
||
173 | 'user_id': str(self.user_id), |
||
174 | 'longitude': str(self.data["lon"]), |
||
175 | 'latitude': str(self.data["lat"]), |
||
176 | } |
||
177 | } |
||
178 | |||
179 | 1 | try: |
|
180 | 1 | response = requests.post(self._URL, json=payload, headers=self._HEADERS) |
|
181 | |||
182 | # save the station |
||
183 | 1 | self.station = response.json()["data"]["rentScooter"]["success"] |
|
184 | 1 | except (Exception, ConnectionError) as error: |
|
185 | 1 | print(error) |
|
186 | |||
187 | |||
188 | 1 | def return_scooter(self, time: int) -> None: |
|
189 | """ |
||
190 | Create log. Data to be added is scooter's position, start date/time and scooter/user id. |
||
191 | """ |
||
192 | 1 | mutation = ''' mutation returnScooter( |
|
193 | $id: String!, |
||
194 | $user_id: String!, |
||
195 | $longitude: String!, |
||
196 | $latitude: String!, |
||
197 | $time: String!, |
||
198 | $station: String!) { |
||
199 | returnScooter( |
||
200 | id: $id, |
||
201 | user_id: $user_id, |
||
202 | longitude: $longitude, |
||
203 | latitude: $latitude, |
||
204 | time: $time, |
||
205 | station: $station) |
||
206 | { |
||
207 | success |
||
208 | } |
||
209 | } ''' |
||
210 | |||
211 | 1 | payload = { |
|
212 | 'query': mutation, |
||
213 | 'variables': { |
||
214 | 'id': self.data["id"], |
||
215 | 'user_id': str(self.user_id), |
||
216 | 'longitude': str(self.data["lon"]), |
||
217 | 'latitude': str(self.data["lat"]), |
||
218 | 'time': str(time), |
||
219 | 'station': self.station |
||
220 | } |
||
221 | } |
||
222 | |||
223 | 1 | try: |
|
224 | 1 | requests.post(self._URL, json=payload, headers=self._HEADERS) |
|
225 | 1 | except (Exception, ConnectionError) as error: |
|
226 | 1 | print(error) |
|
227 | |||
228 | |||
229 | 1 | def get_city_data(self) -> dict: |
|
230 | """ |
||
231 | Get city's center position, id and area where the scooter is located. |
||
232 | And adds it to city dictionary. |
||
233 | """ |
||
234 | 1 | query = ''' query getCityByScooterId($id: String!) { |
|
235 | getCityByScooterId(id: $id) { |
||
236 | id |
||
237 | latitude |
||
238 | longitude |
||
239 | area |
||
240 | } |
||
241 | } ''' |
||
242 | |||
243 | 1 | payload = { |
|
244 | 'query': query, |
||
245 | 'variables': { |
||
246 | 'id': self.data["id"] |
||
247 | } |
||
248 | } |
||
249 | |||
250 | 1 | try: |
|
251 | 1 | response = requests.post(self._URL, json=payload, headers=self._HEADERS) |
|
252 | |||
253 | 1 | return response.json()["data"]["getCityByScooterId"][0] |
|
254 | 1 | except (Exception, ConnectionError): |
|
255 | 1 | return -1 |
|
256 | |||
257 | |||
258 | 1 | def get_station(self, zone_id: str) -> dict: |
|
259 | """ |
||
260 | return random charging/maintenance station data in the city where the scooter is located. |
||
261 | Zone id: 1- Charging Station, 2- Parking Station, 3- Bike Statione, 4- Maintenance Station. |
||
262 | """ |
||
263 | 1 | query = ''' query getStationByCityIdAndZoneId($cityId: String!, $zoneId: String!) { |
|
264 | getStationByCityIdAndZoneId(cityId: $cityId, zoneId: $zoneId) { |
||
265 | id |
||
266 | latitude |
||
267 | longitude |
||
268 | } |
||
269 | } ''' |
||
270 | |||
271 | 1 | payload = { |
|
272 | 'query': query, |
||
273 | 'variables': { |
||
274 | 'cityId': self.city["id"], |
||
275 | 'zoneId': zone_id |
||
276 | } |
||
277 | } |
||
278 | |||
279 | 1 | try: |
|
280 | 1 | response = requests.post(self._URL, json=payload, headers=self._HEADERS) |
|
281 | |||
282 | 1 | return response.json()["data"]["getStationByCityIdAndZoneId"][0] |
|
283 | 1 | except (Exception, ConnectionError): |
|
284 | return -1 |
||
285 |