Skip to content

Commit fab04d0

Browse files
authored
Merge pull request #66 from bennylope/auto_load_api_version
Change default for auto_load_api_version Closes gh-65
2 parents 94eed72 + 75272b7 commit fab04d0

File tree

7 files changed

+363
-148
lines changed

7 files changed

+363
-148
lines changed

.pre-commit-config.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
exclude: "docs|.git|.tox|build|dist"
2+
default_stages: [ pre-commit ]
3+
fail_fast: true
4+
5+
repos:
6+
- repo: https://github.com/charliermarsh/ruff-pre-commit
7+
rev: "v0.11.13"
8+
hooks:
9+
- id: ruff-format
10+
- id: ruff-check
11+

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ tests = [
3939
docs = [
4040
"Sphinx==7.0.1",
4141
]
42+
dev = [
43+
"pre-commit>=4.2.0",
44+
]
4245

4346
[project.urls]
4447
Homepage = "https://github.com/bennylope/pygeocodio"

src/geocodio/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
from geocodio.data import Address # noqa
1010
from geocodio.data import Location # noqa
1111
from geocodio.data import LocationCollection # noqa
12-
from geocodio.data import LocationCollectionDict # noqa
12+
from geocodio.data import LocationCollectionDict # noqa
1313

14-
__all__ = [GeocodioClient, Address, Location, LocationCollection, LocationCollectionDict]
14+
__all__ = [
15+
GeocodioClient,
16+
Address,
17+
Location,
18+
LocationCollection,
19+
LocationCollectionDict,
20+
]

src/geocodio/client.py

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
logger = logging.getLogger(__name__)
1111

12-
DEFAULT_API_VERSION = "1.6"
12+
DEFAULT_API_VERSION = "1.9"
1313

1414

1515
def error_response(response):
@@ -59,23 +59,51 @@ class GeocodioClient(object):
5959
Client connection for Geocod.io API
6060
"""
6161

62-
def __init__(self, key, order="lat", version=None, hipaa_enabled=False, auto_load_api_version=True, timeout=None, custom_base_domain=None):
63-
"""
62+
def __init__(
63+
self,
64+
key,
65+
order="lat",
66+
version=None,
67+
hipaa_enabled=False,
68+
auto_load_api_version=False,
69+
timeout=None,
70+
custom_base_domain=None,
71+
):
72+
"""Initialize and configure the client.
73+
74+
Args:
75+
key: Geocodio API key
76+
order: one of `lat` or `lng` to determine the ordering
77+
of latitude and longitude. The default is `lat`
78+
(lat, lng); using `lng` (lng, lat) uses a PostGIS
79+
compatible ordering
80+
version: the Geocodio API version
81+
hipaa_enabled: whether to use HIPAA API (if you don't know,
82+
then you don't need it!)
83+
auto_load_api_version: whether to automatically select the
84+
latest API version from the Geocodio API. This *may*
85+
result in errors, i.e. during a new API version
86+
rollout.
87+
timeout: request timeout
88+
custom_base_domain: custom API domain
89+
6490
"""
6591
if custom_base_domain is None:
6692
self.hipaa_enabled = hipaa_enabled
6793
self.BASE_DOMAIN = "https://api{hipaa_append}.geocod.io".format(
68-
hipaa_append=('-hipaa' if self.hipaa_enabled else ''))
94+
hipaa_append=("-hipaa" if self.hipaa_enabled else "")
95+
)
6996
else:
7097
self.BASE_DOMAIN = custom_base_domain
71-
7298

7399
if version is None and auto_load_api_version:
74100
version = self._parse_curr_api_version(self.BASE_DOMAIN)
75101
# Fall back to manual default API version if couldn't be found or isn't overridden
76102
self.version = version or DEFAULT_API_VERSION
77103

