mygeocode 1.1.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mygeocode-1.1.1/.gitignore +10 -0
- mygeocode-1.1.1/LICENSE +21 -0
- mygeocode-1.1.1/PKG-INFO +563 -0
- mygeocode-1.1.1/README.md +529 -0
- mygeocode-1.1.1/examples/async_example.py +21 -0
- mygeocode-1.1.1/examples/quickstart.py +14 -0
- mygeocode-1.1.1/examples/smoke.py +31 -0
- mygeocode-1.1.1/pyproject.toml +52 -0
- mygeocode-1.1.1/src/mygeocode/__init__.py +62 -0
- mygeocode-1.1.1/src/mygeocode/_core.py +286 -0
- mygeocode-1.1.1/src/mygeocode/_endpoints.py +69 -0
- mygeocode-1.1.1/src/mygeocode/_version.py +1 -0
- mygeocode-1.1.1/src/mygeocode/async_client.py +415 -0
- mygeocode-1.1.1/src/mygeocode/client.py +404 -0
- mygeocode-1.1.1/src/mygeocode/errors.py +48 -0
- mygeocode-1.1.1/src/mygeocode/models.py +455 -0
- mygeocode-1.1.1/src/mygeocode/py.typed +0 -0
- mygeocode-1.1.1/tests/fixtures/autocomplete.json +66 -0
- mygeocode-1.1.1/tests/fixtures/elevation.json +31 -0
- mygeocode-1.1.1/tests/fixtures/elevation_batch.json +55 -0
- mygeocode-1.1.1/tests/fixtures/error_invalid_request.json +27 -0
- mygeocode-1.1.1/tests/fixtures/forward.json +169 -0
- mygeocode-1.1.1/tests/fixtures/forward_batch.json +351 -0
- mygeocode-1.1.1/tests/fixtures/forward_structured.json +147 -0
- mygeocode-1.1.1/tests/fixtures/ip.json +74 -0
- mygeocode-1.1.1/tests/fixtures/ip_batch.json +135 -0
- mygeocode-1.1.1/tests/fixtures/ipv4.json +74 -0
- mygeocode-1.1.1/tests/fixtures/ipv4_batch.json +186 -0
- mygeocode-1.1.1/tests/fixtures/ipv6.json +74 -0
- mygeocode-1.1.1/tests/fixtures/ipv6_batch.json +135 -0
- mygeocode-1.1.1/tests/fixtures/postcode.json +37 -0
- mygeocode-1.1.1/tests/fixtures/reverse.json +68 -0
- mygeocode-1.1.1/tests/fixtures/reverse_batch.json +188 -0
- mygeocode-1.1.1/tests/fixtures/timezone.json +39 -0
- mygeocode-1.1.1/tests/fixtures/timezone_batch.json +97 -0
- mygeocode-1.1.1/tests/fixtures/timezone_tz.json +44 -0
- mygeocode-1.1.1/tests/helpers.py +129 -0
- mygeocode-1.1.1/tests/run.py +30 -0
- mygeocode-1.1.1/tests/test_async.py +87 -0
- mygeocode-1.1.1/tests/test_client.py +204 -0
- mygeocode-1.1.1/tests/test_endpoints.py +194 -0
- mygeocode-1.1.1/tests/test_null_fields.py +95 -0
- mygeocode-1.1.1/tests/test_parameters.py +127 -0
- mygeocode-1.1.1/tests/test_transport.py +130 -0
mygeocode-1.1.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 My Geocode
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
mygeocode-1.1.1/PKG-INFO
ADDED
|
@@ -0,0 +1,563 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: mygeocode
|
|
3
|
+
Version: 1.1.1
|
|
4
|
+
Summary: Official Python client for the My Geocode API: geocoding, reverse geocoding, address autocomplete, IP, timezone, elevation and postal code lookups.
|
|
5
|
+
Project-URL: Homepage, https://www.mygeocode.com/sdks/
|
|
6
|
+
Project-URL: Documentation, https://www.mygeocode.com/docs/
|
|
7
|
+
Project-URL: Repository, https://github.com/mygeocode/mygeocode-python
|
|
8
|
+
Project-URL: Issues, https://github.com/mygeocode/mygeocode-python/issues
|
|
9
|
+
Author-email: My Geocode <support@mygeocode.com>
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: autocomplete,elevation,geocode,geocoding,ip-geolocation,mygeocode,postcode,reverse-geocoding,timezone
|
|
13
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
14
|
+
Classifier: Framework :: AsyncIO
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.6
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
25
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
26
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
27
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
28
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
29
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
30
|
+
Classifier: Topic :: Scientific/Engineering :: GIS
|
|
31
|
+
Classifier: Typing :: Typed
|
|
32
|
+
Requires-Python: >=3.6
|
|
33
|
+
Description-Content-Type: text/markdown
|
|
34
|
+
|
|
35
|
+
# My Geocode Python SDK
|
|
36
|
+
|
|
37
|
+
The official Python client for the [My Geocode API](https://www.mygeocode.com/): forward and reverse geocoding,
|
|
38
|
+
address autocomplete, IPv4 and IPv6 lookup, timezones, elevation and postal codes.
|
|
39
|
+
|
|
40
|
+

|
|
41
|
+

|
|
42
|
+

|
|
43
|
+
|
|
44
|
+
- Works on every Python from 3.6 to 3.15, so it also runs on the Python 3.6 that ships with RHEL 8, Rocky and Alma
|
|
45
|
+
Linux 8, and on Ubuntu 18.04 and 20.04. No warnings on any of them.
|
|
46
|
+
- No dependencies: only the standard library.
|
|
47
|
+
- A normal client and an asyncio client with the same methods.
|
|
48
|
+
- Safe to drop into any project: it changes no global state (logging, warnings, socket defaults, SSL settings).
|
|
49
|
+
- 2,500 free requests a day without a key or an account.
|
|
50
|
+
|
|
51
|
+
## Install
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
pip install mygeocode
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Quick start (no key)
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
from mygeocode import Client
|
|
61
|
+
|
|
62
|
+
client = Client()
|
|
63
|
+
|
|
64
|
+
found = client.forward("Brandenburg Gate, Berlin")
|
|
65
|
+
place = found.results[0]
|
|
66
|
+
print(place.formatted) # Brandenburg Gate, State of Berlin, Germany
|
|
67
|
+
print(place.lat, place.lon) # 52.516268 13.377692
|
|
68
|
+
|
|
69
|
+
address = client.reverse(48.8584, 2.2945)
|
|
70
|
+
print(address.result.formatted) # 5 Avenue Anatole France, 75007 Paris, France
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Without a key, each network gets 2,500 free requests a day. `found.meta.quota_free_remaining` tells you how many are
|
|
74
|
+
left.
|
|
75
|
+
|
|
76
|
+
## Using a key
|
|
77
|
+
|
|
78
|
+
A key adds your account's prepaid credit or Unlimited package on top of the free requests. Create one in the
|
|
79
|
+
[dashboard](https://www.mygeocode.com/dashboard).
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
client = Client("YOUR_KEY")
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
If you leave out the key, the client reads the `MYGEOCODE_KEY` environment variable. Pass `Client("")` to make sure no
|
|
86
|
+
key is sent. The key always travels in the `X-API-Key` header, never in the URL, and the SDK never writes it anywhere.
|
|
87
|
+
|
|
88
|
+
## Language
|
|
89
|
+
|
|
90
|
+
Results are in English unless you set `lang`. Set it once on the client, or per call:
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
client = Client(lang="de")
|
|
94
|
+
|
|
95
|
+
client.reverse(48.8584, 2.2945, lang="fr") # this call only
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
`lang` takes an ISO 639-1 code such as `de`, `ja` or `pt-BR`. The whole address comes back in that language and
|
|
99
|
+
script. On its own, the API answers each result in the language of its area; the SDK sends `lang=en` by default so
|
|
100
|
+
that every result reads the same.
|
|
101
|
+
|
|
102
|
+
## Methods
|
|
103
|
+
|
|
104
|
+
Every method takes its main arguments first, then keyword options named after the API's own parameters. Every method
|
|
105
|
+
also accepts `params`, a dict of extra query parameters that are sent as they are (and win over the named options),
|
|
106
|
+
and `pretty=True` for indented JSON. A keyword the method does not know raises a `TypeError`, so typos show up
|
|
107
|
+
straight away.
|
|
108
|
+
|
|
109
|
+
| Method | Endpoint |
|
|
110
|
+
|---|---|
|
|
111
|
+
| [`forward`](#forward) | `GET /v1/forward` |
|
|
112
|
+
| [`reverse`](#reverse) | `GET /v1/reverse` |
|
|
113
|
+
| [`autocomplete`](#autocomplete) | `GET /v1/autocomplete` |
|
|
114
|
+
| [`ip`, `ipv4`, `ipv6`](#ip-ipv4-ipv6) | `GET /v1/ip`, `/v1/ipv4`, `/v1/ipv6` |
|
|
115
|
+
| [`timezone`](#timezone) | `GET /v1/timezone` |
|
|
116
|
+
| [`elevation`](#elevation) | `GET /v1/elevation` |
|
|
117
|
+
| [`postcode`](#postcode) | `GET /v1/postcode` |
|
|
118
|
+
| [`forward_batch`](#forward_batch) | `POST /v1/forward` |
|
|
119
|
+
| [`reverse_batch`](#reverse_batch) | `POST /v1/reverse` |
|
|
120
|
+
| [`ip_batch`, `ipv4_batch`, `ipv6_batch`](#ip_batch-ipv4_batch-ipv6_batch) | `GET /v1/ip`, `/v1/ipv4`, `/v1/ipv6` with `ips` |
|
|
121
|
+
| [`timezone_batch`](#timezone_batch) | `GET /v1/timezone` with `locations` |
|
|
122
|
+
| [`elevation_batch`](#elevation_batch) | `POST /v1/elevation` |
|
|
123
|
+
|
|
124
|
+
### forward
|
|
125
|
+
|
|
126
|
+
Text, structured address fields or a place id to coordinates. Returns a `ForwardResponse` whose `results` holds up to
|
|
127
|
+
`limit` `Place` objects, best first. An empty list means nothing matched.
|
|
128
|
+
|
|
129
|
+
```python
|
|
130
|
+
found = client.forward("Brandenburg Gate, Berlin")
|
|
131
|
+
print(found.results[0].formatted) # Brandenburg Gate, State of Berlin, Germany
|
|
132
|
+
|
|
133
|
+
client.forward("Brandenburg Gate, Berlin", limit=3, countries=["de"])
|
|
134
|
+
client.forward(street="10 Downing Street", city="London", country="GB")
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
| Option | Type | What it does |
|
|
138
|
+
|---|---|---|
|
|
139
|
+
| `street`, `city`, `state`, `postcode`, `country` | str | Structured search instead of (or as well as) free text. |
|
|
140
|
+
| `place_id` | str | Look up a `place_id` from an earlier result. |
|
|
141
|
+
| `limit` | int | 1 to 10 results. |
|
|
142
|
+
| `countries` | str or list | ISO 3166-1 alpha-2 codes to search in, such as `["de", "at"]`. |
|
|
143
|
+
| `bounds` | str or list | `south,west,north,east` box that results should fall in. |
|
|
144
|
+
| `strict` | bool | Only return results inside `bounds`. |
|
|
145
|
+
| `proximity` | str or list | `lat,lon` to prefer results near. |
|
|
146
|
+
| `types` | str or list | Kinds of result to keep, such as `address` or `poi`. |
|
|
147
|
+
| `lang` | str | Language of the result. |
|
|
148
|
+
| `timezone`, `elevation`, `wikidata`, `country_info`, `codes`, `sun`, `regions`, `districts`, `census`, `demographics`, `road`, `site` | bool | Each adds its extra block to every result. |
|
|
149
|
+
| `annotations` | bool | Adds every extra block at once. |
|
|
150
|
+
|
|
151
|
+
### reverse
|
|
152
|
+
|
|
153
|
+
A point to the nearest address. Returns a `ReverseResponse` whose `result` is a `Place`, or `None` when nothing is in
|
|
154
|
+
range.
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
address = client.reverse(48.8584, 2.2945)
|
|
158
|
+
print(address.result.formatted) # 5 Avenue Anatole France, 75007 Paris, France
|
|
159
|
+
print(address.result.distance_m) # 47 (metres from the point you gave)
|
|
160
|
+
|
|
161
|
+
client.reverse(48.8584, 2.2945, level="city", timezone=True)
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
| Option | Type | What it does |
|
|
165
|
+
|---|---|---|
|
|
166
|
+
| `level` | str | `address`, `street`, `postcode`, `city`, `region` or `country`. |
|
|
167
|
+
| `radius` | int | Search radius in metres, 1 to 5000. |
|
|
168
|
+
| `lang` | str | Language of the result. |
|
|
169
|
+
| `boundaries` | bool | Add the administrative boundaries the point falls in (Australia). |
|
|
170
|
+
| `timezone`, `elevation`, `wikidata`, `country_info`, `codes`, `sun`, `regions`, `districts`, `census`, `demographics`, `road`, `site` | bool | Each adds its extra block, as for `forward`. |
|
|
171
|
+
| `annotations` | bool | Adds every extra block at once. |
|
|
172
|
+
|
|
173
|
+
### autocomplete
|
|
174
|
+
|
|
175
|
+
Suggestions while someone types an address. Returns an `AutocompleteResponse` with a `suggestions` list; each
|
|
176
|
+
`Suggestion` has `text`, `lat`, `lon`, `type`, `precision` and `place_id`.
|
|
177
|
+
|
|
178
|
+
```python
|
|
179
|
+
typed = client.autocomplete("1600 Pennsylvania")
|
|
180
|
+
print(typed.suggestions[0].text) # 1600 Pennsylvania Avenue NW, Washington, DC 20500, United States
|
|
181
|
+
|
|
182
|
+
client.autocomplete("1600 Pennsylvania", limit=5, countries="us")
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
Options: `limit`, `countries`, `proximity`, `bounds`, `strict`, `types` and `lang`, as for `forward`.
|
|
186
|
+
|
|
187
|
+
### ip, ipv4, ipv6
|
|
188
|
+
|
|
189
|
+
Country, region, city, coordinates, timezone, network owner (ASN) and proxy, VPN and data centre flags for an IP
|
|
190
|
+
address. `ip` takes either version; `ipv4` and `ipv6` accept only their own. Without an address you get the address the
|
|
191
|
+
request came from. Returns an `IpInfo`.
|
|
192
|
+
|
|
193
|
+
```python
|
|
194
|
+
google = client.ip("8.8.8.8")
|
|
195
|
+
print(google.country, google.city) # United States Mountain View
|
|
196
|
+
print(google.asn, google.as_name) # 15169 Google LLC
|
|
197
|
+
print(google.is_datacenter) # True
|
|
198
|
+
|
|
199
|
+
client.ipv6("2001:4860:4860::8888")
|
|
200
|
+
client.ip() # the address the request came from
|
|
201
|
+
client.ipv4("8.8.8.8", elevation=True) # adds elevation_m
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
Option: `elevation` (bool). The full list of `IpInfo` fields is under [Results and meta](#results-and-meta).
|
|
205
|
+
|
|
206
|
+
### timezone
|
|
207
|
+
|
|
208
|
+
The IANA zone, UTC offset, daylight saving state and local time for a point, now or at a given moment. Returns a
|
|
209
|
+
`TimezoneInfo`.
|
|
210
|
+
|
|
211
|
+
```python
|
|
212
|
+
tokyo = client.timezone(35.6762, 139.6503)
|
|
213
|
+
print(tokyo.timezone, tokyo.utc_offset) # Asia/Tokyo +09:00
|
|
214
|
+
print(tokyo.name, tokyo.abbreviation) # Japan Standard Time JST
|
|
215
|
+
print(tokyo.local_time) # 2026-11-24T18:07:15+09:00
|
|
216
|
+
|
|
217
|
+
client.timezone(35.6762, 139.6503, timestamp=1767225600)
|
|
218
|
+
client.timezone(tz="Europe/Paris")
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
| Option | Type | What it does |
|
|
222
|
+
|---|---|---|
|
|
223
|
+
| `tz` | str | Look up a zone by its IANA or Windows id instead of a point (leave out lat and lon). |
|
|
224
|
+
| `timestamp` | int | Unix time to answer for. Default: now. |
|
|
225
|
+
| `lang` | str | Language of the zone name. Sent as the API's `language` parameter, only when you set it here or on the client. `language` works as another name for this option. |
|
|
226
|
+
| `locations` | list or str | Several points: `[(lat, lon), ...]`, `"lat,lon\|lat,lon"` or an `enc:` polyline. |
|
|
227
|
+
| `path`, `samples` | list or str, int | Points along a path, sampled `samples` times (2 to 100). |
|
|
228
|
+
| `bounds`, `rows`, `cols` | str or list, int, int | A grid of points over a box. |
|
|
229
|
+
|
|
230
|
+
For a plain list of points, `timezone_batch` is simpler.
|
|
231
|
+
|
|
232
|
+
### elevation
|
|
233
|
+
|
|
234
|
+
Height in metres above sea level (negative for the sea floor), from a global 15 arc-second grid. Returns an
|
|
235
|
+
`ElevationResponse` whose `results` holds `ElevationPoint` objects with `lat`, `lon`, `elevation_m` and `resolution_m`.
|
|
236
|
+
|
|
237
|
+
```python
|
|
238
|
+
everest = client.elevation(27.9881, 86.9250)
|
|
239
|
+
print(everest.results[0].elevation_m) # 8627.0
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
| Option | Type | What it does |
|
|
243
|
+
|---|---|---|
|
|
244
|
+
| `locations` | list or str | Several points (leave out lat and lon). |
|
|
245
|
+
| `path`, `samples` | list or str, int | Heights along a path. |
|
|
246
|
+
| `bounds`, `rows`, `cols` | str or list, int, int | Heights over a grid. |
|
|
247
|
+
|
|
248
|
+
### postcode
|
|
249
|
+
|
|
250
|
+
A postal code to its place, region and coordinates. Returns a `PostcodeResponse` whose `results` lists every place that
|
|
251
|
+
uses the code.
|
|
252
|
+
|
|
253
|
+
```python
|
|
254
|
+
nyc = client.postcode("10001", country="US")
|
|
255
|
+
first = nyc.results[0]
|
|
256
|
+
print(first.place, first.region_code) # New York NY
|
|
257
|
+
print(first.lat, first.lon) # 40.748401 -73.996702
|
|
258
|
+
|
|
259
|
+
client.postcode("75008", country="FR")
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
Option: `country` (ISO alpha-2). Without it, the code is matched in every country.
|
|
263
|
+
|
|
264
|
+
### forward_batch
|
|
265
|
+
|
|
266
|
+
Up to 100 searches in one request (each counts as one request against your quota). Returns a `ForwardBatchResponse`
|
|
267
|
+
whose `results` holds one `ForwardBatchEntry` per search, in the order you sent them; each entry has the `query` and
|
|
268
|
+
its own `results` list of `Place` objects.
|
|
269
|
+
|
|
270
|
+
```python
|
|
271
|
+
batch = client.forward_batch(["Brandenburg Gate, Berlin", "Sydney Opera House", "Statue of Liberty, New York"])
|
|
272
|
+
for entry in batch.results:
|
|
273
|
+
print(entry.query + ":", entry.results[0].formatted)
|
|
274
|
+
# Brandenburg Gate, Berlin: Brandenburg Gate, State of Berlin, Germany
|
|
275
|
+
# Sydney Opera House: Sydney Opera House, Sydney, New South Wales, Australia
|
|
276
|
+
# Statue of Liberty, New York: Statue of Liberty, New York, New York, United States
|
|
277
|
+
|
|
278
|
+
client.forward_batch(["Brandenburg Gate, Berlin", "Sydney Opera House"], limit=1, timezone=True)
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
The options apply to every search in the batch:
|
|
282
|
+
|
|
283
|
+
| Option | Type | What it does |
|
|
284
|
+
|---|---|---|
|
|
285
|
+
| `limit` | int | 1 to 10 results per search. |
|
|
286
|
+
| `countries`, `bounds`, `strict`, `proximity`, `types` | as for `forward` | Filters for every search. |
|
|
287
|
+
| `lang` | str | Language of the results. |
|
|
288
|
+
| `timezone`, `elevation`, `wikidata`, `country_info`, `codes`, `sun`, `regions`, `districts`, `census`, `demographics`, `road`, `site` | bool | Each adds its extra block to every result. |
|
|
289
|
+
| `annotations` | bool | Adds every extra block at once. |
|
|
290
|
+
|
|
291
|
+
Structured fields and `place_id` are not available in a batch; use `forward` for those.
|
|
292
|
+
|
|
293
|
+
### reverse_batch
|
|
294
|
+
|
|
295
|
+
Up to 100 points in one request (each counts as one request). Returns a `ReverseBatchResponse` whose `results` holds
|
|
296
|
+
one `ReverseResponse` per point, in the order you sent them; its `result` is a `Place`, or `None` when nothing is in
|
|
297
|
+
range.
|
|
298
|
+
|
|
299
|
+
```python
|
|
300
|
+
batch = client.reverse_batch([(48.8584, 2.2945), (40.6892, -74.0445), (-33.8568, 151.2153)])
|
|
301
|
+
for point in batch.results:
|
|
302
|
+
print(point.result.formatted if point.result else "nothing in range")
|
|
303
|
+
# 5 Avenue Anatole France, 75007 Paris, France
|
|
304
|
+
# 1 Liberty Island, New York, NY 10004, United States
|
|
305
|
+
# 2B Macquarie Street, Sydney NSW 2000, Australia
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
The options apply to every point in the batch:
|
|
309
|
+
|
|
310
|
+
| Option | Type | What it does |
|
|
311
|
+
|---|---|---|
|
|
312
|
+
| `level` | str | `address`, `street`, `postcode`, `city`, `region` or `country`. |
|
|
313
|
+
| `radius` | int | Search radius in metres, 1 to 5000. |
|
|
314
|
+
| `lang` | str | Language of the results. |
|
|
315
|
+
| `boundaries` | bool | Add the administrative boundaries each point falls in (Australia). |
|
|
316
|
+
| `timezone`, `elevation`, `wikidata`, `country_info`, `codes`, `sun`, `regions`, `districts`, `census`, `demographics`, `road`, `site` | bool | Each adds its extra block, as for `forward`. |
|
|
317
|
+
| `annotations` | bool | Adds every extra block at once. |
|
|
318
|
+
|
|
319
|
+
### ip_batch, ipv4_batch, ipv6_batch
|
|
320
|
+
|
|
321
|
+
Up to 100 addresses in one request. `ip_batch` takes both versions mixed; `ipv4_batch` and `ipv6_batch` accept only
|
|
322
|
+
their own. Returns an `IpBatchResponse` whose `results` holds one `IpInfo` per address, in the order you sent them. An
|
|
323
|
+
address the API cannot read gets an entry whose `status` is `error` and whose `error` explains why, while the rest
|
|
324
|
+
still answer.
|
|
325
|
+
|
|
326
|
+
```python
|
|
327
|
+
batch = client.ipv4_batch(["8.8.8.8", "1.1.1.1", "9.9.9.9"])
|
|
328
|
+
for info in batch.results:
|
|
329
|
+
print(info.ip + ":", info.as_name if info.status == "ok" else info.error["message"])
|
|
330
|
+
# 8.8.8.8: Google LLC
|
|
331
|
+
# 1.1.1.1: Cloudflare, Inc.
|
|
332
|
+
# 9.9.9.9: Quad9
|
|
333
|
+
|
|
334
|
+
client.ipv6_batch(["2001:4860:4860::8888", "2606:4700:4700::1111"])
|
|
335
|
+
client.ip_batch(["8.8.8.8", "2606:4700:4700::1111"], elevation=True)
|
|
336
|
+
```
|
|
337
|
+
|
|
338
|
+
Option: `elevation` (bool), adds `elevation_m` to every address.
|
|
339
|
+
|
|
340
|
+
### timezone_batch
|
|
341
|
+
|
|
342
|
+
Timezones for up to 100 points. Returns a `TimezoneBatchResponse` whose `results` holds one `TimezoneInfo` per point,
|
|
343
|
+
in the order you sent them.
|
|
344
|
+
|
|
345
|
+
```python
|
|
346
|
+
batch = client.timezone_batch([(48.8584, 2.2945), (40.6892, -74.0445), (-33.8568, 151.2153)])
|
|
347
|
+
for zone in batch.results:
|
|
348
|
+
print(zone.timezone)
|
|
349
|
+
# Europe/Paris
|
|
350
|
+
# America/New_York
|
|
351
|
+
# Australia/Sydney
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
Options: `timestamp` (Unix time, default now) and `lang` (also accepted as `language`), as for `timezone`.
|
|
355
|
+
|
|
356
|
+
### elevation_batch
|
|
357
|
+
|
|
358
|
+
Heights for up to 100 points, sent as a POST body. Returns an `ElevationResponse`.
|
|
359
|
+
|
|
360
|
+
```python
|
|
361
|
+
batch = client.elevation_batch([(48.8584, 2.2945), (40.6892, -74.0445), (-33.8568, 151.2153)])
|
|
362
|
+
for point in batch.results:
|
|
363
|
+
print(point.elevation_m)
|
|
364
|
+
# 37.0
|
|
365
|
+
# 0.0
|
|
366
|
+
# 10.0
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
## Async client
|
|
370
|
+
|
|
371
|
+
`AsyncClient` has the same methods and options; each one is a coroutine. The request runs in the event loop's thread
|
|
372
|
+
pool, so it never blocks the loop.
|
|
373
|
+
|
|
374
|
+
```python
|
|
375
|
+
import asyncio
|
|
376
|
+
|
|
377
|
+
from mygeocode import AsyncClient
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
async def main():
|
|
381
|
+
async with AsyncClient() as client:
|
|
382
|
+
tokyo, everest = await asyncio.gather(
|
|
383
|
+
client.timezone(35.6762, 139.6503),
|
|
384
|
+
client.elevation(27.9881, 86.9250),
|
|
385
|
+
)
|
|
386
|
+
print(tokyo.timezone, everest.results[0].elevation_m) # Asia/Tokyo 8627.0
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
asyncio.run(main()) # on Python 3.6: asyncio.get_event_loop().run_until_complete(main())
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
## Results and meta
|
|
393
|
+
|
|
394
|
+
Results are plain objects whose attributes carry the API's own field names, so the
|
|
395
|
+
[API documentation](https://www.mygeocode.com/docs/) describes them field by field:
|
|
396
|
+
|
|
397
|
+
```python
|
|
398
|
+
place = client.forward("Brandenburg Gate, Berlin").results[0]
|
|
399
|
+
|
|
400
|
+
place.formatted # "Brandenburg Gate, State of Berlin, Germany"
|
|
401
|
+
place.precision # "house"
|
|
402
|
+
place.components.city # address parts: road, house_number, postcode, city, state, country ...
|
|
403
|
+
place.components.country_code # "de"
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
A field the API did not send is `None`, so check for `None` before you use one. The fields this SDK reads are never
|
|
407
|
+
renamed or removed from the API: if one ever has to go, it keeps coming back with the value `None`, and your code
|
|
408
|
+
keeps working. A list that comes back as `null` reads as an empty list. Every result object also has `raw`, the decoded
|
|
409
|
+
JSON as a dict, so fields added to the API later are readable before the SDK knows them.
|
|
410
|
+
|
|
411
|
+
What each method returns, and the attributes you can read:
|
|
412
|
+
|
|
413
|
+
| Class | Returned by | Attributes |
|
|
414
|
+
|---|---|---|
|
|
415
|
+
| `ForwardResponse` | `forward` | `status`, `query`, `results` (list of `Place`), `meta`, `raw` |
|
|
416
|
+
| `ReverseResponse` | `reverse`, each entry of `reverse_batch` | `status`, `lat`, `lon`, `result` (`Place` or `None`), `boundaries`, `meta`, `raw` |
|
|
417
|
+
| `Place` | inside forward and reverse results | `formatted`, `lat`, `lon`, `type`, `precision`, `confidence`, `place_id`, `components`, `bounds`, `distance_m`, `formatted_place`, `place`, `parcel`, `codes`, `population`, `un_locode`, `timezone`, `elevation_m`, `wikidata`, `country_info`, `sun`, `regions`, `districts`, `census`, `demographics`, `road`, `intersection`, `site`, `raw` |
|
|
418
|
+
| `Components` | `Place.components` | `poi`, `building`, `unit`, `level`, `house_number`, `road`, `lot`, `suburb`, `municipality`, `city`, `county`, `state`, `state_code`, `postcode`, `country`, `country_code`, `official_name`, `raw` |
|
|
419
|
+
| `Bounds` | `Place.bounds` | `north`, `south`, `east`, `west`, `raw` |
|
|
420
|
+
| `AutocompleteResponse` | `autocomplete` | `status`, `query`, `suggestions` (list of `Suggestion`), `meta`, `raw` |
|
|
421
|
+
| `Suggestion` | inside autocomplete results | `text`, `lat`, `lon`, `type`, `precision`, `place_id`, `raw` |
|
|
422
|
+
| `IpInfo` | `ip`, `ipv4`, `ipv6`, each entry of the IP batches | `status`, `ip`, `version`, `found`, `prefix`, `country`, `country_code`, `continent`, `continent_code`, `region`, `region_code`, `city`, `postcode`, `lat`, `lon`, `elevation_m`, `timezone`, `utc_offset`, `utc_offset_seconds`, `is_eu`, `asn`, `as_name`, `org`, `usage_type`, `registry`, `allocated`, `tags`, `is_anycast`, `is_satellite`, `is_datacenter`, `is_proxy`, `is_vpn_provider`, `is_tor`, `crawler`, `is_private`, `reason`, `abuse_email`, `threat`, `updated_at`, `error`, `meta`, `raw` |
|
|
423
|
+
| `TimezoneInfo` | `timezone`, each entry of `timezone_batch` | `status`, `lat`, `lon`, `timezone`, `name`, `abbreviation`, `utc_offset`, `utc_offset_seconds`, `raw_offset_seconds`, `dst`, `dst_offset_seconds`, `timestamp`, `local_time`, `country_code`, `is_ocean`, `windows_timezone`, `next_transition` (`TimezoneTransition`), `meta`, `raw` |
|
|
424
|
+
| `TimezoneTransition` | `TimezoneInfo.next_transition` | `timestamp`, `utc_offset`, `utc_offset_seconds`, `dst`, `abbreviation`, `raw` |
|
|
425
|
+
| `ElevationResponse` | `elevation`, `elevation_batch` | `status`, `results` (list of `ElevationPoint`), `meta`, `raw` |
|
|
426
|
+
| `ElevationPoint` | inside elevation results | `lat`, `lon`, `elevation_m`, `resolution_m`, `raw` |
|
|
427
|
+
| `PostcodeResponse` | `postcode` | `status`, `postcode`, `country_code`, `results` (list of `PostcodePlace`), `meta`, `raw` |
|
|
428
|
+
| `PostcodePlace` | inside postcode results | `postcode`, `place`, `region`, `region_code`, `country`, `country_code`, `lat`, `lon`, `raw` |
|
|
429
|
+
| `ForwardBatchResponse` | `forward_batch` | `status`, `results` (list of `ForwardBatchEntry`), `meta`, `raw` |
|
|
430
|
+
| `ForwardBatchEntry` | inside forward batch results | `query`, `results` (list of `Place`), `raw` |
|
|
431
|
+
| `ReverseBatchResponse` | `reverse_batch` | `status`, `results` (list of `ReverseResponse`), `meta`, `raw` |
|
|
432
|
+
| `IpBatchResponse` | `ip_batch`, `ipv4_batch`, `ipv6_batch` | `status`, `results` (list of `IpInfo`), `meta`, `raw` |
|
|
433
|
+
| `TimezoneBatchResponse` | `timezone_batch` | `status`, `timestamp`, `results` (list of `TimezoneInfo`), `meta`, `raw` |
|
|
434
|
+
|
|
435
|
+
The extra blocks you ask for with the flags (`timezone`, `elevation`, `wikidata` and the rest) are attributes of
|
|
436
|
+
`Place` and stay plain dicts.
|
|
437
|
+
|
|
438
|
+
Every response object has `meta`, filled from the response headers:
|
|
439
|
+
|
|
440
|
+
| Attribute | Header | Meaning |
|
|
441
|
+
|---|---|---|
|
|
442
|
+
| `request_id` | `X-Request-Id` | Quote it when you contact support. |
|
|
443
|
+
| `quota_limit` | `X-Quota-Limit` | Free requests a day for this key's account or network. |
|
|
444
|
+
| `quota_used` | `X-Quota-Used` | Free requests used today. |
|
|
445
|
+
| `quota_free_remaining` | `X-Quota-Free-Remaining` | Free requests left today. |
|
|
446
|
+
| `quota_reset` | `X-Quota-Reset` | Unix time when the free requests reset. |
|
|
447
|
+
| `credits_remaining` | `X-Credits-Remaining` | Prepaid credit left, when a key with credit is used. |
|
|
448
|
+
|
|
449
|
+
## Errors
|
|
450
|
+
|
|
451
|
+
Everything the SDK raises derives from `mygeocode.MyGeocodeError`:
|
|
452
|
+
|
|
453
|
+
| Class | When |
|
|
454
|
+
|---|---|
|
|
455
|
+
| `ApiError` | The API answered with an error. `status` is the HTTP status, `code` the API's error code (such as `invalid_request`, `invalid_key` or `quota_exceeded`), `message` its explanation, `param` the parameter at fault, `request_id`, `retry_after` (seconds, or `None`) and `body` (the decoded answer). |
|
|
456
|
+
| `RequestTimeoutError` | No answer within `timeout`. |
|
|
457
|
+
| `NetworkError` | The connection failed: DNS, refused, reset or TLS. |
|
|
458
|
+
| `DecodeError` | The answer could not be read as JSON. |
|
|
459
|
+
|
|
460
|
+
```python
|
|
461
|
+
from mygeocode import ApiError, MyGeocodeError
|
|
462
|
+
|
|
463
|
+
try:
|
|
464
|
+
client.forward("Brandenburg Gate, Berlin")
|
|
465
|
+
except ApiError as error:
|
|
466
|
+
print(error.status, error.code, error.message)
|
|
467
|
+
except MyGeocodeError as error:
|
|
468
|
+
print("Could not reach the API:", error)
|
|
469
|
+
```
|
|
470
|
+
|
|
471
|
+
The [errors page](https://www.mygeocode.com/docs/errors/) lists every error code.
|
|
472
|
+
|
|
473
|
+
## Retries
|
|
474
|
+
|
|
475
|
+
The client retries on its own, up to `max_retries` times (default 2):
|
|
476
|
+
|
|
477
|
+
- **429** (too many requests): waits for `Retry-After`, or until `X-Quota-Reset`. If that is longer than
|
|
478
|
+
`max_retry_wait` (default 60 seconds), for example when the day's free requests are gone, it raises the `ApiError`
|
|
479
|
+
straight away instead of waiting.
|
|
480
|
+
- **500**: tries once more after one second.
|
|
481
|
+
- **503**: waits for `Retry-After`, or 1, 2 and then 4 seconds.
|
|
482
|
+
- **Timeouts and connection errors**: retried with the same 1, 2, 4 second pattern for GET requests. The POST batch
|
|
483
|
+
calls are not retried after a connection error, so a batch is never charged twice.
|
|
484
|
+
|
|
485
|
+
Other errors are raised at once.
|
|
486
|
+
|
|
487
|
+
## Configuration
|
|
488
|
+
|
|
489
|
+
All options are optional:
|
|
490
|
+
|
|
491
|
+
```python
|
|
492
|
+
client = Client(
|
|
493
|
+
"YOUR_KEY", # default: MYGEOCODE_KEY, else no key
|
|
494
|
+
base_url="https://api.mygeocode.com/v1",
|
|
495
|
+
timeout=10, # seconds per request
|
|
496
|
+
max_retries=2,
|
|
497
|
+
max_retry_wait=60, # seconds
|
|
498
|
+
lang="en",
|
|
499
|
+
)
|
|
500
|
+
```
|
|
501
|
+
|
|
502
|
+
`transport`, `sleep` and `clock` replace the HTTP layer, the wait and the clock the retry logic uses. They are meant
|
|
503
|
+
for tests, see below. `Client` also works as a context manager (`with Client() as client:`), and `AsyncClient` with
|
|
504
|
+
`async with`.
|
|
505
|
+
|
|
506
|
+
## Testing your code (transport hook)
|
|
507
|
+
|
|
508
|
+
The client sends every request through its `transport`: any callable that takes an `HttpRequest` (`method`, `url`,
|
|
509
|
+
`headers`, `body`, `timeout`) and returns an `HttpResponse`. Give it your own to test code that uses the SDK without
|
|
510
|
+
touching the network:
|
|
511
|
+
|
|
512
|
+
```python
|
|
513
|
+
import json
|
|
514
|
+
|
|
515
|
+
from mygeocode import Client, HttpResponse
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
def canned(request):
|
|
519
|
+
body = {
|
|
520
|
+
"status": "ok",
|
|
521
|
+
"lat": 48.8584,
|
|
522
|
+
"lon": 2.2945,
|
|
523
|
+
"result": {"formatted": "5 Avenue Anatole France, 75007 Paris, France", "lat": 48.858821, "lon": 2.294596},
|
|
524
|
+
}
|
|
525
|
+
return HttpResponse(200, {"Content-Type": "application/json"}, json.dumps(body).encode("utf-8"))
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
client = Client(transport=canned, sleep=lambda seconds: None) # no real waiting in retry tests
|
|
529
|
+
print(client.reverse(48.8584, 2.2945).result.formatted)
|
|
530
|
+
```
|
|
531
|
+
|
|
532
|
+
The same hook plugs in any HTTP library, such as `requests` with your own session and proxy settings. Raise
|
|
533
|
+
`mygeocode.RequestTimeoutError` for a timeout and `mygeocode.NetworkError` for any other failure to get an answer;
|
|
534
|
+
return every HTTP status as an `HttpResponse`.
|
|
535
|
+
|
|
536
|
+
### Running the SDK's own tests
|
|
537
|
+
|
|
538
|
+
The tests need nothing but Python, so the same suite runs on every version from 3.6 up. It answers from real recorded
|
|
539
|
+
API responses and never calls the API:
|
|
540
|
+
|
|
541
|
+
```bash
|
|
542
|
+
python tests/run.py
|
|
543
|
+
```
|
|
544
|
+
|
|
545
|
+
Any warning counts as a failed test.
|
|
546
|
+
|
|
547
|
+
## Requirements
|
|
548
|
+
|
|
549
|
+
- Python 3.6 or newer. Tested on 3.6, 3.7, 3.8, 3.9, 3.10, 3.11, 3.12, 3.13, 3.14, 3.15 and 3.16 (alpha), with every
|
|
550
|
+
warning treated as a failure.
|
|
551
|
+
- Nothing else: only the standard library.
|
|
552
|
+
|
|
553
|
+
## Links
|
|
554
|
+
|
|
555
|
+
- [API documentation](https://www.mygeocode.com/docs/)
|
|
556
|
+
- [Pricing and free tier](https://www.mygeocode.com/pricing/)
|
|
557
|
+
- [Dashboard and API keys](https://www.mygeocode.com/dashboard)
|
|
558
|
+
- [Issues](https://github.com/mygeocode/mygeocode-python/issues)
|
|
559
|
+
- Support: [support@mygeocode.com](mailto:support@mygeocode.com)
|
|
560
|
+
|
|
561
|
+
## Licence
|
|
562
|
+
|
|
563
|
+
MIT. See [LICENSE](LICENSE).
|