Skip to content

Commit 28ed205

Browse files
author
Mohamed Ben Makhlouf
committed
finish match-close option
1 parent 8d174c8 commit 28ed205

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

pyzipcodeapi/api.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import requests
99

10-
from pyzipcodeapi.dataclass import Distance, Error, MultiDistance, Radius, MultiRadius
10+
from pyzipcodeapi.dataclass import Distance, Error, MultiDistance, Radius, MultiRadius, MatchClose
1111
from pyzipcodeapi.enums import FormatEnum, UnitEnum, CountryEnum
1212
from pyzipcodeapi.options import OPTIONS
1313

@@ -143,14 +143,19 @@ def _post(self, option: str, path: str, data: dict):
143143

144144
def parse_response(
145145
self, data_class: type | None = None
146-
) -> DictReader | bytes | type | Element | Error:
146+
) -> DictReader | bytes | type | Element | Error | list[type]:
147147
response = self.con.getresponse()
148148
success = response.status == 200
149149
data = response.read()
150150
if self.format == FormatEnum.JSON:
151151
data = loads(data)
152152
if success:
153-
return data_class(**data) if data_class else data
153+
if data_class:
154+
if type(data) == dict:
155+
return data_class(**data)
156+
if type(data) == list:
157+
return [data_class(**d) for d in data]
158+
return data
154159
return Error(**data)
155160
elif self.format == FormatEnum.CSV:
156161
return DictReader(StringIO(data.decode()), delimiter=",")
@@ -207,3 +212,10 @@ def multi_radius(
207212
body["addrs"] = "\n".join(addresses)
208213
self._post("multi-radius", f"{distance}/{units}", data=body)
209214
return self.parse_response(data_class=MultiRadius)
215+
216+
def match_close(
217+
self, zip_codes: list[str], distance: int, units: UnitEnum = UnitEnum.KM
218+
) -> list[MatchClose] | DictReader | Element:
219+
"""match-close.<format>/<zip_codes>/<distance>/<units>"""
220+
self._get("match-close", f"{','.join(zip_codes)}/{distance}/{units}")
221+
return self.parse_response(data_class=MatchClose)

pyzipcodeapi/dataclass.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,10 @@ class MultiRadius:
4949
@property
5050
def info(self) -> list[MultiRadiusInfo]:
5151
return [MultiRadiusInfo(**r) for r in self.responses]
52+
53+
54+
@dataclass
55+
class MatchClose:
56+
zip_code1: str
57+
zip_code2: str
58+
distance: float

pyzipcodeapi/example.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@
3535
)
3636
print("------------------")
3737
# https://www.zipcodeapi.com/rest/<api_key>/match-close.<format>/<zip_codes>/<distance>/<units>
38-
print(obj.get("match-close", f).filter(zip_codes="941asd32", distance="5", units=u))
39-
print("------------------")
40-
# https://www.zipcodeapi.com/rest/<api_key>/info.<format>/<zip_code>/<units>
41-
print(obj.get("info", f).filter(zip_code="94132", units=ou))
38+
print(
39+
zca.match_close(
40+
zip_codes=["22911", "22902", "22904", "22905"], distance=120, units=u2
41+
)
42+
)
4243
print("------------------")
4344
# https://www.zipcodeapi.com/rest/<api_key>/multi-info.<format>/<zip_code>/<units>
4445
print(obj.get("multi-info", f).filter(zip_code="94132", units=ou))

0 commit comments

Comments
 (0)