Serialize batch request, send to server, process response. Args: http: httplib2.Http, an http object to be used to make the request with. order: list, list of request ids in the order they were added to the batch. requests: list, list of request obj
(self, http, order, requests)
| 1453 | self._order.append(request_id) |
| 1454 | |
| 1455 | def _execute(self, http, order, requests): |
| 1456 | """Serialize batch request, send to server, process response. |
| 1457 | |
| 1458 | Args: |
| 1459 | http: httplib2.Http, an http object to be used to make the request with. |
| 1460 | order: list, list of request ids in the order they were added to the |
| 1461 | batch. |
| 1462 | requests: list, list of request objects to send. |
| 1463 | |
| 1464 | Raises: |
| 1465 | httplib2.HttpLib2Error if a transport error has occurred. |
| 1466 | googleapiclient.errors.BatchError if the response is the wrong format. |
| 1467 | """ |
| 1468 | message = MIMEMultipart("mixed") |
| 1469 | # Message should not write out it's own headers. |
| 1470 | setattr(message, "_write_headers", lambda self: None) |
| 1471 | |
| 1472 | # Add all the individual requests. |
| 1473 | for request_id in order: |
| 1474 | request = requests[request_id] |
| 1475 | |
| 1476 | msg = MIMENonMultipart("application", "http") |
| 1477 | msg["Content-Transfer-Encoding"] = "binary" |
| 1478 | msg["Content-ID"] = self._id_to_header(request_id) |
| 1479 | |
| 1480 | body = self._serialize_request(request) |
| 1481 | msg.set_payload(body) |
| 1482 | message.attach(msg) |
| 1483 | |
| 1484 | # encode the body: note that we can't use `as_string`, because |
| 1485 | # it plays games with `From ` lines. |
| 1486 | fp = io.StringIO() |
| 1487 | g = Generator(fp, mangle_from_=False) |
| 1488 | g.flatten(message, unixfrom=False) |
| 1489 | body = fp.getvalue() |
| 1490 | |
| 1491 | headers = {} |
| 1492 | headers["content-type"] = ( |
| 1493 | "multipart/mixed; " 'boundary="%s"' |
| 1494 | ) % message.get_boundary() |
| 1495 | |
| 1496 | resp, content = http.request( |
| 1497 | self._batch_uri, method="POST", body=body, headers=headers |
| 1498 | ) |
| 1499 | |
| 1500 | if resp.status >= 300: |
| 1501 | raise HttpError(resp, content, uri=self._batch_uri) |
| 1502 | |
| 1503 | # Prepend with a content-type header so FeedParser can handle it. |
| 1504 | header = "content-type: %s\r\n\r\n" % resp["content-type"] |
| 1505 | # PY3's FeedParser only accepts unicode. So we should decode content |
| 1506 | # here, and encode each payload again. |
| 1507 | content = content.decode("utf-8") |
| 1508 | for_parser = header + content |
| 1509 | |
| 1510 | parser = FeedParser() |
| 1511 | parser.feed(for_parser) |
| 1512 | mime_response = parser.close() |
no test coverage detected