Google Maps
maps
¶
Assembly: Mappy
Filename: maps.py
Author: Terry D. Eppler
Created: 05-31-2022
Last Modified By: Terry D. Eppler
Last Modified On: 05-01-2025
Mappy is a python framework encapsulating the Google Maps functionality.
Copyright © 2022 Terry Eppler
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
You can contact me at: terryeppler@gmail.com or eppler.terry@epa.gov
Classes¶
Maps
¶
Maps(
qps: Optional[float] = 5.0,
retries: int = 5,
min: float = 1.0,
max: float = 30.0,
timeout: float = 15.0,
)
Provide the central Google Maps Web Services gateway.
Purpose
Wraps Google Maps HTTP request execution behind a shared gateway used by geocoding, Places, Distance Matrix, Static Maps, Time Zone, spreadsheet enrichment, and Streamlit workflows. The class stores request configuration, applies process-local rate limiting, injects the configured API key, retries transient failures with exponential backoff, and records response diagnostics for downstream inspection.
Attributes:
| Name | Type | Description |
|---|---|---|
base_url |
Optional[str]
|
Base Google Maps API URL. |
endpoint |
Optional[str]
|
Endpoint path used for the active request. |
params |
Optional[Dict[str, str]]
|
Query parameters supplied by the caller before API-key injection. |
api_key |
str
|
Google Maps Platform API key read from configuration. |
query_per_second |
Optional[float]
|
Optional request-rate ceiling. |
retries |
Optional[int]
|
Maximum retry attempts for transient failures. |
min |
Optional[float]
|
Initial retry backoff in seconds. |
max |
Optional[float]
|
Maximum retry backoff in seconds. |
timeout |
Optional[float]
|
HTTP request timeout in seconds. |
url |
Optional[str]
|
Fully qualified request URL for the active endpoint. |
session |
Optional[Session]
|
Requests session used for outbound HTTP calls. |
limiter |
Optional[RateLimiter]
|
Rate limiter invoked before outbound requests. |
query |
Optional[Dict[str, str]]
|
Final query parameters sent with the active request. |
response |
Optional[Response]
|
Most recent HTTP response object. |
last_url |
Optional[str]
|
Most recent request URL. |
last_query |
Optional[Dict[str, str]]
|
Most recent query parameters after API-key injection. |
last_status |
Optional[int]
|
Most recent HTTP status code. |
last_elapsed_ms |
Optional[float]
|
Most recent request duration in milliseconds. |
last_payload_status |
Optional[str]
|
Most recent Google payload status value. |
last_payload_error |
Optional[str]
|
Most recent Google payload error message. |
Initialize the Google Maps gateway.
Purpose
Creates the shared HTTP session, rate limiter, retry settings, timeout settings, and diagnostic fields used by later gateway requests. The constructor reads the Google Maps API key from configuration and prepares the instance for reuse by Mappy service wrappers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
qps
|
Optional[float]
|
Maximum queries per second. |
5.0
|
retries
|
int
|
Maximum number of retry attempts for transient HTTP or Google payload failures. |
5
|
min
|
float
|
Initial exponential-backoff delay in seconds. |
1.0
|
max
|
float
|
Maximum exponential-backoff delay in seconds. |
30.0
|
timeout
|
float
|
HTTP request timeout in seconds. |
15.0
|
Source code in maps.py
Methods:¶
request
¶
Execute a Google Maps API GET request.
Purpose
Builds and sends a request to the selected Google Maps Web Services
endpoint using caller-supplied query parameters plus the configured API
key. The method rate-limits outbound calls, records request and response
diagnostics, retries transient HTTP and Google payload failures with
exponential backoff, normalizes hard Google API status failures into
GatewayError, and returns the parsed JSON payload on success.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint
|
str
|
Endpoint path such as |
required |
params
|
Dict[str, str]
|
Query parameters for the request before API-key injection. |
required |
Returns:
| Type | Description |
|---|---|
Dict | None
|
Dict | None: Parsed JSON response payload on success. |
Raises:
| Type | Description |
|---|---|
Error
|
Raised after the exception is wrapped and written to the application logger. |
Source code in maps.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | |
Functions:¶
throw_if
¶
Validate that a required gateway value is present.
Purpose
Provides a lightweight guard for required Google Maps gateway inputs. The function prevents missing endpoints, empty parameter dictionaries, and blank string values from reaching outbound request construction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the argument being validated. |
required |
value
|
object
|
Runtime value to validate. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
Raised when |