|
1 | 1 | from collections import namedtuple
|
2 | 2 | import json
|
3 |
| -from typing import List |
| 3 | +from typing import Callable, List |
4 | 4 | import smartcar.config as config
|
5 | 5 | import smartcar.helpers as helpers
|
6 | 6 | import smartcar.smartcar
|
@@ -397,6 +397,30 @@ def send_destination(self, latitude, longitude) -> types.Action:
|
397 | 397 | )
|
398 | 398 | return types.select_named_tuple("send_destination", response)
|
399 | 399 |
|
| 400 | + @staticmethod |
| 401 | + def _batch_path_response( |
| 402 | + path: str, path_response: dict, top_response: dict |
| 403 | + ) -> Callable[[], namedtuple]: |
| 404 | + if path_response.get("code") == 200: |
| 405 | + # attach top-level sc-request-id to res_dict |
| 406 | + path_response["headers"]["sc-request-id"] = top_response.headers.get( |
| 407 | + "sc-request-id" |
| 408 | + ) |
| 409 | + # use lambda default args to avoid issues with closures |
| 410 | + return lambda p=path, r=path_response: types.select_named_tuple(p, r) |
| 411 | + |
| 412 | + # if individual response is erroneous, attach a lambda that returns a SmartcarException |
| 413 | + def _attribute_raise_exception(smartcar_exception): |
| 414 | + raise smartcar_exception |
| 415 | + |
| 416 | + path_status_code = path_response.get("code") |
| 417 | + path_headers = path_response.get("headers", {}) |
| 418 | + path_body = json.dumps(path_response.get("body")) |
| 419 | + sc_exception = sce.exception_factory( |
| 420 | + path_status_code, path_headers, path_body, False |
| 421 | + ) |
| 422 | + return lambda e=sc_exception: _attribute_raise_exception(e) |
| 423 | + |
400 | 424 | def batch(self, paths: List[str]) -> namedtuple:
|
401 | 425 | """
|
402 | 426 | POST Vehicle.batch
|
@@ -432,32 +456,13 @@ def batch(self, paths: List[str]) -> namedtuple:
|
432 | 456 | # success of the request.
|
433 | 457 | batch_dict = dict()
|
434 | 458 | path_responses = response.json()["responses"]
|
435 |
| - for res_dict in path_responses: |
| 459 | + for path_response in path_responses: |
436 | 460 | path, attribute = helpers.format_path_and_attribute_for_batch(
|
437 |
| - res_dict["path"] |
| 461 | + path_response["path"] |
| 462 | + ) |
| 463 | + batch_dict[attribute] = Vehicle._batch_path_response( |
| 464 | + path, path_response, response |
438 | 465 | )
|
439 |
| - |
440 |
| - if res_dict.get("code") == 200: |
441 |
| - # attach top-level sc-request-id to res_dict |
442 |
| - res_dict["headers"]["sc-request-id"] = response.headers.get( |
443 |
| - "sc-request-id" |
444 |
| - ) |
445 |
| - # use lambda default args to avoid issues with closures |
446 |
| - batch_dict[attribute] = ( |
447 |
| - lambda p=path, r=res_dict: types.select_named_tuple(p, r) |
448 |
| - ) |
449 |
| - else: |
450 |
| - # if individual response is erroneous, attach a lambda that returns a SmartcarException |
451 |
| - def _attribute_raise_exception(smartcar_exception): |
452 |
| - raise smartcar_exception |
453 |
| - |
454 |
| - code = res_dict.get("code") |
455 |
| - headers = response.headers |
456 |
| - body = json.dumps(res_dict.get("body")) |
457 |
| - sc_exception = sce.exception_factory(code, headers, body) |
458 |
| - batch_dict[attribute] = ( |
459 |
| - lambda e=sc_exception: _attribute_raise_exception(e) |
460 |
| - ) |
461 | 466 |
|
462 | 467 | # STEP 3 - Attach Meta to batch_dict
|
463 | 468 | batch_dict["meta"] = types.build_meta(response.headers)
|
|
0 commit comments