-
Notifications
You must be signed in to change notification settings - Fork 8
/
webserver.py
198 lines (175 loc) · 7.66 KB
/
webserver.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
187
188
189
190
191
192
193
194
195
196
197
198
# -*- coding: utf-8 -*-
__author__ = "C. Sudama, Matthew Alhonte"
__license__ = "Apache License 2.0: http://www.apache.org/licenses/LICENSE-2.0"
import urllib3.contrib.pyopenssl
urllib3.contrib.pyopenssl.inject_into_urllib3()
from os import environ
from flask import Flask, request, jsonify
from flask_swagger import swagger
from nyc_geoclient import Geoclient
from nyctext.nycaddress import parse_with_geo
# from flask.ext.cors import CORS
# CORS(app, resources=r'/*', allow_headers='Content-Type')
app = Flask(__name__)
# load app keys from environment
appid = environ['DOITT_CROL_APP_ID']
appkey = environ['DOITT_CROL_APP_KEY']
g = Geoclient(appid, appkey)
@app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers',
"Authorization, Content-Type")
response.headers.add('Access-Control-Expose-Headers', "Authorization")
# "GET, POST, PUT, DELETE, OPTIONS")
response.headers.add('Access-Control-Allow-Methods',
"GET, POST")
response.headers.add('Access-Control-Allow-Credentials', "true")
response.headers.add('Access-Control-Max-Age', 60 * 60 * 24 * 20)
return response
@app.route('/', methods=['GET'])
def index():
return "BetaNYC 5 Borough's address finder"
@app.route('/api/parseaddresses', methods=['POST'])
def parseaddresses():
"""
Parse Addresses
The only endpoint used to submit a string to be parsed.
---
responses:
'200':
description: Response Payload
schema:
id: refLocation
required:
- refLocation
properties:
refLocation:
type: array
items:
schema:
id: location
required:
- "@type"
- "@context"
- address
- geo
properties:
"@type":
type: string
example: Place
"@context":
type: string
example: http://schema.org
address:
schema:
id: address
required:
- "@type"
- addressLocality
- addressRegion
- postalCode
- streetAddress
- borough
properties:
"@type":
type: string
example: PostalAddress
streetAddress:
type: string
example: >
31-01 Ditmars
Boulevard
addressLocality:
type: string
example: New York City
addressRegion:
type: string
example: NY
postalCode:
type: string
example: 11105
borough:
type: string
example: Queens
geo:
schema:
id: geo
required:
- "@type"
- longitude
- latitude
properties:
"@type":
type: string
example: GeoCoordinates
longitude:
type: string
example: 40.776306
latitude:
type: string
example: -73.910118
default:
description: Unexpected error
schema:
id: Error
required:
- code
- message
properties:
code:
type: integer
format: int32
example: 400
message:
type: string
description: Error message
example: Invalid or Missing JSON Request
parameters:
- in: body
name: source
schema:
required:
- source
id: Input
properties:
source:
type: string
description: String to parse
example: >
NOTICE IS HEREBY GIVEN, pursuant to law, that
the New York City Department of Consumer
Affairs will hold a Public Hearing on
Wednesday, January 28, 2015, at 2:00 P.M., at
66 John Street, 11th Floor, in the Borough of
Manhattan, on the following petitions for
sidewalk café revocable consent: 1. 132
Mulberry Inc. 132 Mulberry Street in the
Borough of Manhattan
"""
global g
if request.method == 'GET':
return 'So, instructions would be printed here...'
try:
data = request.json
source = data['source'].encode('utf8')
ret = jsonify(refLocation=parse_with_geo(source, g, True))
except Exception, e:
print 'Exception: %s' % e
errmsg = 'Invalid or Missing JSON Request'
ret = jsonify({'code': 400, 'message': errmsg})
return ret
@app.route('/spec')
def spec():
swag = swagger(app)
swag['info']['version'] = '0.1'
swag['info']['title'] = "BetaNYC 5 Borough's address finder"
return jsonify(swag)
@app.route('/api')
def apiroot():
return app.send_static_file('index.html')
@app.route('/api/<path:path>')
def api(path):
return app.send_static_file(path)
if __name__ == '__main__':
app.run(host='0.0.0.0')