Skip to content

Commit 304e9c8

Browse files
author
Mohamed Ben Makhlouf
committed
new zip code class version 2
1 parent bb32804 commit 304e9c8

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

pyzipcodeapi/api.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import requests
88

9-
from pyzipcodeapi.dataclass import Distance, Error
9+
from pyzipcodeapi.dataclass import Distance, Error, MultiDistance
1010
from pyzipcodeapi.enums import FormatEnum, UnitEnum, CountryEnum
1111
from pyzipcodeapi.options import OPTIONS
1212

@@ -104,13 +104,13 @@ def __init__(self, api_key: str):
104104
self.api_key = api_key
105105
self.con = HTTPSConnection(host=self.host)
106106

107-
def _make_api_call(
107+
def _api_call(
108108
self,
109109
option: str,
110110
f: FormatEnum,
111111
path: str,
112-
country: CountryEnum = CountryEnum.US,
113112
data_class: type | None = None,
113+
country: CountryEnum = CountryEnum.US,
114114
) -> DictReader | bytes | type | Element | Error:
115115
base_url = f"rest/v2/CA" if country == CountryEnum.CA else f"rest"
116116
self.con.request(
@@ -140,10 +140,19 @@ def distance(
140140
country: CountryEnum = CountryEnum.US,
141141
) -> Distance | DictReader | Element:
142142
"""distance.<format>/<zip_code1>/<zip_code2>/<units>"""
143-
return self._make_api_call(
144-
"distance",
145-
f,
146-
path=f"{zip_code1}/{zip_code2}/{units}",
147-
country=country,
148-
data_class=Distance,
149-
)
143+
path = f"{zip_code1}/{zip_code2}/{units}"
144+
dc = Distance
145+
return self._api_call("distance", f, path, dc, country=country)
146+
147+
def multi_distance(
148+
self,
149+
zip_code: str,
150+
zip_codes: list[str],
151+
units: UnitEnum = UnitEnum.KM,
152+
f: FormatEnum | None = FormatEnum.JSON,
153+
) -> MultiDistance | DictReader | Element:
154+
"""multi-distance.<format>/<zip_code>/<other_zip_codes>/<units>"""
155+
assert len(zip_codes) <= 100
156+
path = f"{zip_code}/{','.join(zip_codes)}/{units}"
157+
dc = MultiDistance
158+
return self._api_call("multi-distance", f, path, dc)

pyzipcodeapi/dataclass.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@ class Error:
1010
@dataclass
1111
class Distance:
1212
distance: float
13+
14+
15+
@dataclass
16+
class MultiDistance:
17+
distances: dict[str, float]

0 commit comments

Comments
 (0)