-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
160 lines (133 loc) · 3.77 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
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
from flask import Flask, jsonify, abort
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
from scraper import scrape_area_codes
from flasgger import Swagger
from exceptions import StateNotFoundError
swagger_config = {
"headers": [],
"specs": [
{
"endpoint": 'apispec_1',
"route": '/apispec_1.json',
"rule_filter": lambda rule: True, # all in
"model_filter": lambda tag: True, # all in
}
],
"static_url_path": "/flasgger_static",
"swagger_ui": True,
"specs_route": "/apidocs/"
}
template = {
"swagger": "2.0",
"info": {
"title": "OpenAreaText",
"description": "Open Source area codes and locale API for SMS messaging and marketing companies.",
"version": "1.0.0"
}
}
app = Flask(__name__)
Swagger(app, config=swagger_config, template=template)
limiter = Limiter(get_remote_address, app=app, default_limits=["200 per day", "50 per hour"])
# CSV_FILE_PATH = 'area_codes.csv'
area_codes_data = scrape_area_codes()
@app.route('/', methods=['GET'])
def index():
"""
Default API route.
---
tags:
- Default
responses:
200:
description: Welcome message of the API.
"""
return jsonify({"message": "Welcome to the OpenAreaText API!"})
@app.route('/areacodes', methods=['GET'])
def get_all_area_codes():
""" Get all area codes in a list.
---
tags:
- Area Codes
responses:
200:
description: Returns a list of area codes for the specified state.
"""
return jsonify(area_codes_data)
@app.route('/areacodes/<string:state>', methods=['GET'])
def get_area_codes_by_state(state):
"""
Get area codes for a specific state.
---
tags:
- Area Codes
parameters:
- name: state
in: path
type: string
required: true
description: The state for which to look up area codes
responses:
200:
description: Returns a list of area codes for the specified state.
examples:
application/json: [205, 251, 256, 334, 659, 938]
404:
description: State not found.
"""
try:
state = state.lower()
if state in area_codes_data:
return jsonify(area_codes_data[state])
# area_codes = get_area_codes_by_state_from_db(state.lower())
# return jsonify(area_codes)
except StateNotFoundError:
abort(404, description="State not found")
@app.route('/states', methods=['GET'])
def get_all_states():
"""
Get a list of all states.
---
tags:
- States
responses:
200:
description: Returns a list of all states.
"""
states = list(area_codes_data.keys())
return jsonify(states)
# states = get_states()
# return jsonify(states)
@app.route('/states/<int:area_code>', methods=['GET'])
def get_state_by_area_code(area_code):
"""
Get the state corresponding to an area code.
---
tags:
- States
parameters:
- name: area_code
in: path
type: integer
required: true
description: The area code to lookup
responses:
200:
description: Returns the state corresponding to the area code.
400:
description: Invalid area code format.
404:
description: Area code not found.
"""
if not isinstance(area_code, int):
abort(400, description="Invalid area code format")
for state, codes in area_codes_data.items():
if area_code in codes:
return jsonify(state)
# state = get_state_by_area_code_from_db(area_code)
# if state:
# return jsonify(state)
else:
abort(404, description="Area code not found")
if __name__ == '__main__':
app.run(debug=True)