async def _slurp(self, res: ActorRefT, it: AsyncIterator) -> None: # this is used when the agent returns an AsyncIterator, # and simply consumes that async iterator. stream: Optional[StreamT] = None async for value in it: self.log.debug("%r yielded: %r", self.fun, value) if stream is None: stream = res.stream.get_active_stream() event = stream.current_event if event is not None: headers = event.headers reply_to: Optional[str] = None correlation_id: Optional[str] = None if isinstance(event.value, ReqRepRequest): req: ReqRepRequest = event.value reply_to = req.reply_to correlation_id = req.correlation_id elif headers: reply_to_bytes = headers.get("Faust-Ag-ReplyTo") if reply_to_bytes: reply_to = want_str(reply_to_bytes) correlation_id_bytes = headers.get( "Faust-Ag-CorrelationId") if correlation_id_bytes: correlation_id = want_str(correlation_id_bytes) if reply_to is not None: await self._reply(event.key, value, reply_to, cast(str, correlation_id)) await self._delegate_to_sinks(value)
def test_combinators(input: Mapping[str, str]) -> None: s = json() | _binary() assert repr(s).replace("u'", "'") == 'json() | binary()' d = s.dumps(input) assert isinstance(d, bytes) assert _json.loads(want_str(base64.b64decode(d))) == input
def _prepare_payload(self, typ: Optional[ModelArg], value: Any) -> Any: if typ is None: # (autodetect) return self.Model._maybe_reconstruct(value) elif typ is int: return int(want_str(value)) elif typ is float: return float(want_str(value)) elif typ is Decimal: return Decimal(want_str(value)) elif typ is str: return want_str(value) elif typ is bytes: return want_bytes(value) else: # type set to Model model = cast(ModelT, typ) return model.from_data(value, preferred_type=model)
def merge_headers(target: OpenHeadersArg, source: Mapping[str, Any]) -> None: # XXX modify in-place if source: source = {want_str(k): want_bytes(v) for k, v in source.items()} if isinstance(target, Mapping): target = cast(MutableMapping, target) target.update({k: v for k, v in source.items()}) elif isinstance(target, list): target.extend((h for h in source.items()))
def from_headers(cls, headers: Mapping) -> Optional['TestExecution']: """Create instance from mapping of HTTP/Kafka headers.""" try: test_id = want_str(headers[HEADER_TEST_ID]) except KeyError: return None else: test_name = headers[HEADER_TEST_NAME] timestamp = headers[HEADER_TEST_TIMESTAMP] expires = headers[HEADER_TEST_EXPIRES] return cls( id=test_id, case_name=want_str(test_name), timestamp=parse_iso8601(want_str(timestamp)), expires=parse_iso8601(want_str(expires)), test_args=(), test_kwargs={}, )
async def proc(request, target, item): """ $ curl -s -d '{"sentence":"Hugging Face is a technology company based in New York and Paris"}' \ -H "Content-Type: application/json" -X POST \ localhost:1755/proc/t5/T5_de | json $ curl -s -d '{"sentence":"Hugging Face is a technology company"}' \ -H "Content-Type: application/json" -X POST \ localhost:1755/proc/simple/Simple | json :param request: :return: """ from mode.utils.compat import want_bytes, want_str rd = request.json result = listings.proc(target, item, rd) return json(result, dumps=lambda c: want_str(c.dumps(serializer='json')))
def merge_headers(target: HeadersArg, source: Mapping[str, Any]) -> HeadersArg: # XXX may modify in-place, but always use return value. if target is None: target = [] if source: source = {want_str(k): want_bytes(v) for k, v in source.items()} if isinstance(target, MutableMapping): target.update({k: v for k, v in source.items()}) elif isinstance(target, Mapping): target = dict(target) target.update({k: v for k, v in source.items()}) elif isinstance(target, list): target.extend((h for h in source.items())) elif isinstance(target, tuple): target += tuple(h for h in source.items()) elif isinstance(target, Iterable): target = list(cast(Iterable[Tuple[str, bytes]], target)) target.extend((h for h in source.items())) return target
def _loads(self, s: bytes) -> Any: if _yaml is None: raise ImproperlyConfigured('Missing yaml: pip install PyYAML') return _yaml.safe_load(want_str(s))
def _loads(self, s: bytes) -> Any: return _json.loads(want_str(s))
def _splitheader(self, header: _bytes) -> Tuple[str, str]: key, value = header.split(self.header_key_value_separator, 1) return want_str(key.strip()), want_str(value.strip())
def test_want_str(input, expected): assert want_str(input) == expected