78-
self.BASE_URL = "{domain}/v{version}/{{verb}}".format(domain=self.BASE_DOMAIN, version=self.version)
104+
self.BASE_URL = "{domain}/v{version}/{{verb}}".format(
105+
domain=self.BASE_DOMAIN, version=self.version
106+
)
79107
self.API_KEY = key
80108
if order not in ("lat", "lng"):
81109
raise ValueError("Order but be either `lat` or `lng`")
@@ -105,7 +133,11 @@ def _req(self, method="get", verb=None, headers={}, params={}, data={}):
105133
request_headers.update(headers)
106134
request_params.update(params)
107135
return getattr(requests, method)(
108-
url, params=request_params, headers=request_headers, data=data, timeout=self.timeout
136+
url,
137+
params=request_params,
138+
headers=request_headers,
139+
data=data,
140+
timeout=self.timeout,
109141
)
110142

111143
def parse(self, address):
@@ -237,13 +269,19 @@ def geocode(self, address_data=None, components_data=None, **kwargs):
237269
use_components = components_data is not None and address_data is None
238270
param_data = components_data if use_components else address_data
239271

240-
use_batch = isinstance(param_data, list) or (not use_components and isinstance(param_data, dict)) or (
241-
use_components and isinstance(param_data, dict) and all(
242-
isinstance(c, dict) for c in param_data.values()))
272+
use_batch = (
273+
isinstance(param_data, list)
274+
or (not use_components and isinstance(param_data, dict))
275+
or (
276+
use_components
277+
and isinstance(param_data, dict)
278+
and all(isinstance(c, dict) for c in param_data.values())
279+
)
280+
)
243281
if use_batch:
244282
return self.batch_geocode(param_data, **kwargs)
245283
else:
246-
param_key = 'components' if use_components else 'address'
284+
param_key = "components" if use_components else "address"
247285
kwargs.update({param_key: param_data})
248286
return self.geocode_address(**kwargs)
249287

src/geocodio/data.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ def __init__(self, results_list, order="lat"):
120120
for index, result in enumerate(results_list):
121121
results.append(Location(result["response"], order=order))
122122
orig_query = result["query"]
123-
lookup_key = json.dumps(orig_query) if isinstance(orig_query, dict) else orig_query
123+
lookup_key = (
124+
json.dumps(orig_query) if isinstance(orig_query, dict) else orig_query
125+
)
124126
lookups[lookup_key] = index
125127

126128
super().__init__(results)
@@ -135,7 +137,9 @@ def __getitem__(self, item):
135137
try:
136138
ind = self.lookups[key]
137139
except KeyError as e:
138-
raise IndexError("Invalid Index From Lookup For Location Collection") from e
140+
raise IndexError(
141+
"Invalid Index From Lookup For Location Collection"
142+
) from e
139143
return super().__getitem__(ind)
140144

141145
def get(self, key, default=None):
@@ -178,7 +182,9 @@ def __init__(self, results_list, order="lat"):
178182
for key, result in results_list.items():
179183
results[key] = Location(result["response"], order=order)
180184
orig_query = result["query"]
181-
lookup_key = json.dumps(orig_query) if isinstance(orig_query, dict) else orig_query
185+
lookup_key = (
186+
json.dumps(orig_query) if isinstance(orig_query, dict) else orig_query
187+
)
182188
lookups[lookup_key] = key
183189

184190
super().__init__(results)
@@ -187,7 +193,9 @@ def __init__(self, results_list, order="lat"):
187193

188194
def __contains__(self, value):
189195
key = LocationCollectionUtils.get_lookup_key(value)
190-
return super().__contains__(key) or (key in self.lookups and super().__contains__(self.lookups[key]))
196+
return super().__contains__(key) or (
197+
key in self.lookups and super().__contains__(self.lookups[key])
198+
)
191199

192200
def __getitem__(self, item):
193201
key = LocationCollectionUtils.get_lookup_key(item)
@@ -209,11 +217,11 @@ def coords(self):
209217
"""
210218
Returns a dict of tuples for the best matched coordinates.
211219
"""
212-
return {k: l.coords for k, l in self.items()}
220+
return {k: v.coords for k, v in self.items()}
213221

214222
@property
215223
def formatted_addresses(self):
216224
"""
217225
Returns a dict of formatted addresses from the Location list
218226
"""
219-
return {k: l.formatted_address for k, l in self.items()}
227+
return {k: v.formatted_address for k, v in self.items()}

0 commit comments

Comments
 (0)