-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicroservice.py
60 lines (44 loc) · 1.25 KB
/
microservice.py
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
# written by Kelsey Collins
from time import sleep
import requests
import json
#thomas' key
key = '2iQIs1WfYcfWAFhfY5WAmpvlOlJGnGZY'
url = 'https://aeroapi.flightaware.com/aeroapi/flights/'
def createFiles():
f = open("request.txt", "w")
f.close()
f = open("response.txt", "w")
f.close()
def listenForRequest():
# check for flight number in text file
request = open("request.txt", "r+")
flight = request.readline().strip()
while not flight:
request.close()
sleep(1)
request = open("request.txt", "r+")
flight = request.readline().strip()
# clear request text file
request.truncate(0)
request.close()
sleep(1)
return flight
def flightAware(flight):
# send request to API
r = requests.get(url + flight, headers={"x-apikey": key})
return r
def writeResponse(r):
with open("response.txt", "w") as response:
# error handling for 4XX status codes
if 399 < r.status_code < 500:
json.dump({'Error': r.status_code}, response)
else:
json.dump(r.json(), response)
# close response text file
response.close()
createFiles()
while True:
flight = listenForRequest()
r = flightAware(flight)
writeResponse(r)