-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
60 lines (54 loc) · 2 KB
/
main.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
from flask import Blueprint, Flask, jsonify
from models import CelestialBodies, Landmark, User, Passenger
import requests
main = Blueprint('main', __name__)
@main.route('/')
def hello():
return jsonify({'message': "App is running"})
@main.route('/api/v1/celestial_bodies', methods = ['GET'])
def get_bodies():
try:
bodies = CelestialBodies.query.all()
if not bodies:
return jsonify({'errors': 'No celestial bodies found.'}), 404
else:
return jsonify({'data': [e.serialize() for e in bodies]})
except Exception as e:
return(str(e))
@main.route('/api/v1/celestial_bodies/<id>', methods = ['GET'])
def get_one_body(id):
try:
celestial_body = CelestialBodies.query.get(id)
if celestial_body is not None:
return jsonify(celestial_body.serialize())
else:
return jsonify({'errors': f'No object found with id: {id}.'}), 404
except Exception as e:
return(str(e))
@main.route('/api/v1/news', methods = ['GET'])
def get_api():
try:
response = requests.get('https://spaceflightnewsapi.net/api/v1/articles')
return response.json()
except Exception as response:
return(str("Bad Request"))
@main.route('/api/v1/celestial_bodies/<id>/landmarks', methods = ['GET'])
def get_landmarks(id):
try:
landmarks = Landmark.query.filter(Landmark.celestial_body_id == id)
if CelestialBodies.query.get(id) is not None:
return jsonify({'data': [e.serialize() for e in landmarks]})
else:
return jsonify({'errors': f'No celestial body with id: {id}.'}), 404
except Exception as e:
return(str(e))
@main.route('/api/v1/landmarks/<id>', methods = ['GET'])
def get_landmark(id):
try:
landmark = Landmark.query.get(id)
if landmark is not None:
return jsonify(landmark.serialize())
else:
return jsonify({'errors': f'No landmark found with id: {id}.'}), 404
except Exception as e:
return(str(e))