Skip to content

Commit 674a948

Browse files
author
Mohamed Ben Makhlouf
committed
finish zip codes option
1 parent c428121 commit 674a948

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

pyzipcodeapi/api.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from http.client import HTTPSConnection
33
from io import StringIO
44
from json import loads
5-
from urllib.parse import urlencode
5+
from urllib.parse import quote_plus, urlencode
66
from xml.etree.ElementTree import Element, fromstring
77

88
import requests
@@ -15,10 +15,13 @@
1515
MultiDistance,
1616
MultiRadius,
1717
Radius,
18+
ZipCode,
1819
)
1920
from pyzipcodeapi.enums import CountryEnum, DistanceUnitEnum, FormatEnum, GeoUnitEnum
2021
from pyzipcodeapi.options import OPTIONS
2122

23+
ResultType = DictReader | bytes | type | Element | Error | list[type]
24+
2225
BASE_URL = "https://www.zipcodeapi.com/rest/{api_key}/{option}.{format}/"
2326
FORMAT = ["json", "xml", "csv"]
2427

@@ -149,9 +152,7 @@ def _post(self, option: str, path: str, data: dict):
149152
headers=headers,
150153
)
151154

152-
def _parse_response(
153-
self, data_class: type | None = None
154-
) -> DictReader | bytes | type | Element | Error | list[type]:
155+
def _parse_response(self, data_class: type | None = None) -> ResultType:
155156
response = self.con.getresponse()
156157
success = response.status == 200
157158
data = response.read()
@@ -250,3 +251,19 @@ def multi_info(
250251
"""multi-info.<format>/<zip_code>/<units>"""
251252
self._get("multi-info", f"{','.join(zip_codes)}/{units}")
252253
return self._parse_response()
254+
255+
def city_zip_codes(
256+
self, city: str, state: str
257+
) -> ZipCode | DictReader | Element:
258+
"""
259+
US: city-zips.<format>/<city>/<state>
260+
CA: city-postal-codes.<format>/<city>/<province>
261+
"""
262+
option = "city-postal-codes" if self.country == CountryEnum.CA else "city-zips"
263+
self._get(option, f"{quote_plus(city)}/{quote_plus(state)}")
264+
return self._parse_response(data_class=ZipCode)
265+
266+
def state_zip_codes(self, state: str) -> ZipCode | DictReader | Element:
267+
"""state-zips.<format>/<state>"""
268+
self._get("state-zips", f"{quote_plus(state)}")
269+
return self._parse_response(data_class=ZipCode)

pyzipcodeapi/dataclass.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,8 @@ class Info:
6868
timezone: dict
6969
acceptable_city_names: list[dict]
7070
area_codes: list[int]
71+
72+
73+
@dataclass
74+
class ZipCode:
75+
zip_codes: list[str]

pyzipcodeapi/example.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@
4444
# https://www.zipcodeapi.com/rest/<api_key>/multi-info.<format>/<zip_code>/<units>
4545
print(zca.multi_info(zip_codes=["22911"], units=ug))
4646
print("------------------")
47+
# https://www.zipcodeapi.com/rest/<api_key>/city-zips.<format>/<city>/<state>
48+
print(zca.city_zip_codes(city="New York", state="VA"))
49+
print("------------------")
50+
# https://www.zipcodeapi.com/rest/<api_key>/state-zips.<format>/<state>
51+
print(zca.state_zip_codes(state="VA"))
52+
print("------------------")
4753
# https://www.zipcodeapi.com/rest/<api_key>/radius-sql.<format>/<lat>/<long>/<lat_long_units>/<distance>/<units>/
4854
# <lat_field_name>/<long_field_name>/<precision>
4955
print(

0 commit comments

Comments
 (0)