|
| 1 | +# Copyright 2021 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Helpers for server-side streaming in REST.""" |
| 16 | + |
| 17 | +fromcollectionsimportdeque |
| 18 | +importstring |
| 19 | +fromtypingimportDeque,Union |
| 20 | + |
| 21 | +importproto |
| 22 | +importrequests |
| 23 | +importgoogle.protobuf.message |
| 24 | +fromgoogle.protobuf.json_formatimportParse |
| 25 | + |
| 26 | + |
| 27 | +classResponseIterator: |
| 28 | +"""Iterator over REST API responses. |
| 29 | +
|
| 30 | + Args: |
| 31 | + response (requests.Response): An API response object. |
| 32 | + response_message_cls (Union[proto.Message, google.protobuf.message.Message]): A response |
| 33 | + class expected to be returned from an API. |
| 34 | +
|
| 35 | + Raises: |
| 36 | + ValueError: If `response_message_cls` is not a subclass of `proto.Message` or `google.protobuf.message.Message`. |
| 37 | + """ |
| 38 | + |
| 39 | +def__init__( |
| 40 | +self, |
| 41 | +response:requests.Response, |
| 42 | +response_message_cls:Union[proto.Message,google.protobuf.message.Message], |
| 43 | + ): |
| 44 | +self._response=response |
| 45 | +self._response_message_cls=response_message_cls |
| 46 | +# Inner iterator over HTTP response's content. |
| 47 | +self._response_itr=self._response.iter_content(decode_unicode=True) |
| 48 | +# Contains a list of JSON responses ready to be sent to user. |
| 49 | +self._ready_objs:Deque[str]=deque() |
| 50 | +# Current JSON response being built. |
| 51 | +self._obj="" |
| 52 | +# Keeps track of the nesting level within a JSON object. |
| 53 | +self._level=0 |
| 54 | +# Keeps track whether HTTP response is currently sending values |
| 55 | +# inside of a string value. |
| 56 | +self._in_string=False |
| 57 | +# Whether an escape symbol "\" was encountered. |
| 58 | +self._escape_next=False |
| 59 | + |
| 60 | +defcancel(self): |
| 61 | +"""Cancel existing streaming operation.""" |
| 62 | +self._response.close() |
| 63 | + |
| 64 | +def_process_chunk(self,chunk:str): |
| 65 | +ifself._level==0: |
| 66 | +ifchunk[0]!="[": |
| 67 | +raiseValueError( |
| 68 | +"Can only parse array of JSON objects, instead got %s"%chunk |
| 69 | + ) |
| 70 | +forcharinchunk: |
| 71 | +ifchar=="{": |
| 72 | +ifself._level==1: |
| 73 | +# Level 1 corresponds to the outermost JSON object |
| 74 | +# (i.e. the one we care about). |
| 75 | +self._obj="" |
| 76 | +ifnotself._in_string: |
| 77 | +self._level+=1 |
| 78 | +self._obj+=char |
| 79 | +elifchar=="}": |
| 80 | +self._obj+=char |
| 81 | +ifnotself._in_string: |
| 82 | +self._level-=1 |
| 83 | +ifnotself._in_stringandself._level==1: |
| 84 | +self._ready_objs.append(self._obj) |
| 85 | +elifchar=='"': |
| 86 | +# Helps to deal with an escaped quotes inside of a string. |
| 87 | +ifnotself._escape_next: |
| 88 | +self._in_string=notself._in_string |
| 89 | +self._obj+=char |
| 90 | +elifcharinstring.whitespace: |
| 91 | +ifself._in_string: |
| 92 | +self._obj+=char |
| 93 | +elifchar=="[": |
| 94 | +ifself._level==0: |
| 95 | +self._level+=1 |
| 96 | +else: |
| 97 | +self._obj+=char |
| 98 | +elifchar=="]": |
| 99 | +ifself._level==1: |
| 100 | +self._level-=1 |
| 101 | +else: |
| 102 | +self._obj+=char |
| 103 | +else: |
| 104 | +self._obj+=char |
| 105 | +self._escape_next=notself._escape_nextifchar=="\\"elseFalse |
| 106 | + |
| 107 | +def__next__(self): |
| 108 | +whilenotself._ready_objs: |
| 109 | +try: |
| 110 | +chunk=next(self._response_itr) |
| 111 | +self._process_chunk(chunk) |
| 112 | +exceptStopIterationase: |
| 113 | +ifself._level>0: |
| 114 | +raiseValueError("Unfinished stream: %s"%self._obj) |
| 115 | +raisee |
| 116 | +returnself._grab() |
| 117 | + |
| 118 | +def_grab(self): |
| 119 | +# Add extra quotes to make json.loads happy. |
| 120 | +ifissubclass(self._response_message_cls,proto.Message): |
| 121 | +returnself._response_message_cls.from_json( |
| 122 | +self._ready_objs.popleft(),ignore_unknown_fields=True |
| 123 | + ) |
| 124 | +elifissubclass(self._response_message_cls,google.protobuf.message.Message): |
| 125 | +returnParse(self._ready_objs.popleft(),self._response_message_cls()) |
| 126 | +else: |
| 127 | +raiseValueError( |
| 128 | +"Response message class must be a subclass of proto.Message or google.protobuf.message.Message." |
| 129 | + ) |
| 130 | + |
| 131 | +def__iter__(self): |
| 132 | +returnself |