Performs HTTP GET/POST with credentials, returning the body as JSON. :param url: URL path for the request. Should begin with a slash. :type url: string :param params: HTTP GET parameters. :type params: dict or list of key/value tuples :param first_r
(self, url, params, first_request_time=None, retry_counter=0,
base_url=None, accepts_clientid=True,
extract_body=None, requests_kwargs=None, post_json=None)
| 237 | self.requests_kwargs["headers"] = headers |
| 238 | |
| 239 | def _request(self, url, params, first_request_time=None, retry_counter=0, |
| 240 | base_url=None, accepts_clientid=True, |
| 241 | extract_body=None, requests_kwargs=None, post_json=None): |
| 242 | """Performs HTTP GET/POST with credentials, returning the body as |
| 243 | JSON. |
| 244 | |
| 245 | :param url: URL path for the request. Should begin with a slash. |
| 246 | :type url: string |
| 247 | |
| 248 | :param params: HTTP GET parameters. |
| 249 | :type params: dict or list of key/value tuples |
| 250 | |
| 251 | :param first_request_time: The time of the first request (None if no |
| 252 | retries have occurred). |
| 253 | :type first_request_time: datetime.datetime |
| 254 | |
| 255 | :param retry_counter: The number of this retry, or zero for first attempt. |
| 256 | :type retry_counter: int |
| 257 | |
| 258 | :param base_url: The base URL for the request. Defaults to the Maps API |
| 259 | server. Should not have a trailing slash. |
| 260 | :type base_url: string |
| 261 | |
| 262 | :param accepts_clientid: Whether this call supports the client/signature |
| 263 | params. Some APIs require API keys (e.g. Roads). |
| 264 | :type accepts_clientid: bool |
| 265 | |
| 266 | :param extract_body: A function that extracts the body from the request. |
| 267 | If the request was not successful, the function should raise a |
| 268 | googlemaps.HTTPError or googlemaps.ApiError as appropriate. |
| 269 | :type extract_body: function |
| 270 | |
| 271 | :param requests_kwargs: Same extra keywords arg for requests as per |
| 272 | __init__, but provided here to allow overriding internally on a |
| 273 | per-request basis. |
| 274 | :type requests_kwargs: dict |
| 275 | |
| 276 | :raises ApiError: when the API returns an error. |
| 277 | :raises Timeout: if the request timed out. |
| 278 | :raises TransportError: when something went wrong while trying to |
| 279 | exceute a request. |
| 280 | """ |
| 281 | |
| 282 | if base_url is None: |
| 283 | base_url = self.base_url |
| 284 | |
| 285 | if not first_request_time: |
| 286 | first_request_time = datetime.now() |
| 287 | |
| 288 | elapsed = datetime.now() - first_request_time |
| 289 | if elapsed > self.retry_timeout: |
| 290 | raise googlemaps.exceptions.Timeout() |
| 291 | |
| 292 | if retry_counter > 0: |
| 293 | # 0.5 * (1.5 ^ i) is an increased sleep time of 1.5x per iteration, |
| 294 | # starting at 0.5s when retry_counter=0. The first retry will occur |
| 295 | # at 1, so subtract that first. |
| 296 | delay_seconds = 0.5 * 1.5 ** (retry_counter - 1) |