|
9 | 9 |
|
10 | 10 | logger = logging.getLogger(__name__)
|
11 | 11 |
|
12 |
| -DEFAULT_API_VERSION = "1.6" |
| 12 | +DEFAULT_API_VERSION = "1.9" |
13 | 13 |
|
14 | 14 |
|
15 | 15 | def error_response(response):
|
@@ -59,23 +59,51 @@ class GeocodioClient(object):
|
59 | 59 | Client connection for Geocod.io API
|
60 | 60 | """
|
61 | 61 |
|
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 | +
|
64 | 90 | """
|
65 | 91 | if custom_base_domain is None:
|
66 | 92 | self.hipaa_enabled = hipaa_enabled
|
67 | 93 | 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 | + ) |
69 | 96 | else:
|
70 | 97 | self.BASE_DOMAIN = custom_base_domain
|
71 |
| - |
72 | 98 |
|
73 | 99 | if version is None and auto_load_api_version:
|
74 | 100 | version = self._parse_curr_api_version(self.BASE_DOMAIN)
|
75 | 101 | # Fall back to manual default API version if couldn't be found or isn't overridden
|
76 | 102 | self.version = version or DEFAULT_API_VERSION
|
77 | 103 |
|
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 | + ) |
79 | 107 | self.API_KEY = key
|
80 | 108 | if order not in ("lat", "lng"):
|
81 | 109 | raise ValueError("Order but be either `lat` or `lng`")
|
@@ -105,7 +133,11 @@ def _req(self, method="get", verb=None, headers={}, params={}, data={}):
|
105 | 133 | request_headers.update(headers)
|
106 | 134 | request_params.update(params)
|
107 | 135 | 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, |
109 | 141 | )
|
110 | 142 |
|
111 | 143 | def parse(self, address):
|
@@ -237,13 +269,19 @@ def geocode(self, address_data=None, components_data=None, **kwargs):
|
237 | 269 | use_components = components_data is not None and address_data is None
|
238 | 270 | param_data = components_data if use_components else address_data
|
239 | 271 |
|
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 | + ) |
243 | 281 | if use_batch:
|
244 | 282 | return self.batch_geocode(param_data, **kwargs)
|
245 | 283 | else:
|
246 |
| - param_key = 'components' if use_components else 'address' |
| 284 | + param_key = "components" if use_components else "address" |
247 | 285 | kwargs.update({param_key: param_data})
|
248 | 286 | return self.geocode_address(**kwargs)
|
249 | 287 |
|
|
0 commit comments