Skip to content

Instantly share code, notes, and snippets.

@mikery
Created June 29, 2019 12:24
Show Gist options
  • Save mikery/d37ff0be69b866da5584dfae4c98a64c to your computer and use it in GitHub Desktop.
Save mikery/d37ff0be69b866da5584dfae4c98a64c to your computer and use it in GitHub Desktop.
schiphol-external-adaptor
import json
import requests
from flask import Flask, request, jsonify
app = Flask(__name__)
SCHIPHOL_API_ID = 'x'
SCHIPHOL_API_KEY = 'y'
SCHIPHOL_API_URL = 'https://api.schiphol.nl/public-flights/'
headers = {
'Accept': 'application/json',
'resourceversion': 'v4',
'app_id': SCHIPHOL_API_ID,
'app_key': SCHIPHOL_API_KEY
}
@app.route('/flight', methods=['POST', ])
def get_flight_status():
cl_request = request.get_json()
flight_id = cl_request['data']['flightId']
response = requests.request('GET', f'{SCHIPHOL_API_URL}flights/{flight_id}', headers=headers)
if response.status_code != 200:
app.logger.info(f'Error {response.status_code}: {response.text}')
return
flight_data = response.json()
if 'ARR' in flight_data['publicFlightState']['flightStates']:
result = 1
elif 'CNX' in flight_data['publicFlightState']['flightStates']:
result = 2
else:
result = 0
job_result = {
"jobRunID": cl_request['id'],
"data": {
'flightStatus': result,
'last_update': flight_data['lastUpdatedAt']
},
"status": "completed",
"error": None,
"pending": False
}
return jsonify(job_result)
if __name__ == '__main__':
app.run(host='0.0.0.0')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment