-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
186 lines (155 loc) · 6.74 KB
/
server.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# -*- coding: UTF-8 -*-
# 6/1 THIS SERVER.PY IS EXACTLY SERVER_CORS.PY, SAVED W/O _CORS FOR HEROKU
import os
from flask import Flask,request
from urlparse import urlparse
import requests
import demjson
#import jsonp_wrap
#import cors_wrap
from datetime import timedelta
from flask import make_response, request, current_app
from functools import update_wrapper
app = Flask(__name__)
def crossdomain(origin=None, methods=None, headers=None,
max_age=21600, attach_to_all=True,
automatic_options=True):
if methods is not None:
methods = ', '.join(sorted(x.upper() for x in methods))
if headers is not None and not isinstance(headers, basestring):
headers = ', '.join(x.upper() for x in headers)
if not isinstance(origin, basestring):
origin = ', '.join(origin)
if isinstance(max_age, timedelta):
max_age = max_age.total_seconds()
def get_methods():
if methods is not None:
return methods
options_resp = current_app.make_default_options_response()
return options_resp.headers['allow']
def decorator(f):
def wrapped_function(*args, **kwargs):
if automatic_options and request.method == 'OPTIONS':
resp = current_app.make_default_options_response()
else:
resp = make_response(f(*args, **kwargs))
if not attach_to_all and request.method != 'OPTIONS':
return resp
h = resp.headers
h['Access-Control-Allow-Origin'] = origin
h['Access-Control-Allow-Methods'] = get_methods()
h['Access-Control-Max-Age'] = str(max_age)
if headers is not None:
h['Access-Control-Allow-Headers'] = headers
return resp
f.provide_automatic_options = False
return update_wrapper(wrapped_function, f)
return decorator
@app.route("/")
def hello():
return "Hello World!"
# GENERIC
@app.route("/api/<api_path>")
def genericPassthru(api_path):
query = request.query_string
edm_qry = "http://api.edmunds.com/v1/api/vehicle/stylerepository/" + api_path + '?' + query #+ '&api_key=sbzh2xtvh99h73pzr398c2fc&fmt=json'
#http://api.edmunds.com/v1/api/vehicle/stylerepository/findstylesbymakemodelyear?make=honda&model=accord&year=2003&api_key=sbzh2xtvh99h73pzr398c2fc&fmt=json
try:
edResponse = requests.get(edm_qry)
except requests.ConnectionError:
return "Connection Error"
print "received the api string"
styleObj = edResponse.text
style_dict = demjson.decode(styleObj)
print "done with decoding json to dict"
print style_dict["styleHolder"][0]["modelName"] # prints to terminal
return style_dict["styleHolder"][0]["modelName"]
return styleObj # this returns api string in its entirety, successfully.
#MODELS for menu
@app.route("/models/<api_call>")
@crossdomain(origin='*')
def modelPassthru(api_call):
query = request.query_string
edm_qry = "http://api.edmunds.com/v1/api/vehicle/modelrepository/" + api_call + '?' + query
try:
edResponse = requests.get(edm_qry)
except requests.ConnectionError:
return "Connection Error"
styleObj = edResponse.text
model_dict = demjson.decode(styleObj)
# keys I need here are just niceName, name, modelYears.
keys_as_is = { 'niceName', 'name', 'modelYears' }
new_model_dict = {}
new_model_dict["modelHolder"] = []
for model in model_dict["modelHolder"]: # array of dicts, style is a dict
temp_dict = {}
for key in keys_as_is:
temp_dict[key] = model[key]
new_model_dict["modelHolder"].append( temp_dict )
return demjson.encode( new_model_dict )
#TRIMS for menu
@app.route("/trims/<api_call>")
@crossdomain(origin='*')
def trimPassthru(api_call):
query = request.query_string
edm_qry = "http://api.edmunds.com/v1/api/vehicle/stylerepository/" + api_call + '?' + query
try:
edResponse = requests.get(edm_qry)
except requests.ConnectionError:
return "Connection Error"
styleObj = edResponse.text
style_dict = demjson.decode(styleObj)
# keys I need here are just name and price...
keys_as_is = { 'price', 'name', 'id','makeName','makeNiceName','modelNiceName','modelName','year' }
new_style_dict = {}
new_style_dict["styleHolder"] = []
for style in style_dict["styleHolder"]: # array of dicts, style is a dict
temp_dict = {}
for key in keys_as_is:
temp_dict[key] = style[key]
new_style_dict["styleHolder"].append( temp_dict )
print new_style_dict
return demjson.encode( new_style_dict )
@app.route("/fullstyleapi/<styleID_to_get>") #, methods=['GET'])
@crossdomain(origin='*')
def fullStylePassthru(styleID_to_get):
# sample destination: var http://www.edmunds.com/api/vehicle/style/100003100?fmt=full_json
edm_qry = 'http://www.edmunds.com/api/vehicle/style/' + styleID_to_get + '?fmt=full_json'
try:
edResponse = requests.get(edm_qry)
except requests.ConnectionError:
return "Connection Error: Request was: " + edm_qry
print edm_qry
styleString = edResponse.text
style_dict = demjson.decode(styleString)
#return style_dict["styleHolder"][0]["modelName"] # returns just model name
keys_as_is = { 'trim', 'niceName', 'makeName', 'year', 'id', 'modelName', 'engineCylinder', 'price', 'makeNiceName', 'engineSize', 'name', 'modelNiceName', 'publicationState' }
new_style_dict = {}
new_style_dict["styleHolder"] = []
new_style_dict["styleHolder"].append( {} )
for key in keys_as_is:
print key
new_style_dict["styleHolder"][0][key] = style_dict["styleHolder"][0][key]
# set up deeper structure
new_style_dict["styleHolder"][0]["attributeGroups"] = {}
new_style_dict["styleHolder"][0]["standardEquipment"] = []
# attributeGroups
attgrp_keepers = ["INTERIOR_DIMENSIONS","CARGO_DIMENSIONS","SPECIFICATIONS","EXTERIOR_DIMENSIONS","CRASH_TEST_RATINGS","PRICING"]
for akey in attgrp_keepers:
new_style_dict["styleHolder"][0]["attributeGroups"][akey] = style_dict["styleHolder"][0]["attributeGroups"][akey]
new_style_dict
# standardEquipment
stdequip_keepers = ["ENGINE"]
for edm_equip_dict in style_dict["styleHolder"][0]["standardEquipment"]:
for key in stdequip_keepers:
if key == edm_equip_dict["equipmentClass"]: # we found it boss
new_style_dict["styleHolder"][0]["standardEquipment"].append( edm_equip_dict )
stdequip_keepers.pop( stdequip_keepers.index(key) ) # don't bother looking for it again
break
if len(stdequip_keepers) == 0: # ran out of stdEquip objects to find
break #breaks out of biggest loop
print new_style_dict["styleHolder"][0]["standardEquipment"][0]
return demjson.encode( new_style_dict )
if __name__ == "__main__":
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)