This repository has been archived by the owner on Aug 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PyInterstate.py
212 lines (175 loc) · 6.83 KB
/
PyInterstate.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#PyInterstate by @pygeek
import urllib2
import json
class InterstateError(Exception):
"""Base class for Interstate App Exceptions."""
pass
class AuthError(InterstateError):
"""Exception raised upon authentication errors."""
pass
class IdError(InterstateError):
"""Raised when an operation attempts to query's an Interstate \
Road or Roadmap that does not exist.
"""
pass
class InterstateApp(object):
"""Pythonic Interstate App API Wrapper
(http://interstateapp.com)
Requires:
-Python 2.6+
"""
__version__ = "0.2.0"
def __init__(self):
self.protocol = "http://"
self.base_url = "interstateapp.com"
self.api_version = "v1"
public_key = "public-key"
private_key = "private-key"
"""Installing opener authentication for inevitable, \
subsequent requests
"""
# create a password manager
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, self.protocol + \
self.base_url, public_key, private_key)
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
# create "opener" (OpenerDirector instance)
urlopener = urllib2.build_opener(handler)
urllib2.install_opener(urlopener)
def auth_test(self):
"""Authenticate Interstate App credentials; *public_key* : *private_key*."""
listAll_url = "{0}{1}/api/{2}/roadmap/listAll" \
.format(self.protocol, self.base_url, self.api_version)
try:
urllib2.urlopen(listAll_url)
except:
raise AuthError("Authentication Error: Please verify credentials.")
return True
def id_test(self,roadmap_id=None,road_id=None):
"""Verify whether Road or Roadmap Id exists."""
roadmap_get = "{0}{1}/api/{2}/roadmap/get/id/{3}" \
.format(self.protocol, self.base_url, self.api_version, \
roadmap_id)
road_get = "{0}{1}/api/{2}/road/get/id/{3}" \
.format(self.protocol, self.base_url, self.api_version, \
road_id)
if roadmap_id:
try:
urllib2.urlopen(roadmap_get)
except:
raise IdError("Id Error: Roadmap Id \"{0}\" does not exist.".format(roadmap_id))
return True
elif road_id:
try:
urllib2.urlopen(road_get)
except:
raise IdError("Id Error: Roadmap Id \"{0}\" does not exist.".format(road_id))
return True
class Roadmap(InterstateApp):
"""Contains methods for the Roadmap object."""
def get(self, roadmap_id):
"""roadmap/get:
Retrieve information regarding a specific \
Interstate roadmap.
Parameters:
- id(Roadmap ID)
The unique id of the Interstate roadmap.
Example Request:
http://interstateapp.com/api/v1/roadmap/get/id/ \
4c2d3b5f8ead0ec070010000
Outputs: JSON
See: http://interstateapp.com/developers/method/0/0
"""
if self.auth_test() and self.id_test(roadmap_id=roadmap_id):
get_url = "{0}{1}/api/{2}/roadmap/get/id/{3}" \
.format(self.protocol, self.base_url, self.api_version, \
roadmap_id)
roadmap = urllib2.urlopen(get_url)
roadmap = roadmap.read()
return json.loads(roadmap)
else:
return False
def listAll(self):
"""roadmap/listAll:
List all Interstate roadmaps associated with the \
used API Key.
Parameters:
None
Example Request:
http://interstateapp.com/api/v1/roadmap/listAll
Outputs: JSON
See: http://interstateapp.com/developers/method/0/1
"""
if self.auth_test():
listAll_url = "{0}{1}/api/{2}/roadmap/listAll" \
.format(self.protocol, self.base_url, self.api_version)
roadmap = urllib2.urlopen(listAll_url)
roadmap = roadmap.read()
return json.loads(roadmap)
else:
return False
def roads(self, roadmap_id):
"""roadmap/roads:
List all roads attached to the specific Interstate roadmap.
Parameters:
- id(Roadmap ID)
The unique id of the Interstate roadmap.
Example Request:
http://interstateapp.com/api/v1/roadmap/roads/id/ \
4c2d3b5f8ead0ec070010000
Outputs: JSON
See: http://interstateapp.com/developers/method/0/2
"""
if self.auth_test() and self.id_test(roadmap_id=roadmap_id):
roads_url = "{0}{1}/api/{2}/roadmap/roads/id/{3}" \
.format(self.protocol, self.base_url, self.api_version, \
roadmap_id)
roadmap = urllib2.urlopen(roads_url)
roadmap = roadmap.read()
return json.loads(roadmap)
else:
return False
class Road(InterstateApp):
"""Contains methods for the Road object."""
def get(self, road_id):
"""road/get:
Retrieve information regarding a specific Interstate road.
Parameters:
- id(Road ID)
The unique id of the Interstate road.
Example Request:
http://interstateapp.com/api/v1/road/get/id/ \
4c2d3b5f8ead0ec070010000
Outputs: JSON
See: http://interstateapp.com/developers/method/1/0
"""
if self.auth_test() and self.id_test(road_id=road_id):
get_url = "{0}{1}/api/{2}/road/get/id/{3}" \
.format(self.protocol, self.base_url, self.api_version, \
road_id)
road = urllib2.urlopen(get_url)
road = road.read()
return json.loads(road)
else:
return False
def updates(self, road_id):
"""road/get:
Retrieve updates attached to a specific Interstate road.
Parameters:
- id(Road ID)
The unique id of the Interstate road.
Example Request:
http://interstateapp.com/api/v1/road/updates/id/
4c2d3b5f8ead0ec070010000
Outputs: JSON
See: http://interstateapp.com/developers/method/1/1
"""
if self.auth_test() and self.id_test(road_id=road_id):
updates_url = "{0}{1}/api/{2}/road/updates/id/{3}" \
.format(self.protocol, self.base_url, self.api_version, \
road_id)
road = urllib2.urlopen(updates_url)
road = road.read()
return json.loads(road)
else:
return False