-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
101 lines (90 loc) · 2.69 KB
/
app.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
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
import json
import os
import logging
import sys
import config
import requests
from flask import Flask, request, Response, jsonify
from flask_restful import Api, Resource, reqparse
from flasgger import Swagger, swag_from
from utils.utils import get_food
import yaml
import time
# Setup Flask Server
app = Flask(__name__)
# Create an APISpec
template = {
"swagger": "2.0",
"info": {
"title": "Fatsecret API",
"description": "A Restful API for Fatsecret food info",
"version": "1.0.0",
"contact": {
"name": "T1b4lt",
"github": "https://github.com/T1b4lt",
}
}
}
app.config['SWAGGER'] = {
'title': 'Fatsecret API',
'uiversion': 3,
"specs_route": "/"
}
swagger = Swagger(app, template=template)
app.config.from_object(config.Config)
api = Api(app)
config_file = open('config.yaml')
config_data = yaml.load(config_file, Loader=yaml.FullLoader)
class FoodEndpoint(Resource):
def get(self, country, food_name):
"""
get endpoint
---
tags:
- Food API Endpoint
parameters:
- name: country
in: path
type: string
required: true
description: country from which you want the information (supported [us, es])
- name: food_name
in: path
type: string
required: true
description: name of the food
responses:
500:
description: NOT OK
200:
description: OK
schema:
id: food
properties:
type:
type: string
description: Type of object
country:
type: string
description: Country from which you want the information
food_array:
type: array of food_objects
description: Array of food_objects
timestamp:
type: timestamp
description: Timestamp of petition
"""
url_domain = config_data['countries'][country]['domain']
url_resource = config_data['countries'][country]['resource']
food_name = food_name.replace(' ', '+')
food_array = get_food(url_domain, url_resource, food_name)
return jsonify({
"type": 'food',
"country": country,
"food_array": [food_obj.to_json() for food_obj in food_array],
"timestamp": time.time()
})
# Api resource routing
api.add_resource(FoodEndpoint, '/food/<string:country>/<string:food_name>')
if __name__ == "__main__":
app.run(debug=True, port=config_data['port'])