Skip to content

Commit 1647688

Browse files
committed
IMP support for Postcode API v3
1 parent aa0fc31 commit 1647688

1 file changed

Lines changed: 83 additions & 17 deletions

File tree

pyPostcode/__init__.py

Lines changed: 83 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ def __init__(self, id, message):
2828

2929
class Api(object):
3030

31-
def __init__(self, api_key, api_version=(2, 0, 0)):
31+
def __init__(self, api_key, api_version=(3, 0, 0)):
3232
if api_key is None or api_key is '':
3333
raise pyPostcodeException(
3434
0, "Please request an api key on http://postcodeapi.nu")
3535

3636
self.api_key = api_key
3737
self.api_version = api_version
38-
if api_version >= (2, 0, 0):
38+
if (2, 0, 0) <= api_version < (3, 0, 0):
3939
self.url = 'https://postcode-api.apiwise.nl'
4040
else:
4141
self.url = 'http://api.postcodeapi.nu'
@@ -58,9 +58,7 @@ def request(self, path=None):
5858
headers = {
5959
"Accept": "application/json",
6060
"Accept-Language": "en",
61-
# this is the v1 api
62-
"Api-Key": self.api_key,
63-
# this is the v2 api
61+
# this is the v2 and v3 api key
6462
"X-Api-Key": self.api_key,
6563
}
6664

@@ -77,28 +75,30 @@ def request(self, path=None):
7775
resultdata = resultdata.decode("utf-8") # for Python 3
7876
jsondata = json.loads(resultdata)
7977

80-
if self.api_version >= (2, 0, 0):
78+
if (2, 0, 0) <= self.api_version < (3, 0, 0):
8179
data = jsondata.get('_embedded', {}).get('addresses', [])
8280
if data:
8381
data = data[0]
8482
else:
8583
data = None
8684
else:
87-
data = jsondata['resource']
85+
data = jsondata
8886

8987
return data
9088

9189
def getaddress(self, postcode, house_number=None):
92-
if house_number is None:
93-
house_number = ''
94-
95-
if self.api_version >= (2, 0, 0):
96-
path = '/v2/addresses/?postcode={0}&number={1}'
90+
if (2, 0, 0) <= self.api_version < (3, 0, 0):
91+
path = '/v2/addresses/?postcode={0}'
92+
if house_number is not None:
93+
path += '&number={1}'
94+
resource = ResourceV2
9795
else:
98-
path = '/{0}/{1}'
96+
path = '/v3/lookup/{postcode}/{house_number}'
97+
if house_number is not None:
98+
path += '/{house_number}'
99+
resource = ResourceV3
99100
path = path.format(
100-
str(postcode),
101-
str(house_number))
101+
postcode=str(postcode), house_number=str(house_number))
102102

103103
try:
104104
data = self.request(path)
@@ -113,16 +113,33 @@ def getaddress(self, postcode, house_number=None):
113113
data = None
114114

115115
if data is not None:
116-
return Resource(data)
116+
return resource(data)
117117
else:
118118
return False
119119

120120

121-
class Resource(object):
121+
class Resource:
122122

123123
def __init__(self, data):
124124
self._data = data
125125

126+
def not_implemented(self):
127+
return NotImplemented
128+
129+
street = property(not_implemented)
130+
house_number = property(not_implemented)
131+
postcode = property(not_implemented)
132+
town = property(not_implemented)
133+
municipality = property(not_implemented)
134+
province = property(not_implemented)
135+
latitude = property(not_implemented)
136+
longitude = property(not_implemented)
137+
x = property(not_implemented)
138+
y = property(not_implemented)
139+
140+
141+
class ResourceV2(Resource):
142+
126143
@property
127144
def street(self):
128145
return self._data['street']
@@ -184,3 +201,52 @@ def y(self):
184201
if self._data.get('y'):
185202
return self._data.get('y')
186203
return self._get_geo_coordinates('rd')[1]
204+
205+
206+
class ResourceV3(Resource):
207+
208+
@property
209+
def street(self):
210+
return self._data['street']
211+
212+
@property
213+
def house_number(self):
214+
return self._data.get('number')
215+
216+
@property
217+
def postcode(self):
218+
return self._data.get('postcode')
219+
220+
@property
221+
def city(self):
222+
return self._data.get('city')
223+
224+
@property
225+
def municipality(self):
226+
return self._data.get('municipality')
227+
228+
@property
229+
def province(self):
230+
return self._data.get('province')
231+
232+
@property
233+
def coordinates(self):
234+
return self._data.get('location', {}).get('coordinates')
235+
236+
@property
237+
def latitude(self):
238+
coordinates = self.coordinates
239+
return coordinates and coordinates[1] or None
240+
241+
@property
242+
def longitude(self):
243+
coordinates = self.coordinates
244+
return coordinates and coordinates[0] or None
245+
246+
@property
247+
def x(self):
248+
return self.longitude
249+
250+
@property
251+
def y(self):
252+
return self.latitude

0 commit comments

Comments
 (0)