def forge(type_: Type["OneOfMessages"], data: JSONMapping, forge: Forge) -> "OneOfMessages": if "oneOf" in data: return type_(one_of=forge(type_.__annotations__["one_of"], data["oneOf"]), ) return type_(one_of=[forge(Message, data)])
def forge(type_: Type["Message"], data: JSONMapping, forge: Forge) -> "Message": return type_( name=forge(type_.__annotations__["name"], data["name"]), payload=forge(type_.__annotations__["payload"], data.get("payload")), x_handler=forge(type_.__annotations__["x_handler"], data.get("x-handler")), x_ack=forge(type_.__annotations__["x_ack"], data.get("x-ack")), )
def forge(type_: Type["OAuth2Flow"], data: JSONMapping, forge: Forge) -> "OAuth2Flow": return type_( scopes=forge(type_.__annotations__["scopes"], data.get("scopes")), authorization_url=forge(type_.__annotations__["authorization_url"], data.get("authorizationUrl")), token_url=forge(type_.__annotations__["token_url"], data.get("tokenUrl")), refresh_url=forge(type_.__annotations__["refresh_url"], data.get("refreshUrl")), )
def forge(type_: Type["Channel"], data: JSONMapping, forge: Forge) -> "Channel": return type_( subscribe=forge(type_.__annotations__["subscribe"], data.get("subscribe")), publish=forge(type_.__annotations__["publish"], data.get("publish")), bindings=forge(type_.__annotations__["bindings"], data.get("bindings")), x_handlers=forge(type_.__annotations__["x_handlers"], data.get("x-handlers")), x_security=forge(type_.__annotations__["x_security"], data.get("x-security")), )
def forge(type_: Type["OAuth2Flows"], data: JSONMapping, forge: Forge) -> "OAuth2Flows": return type_( implicit=forge(type_.__annotations__["implicit"], data.get("implicit")), password=forge(type_.__annotations__["password"], data.get("password")), client_credentials=forge( type_.__annotations__["client_credentials"], data.get("clientCredentials"), ), authorization_code=forge( type_.__annotations__["authorization_code"], data.get("authorizationCode"), ), )
def __call__(self, *args: Any, **kwargs: Any) -> T: json = self._radegast.session(self.method, self.url(self.url_params(kwargs)), params=self.get_params()) return forge(self._request.response_type, self._request.processor(json))
def test_one_of_messages_deserialisation_of_message_structure(faker: Faker): name = faker.pystr() payload = faker.pydict(value_types=[str, int]) x_handler = faker.pystr() data = {"name": name, "payload": payload, "x-handler": x_handler} one_of_messages = forge(OneOfMessages, data) assert len(one_of_messages.one_of) == 1 assert one_of_messages.one_of[0].name == name assert one_of_messages.one_of[0].payload == payload assert one_of_messages.one_of[0].x_handler == x_handler
def test_message_deserialisation_with_missing_fields(faker: Faker): name = faker.pystr() message = forge( Message, {"name": name}, ) assert message.name == name assert message.payload is None assert message.x_handler is None assert message.x_ack is None
def test_one_of_messages_deserialisation_of_one_of_structure(faker: Faker): n = faker.pyint(min_value=2, max_value=10) data = { "oneOf": [{ "name": faker.pystr(), "payload": faker.pydict(value_types=[str, int]), "x-handler": faker.pystr(), } for _ in range(n)] } one_of_messages = forge(OneOfMessages, data) assert len(one_of_messages.one_of) == n
def test_channel_deserialization(faker: Faker): data = { "subscribe": { "message": { "oneOf": [{ "name": faker.pystr(), "payload": faker.pydict(value_types=[str, int]), } for _ in range(faker.pyint(min_value=2, max_value=10))] } }, "publish": { "message": { "oneOf": [{ "name": faker.pystr(), "payload": faker.pydict(value_types=[str, int]), "x-handler": faker.pydict(value_types=[str, int]), } for _ in range(faker.pyint(min_value=2, max_value=10))] } }, "bindings": { "ws": { "method": faker.pystr(), "query": faker.pydict(value_types=[str, int]), } }, "x-handlers": { "connect": faker.pystr(), "disconnect": faker.pystr(), }, "x-security": [{ "basic": [] }], } channel = forge(Channel, data) assert isinstance(channel.publish, Operation) assert isinstance(channel.subscribe, Operation) assert isinstance(channel.bindings, ChannelBindings) assert isinstance(channel.x_handlers, ChannelHandlers) assert isinstance(channel.x_security, list) assert len(channel.x_security) == 1 assert "basic" in channel.x_security[0] assert isinstance(channel.x_security[0]["basic"], list) assert len(channel.x_security[0]["basic"]) == 0
def test_message_deserialisation(faker: Faker): name = faker.pystr() payload = faker.pydict(value_types=[str, int]) x_handler = faker.pystr() x_ack_args = faker.pydict(value_types=[str, int]) message = forge( Message, { "name": name, "payload": payload, "x-handler": x_handler, "x-ack": { "args": x_ack_args }, }, ) assert message.name == name assert message.payload == payload assert message.x_handler == x_handler assert message.x_ack assert message.x_ack.args == x_ack_args
def from_dict(data: JSONMapping) -> "AsyncApiSpec": spec = forge(AsyncApiSpec, data) spec._raw = data # type: ignore return spec
def forge(type_: Type["Components"], data: JSONMapping, forge: Forge) -> "Components": return type_(security_schemes=forge( type_.__annotations__["security_schemes"], data.get("securitySchemes", dict()), ))
def forge(type_: Type["SecurityScheme"], data: JSONMapping, forge: Forge) -> "SecurityScheme": return type_( type=forge(type_.__annotations__["type"], data.get("type")), description=forge(type_.__annotations__["description"], data.get("description")), name=forge(type_.__annotations__["name"], data.get("name")), in_=forge(type_.__annotations__["in_"], data.get("in")), scheme=forge(type_.__annotations__["scheme"], data.get("scheme")), bearer_format=forge(type_.__annotations__["bearer_format"], data.get("bearerFormat")), flows=forge(type_.__annotations__["flows"], data.get("flows")), open_id_connect_url=forge( type_.__annotations__["open_id_connect_url"], data.get("openIdConnectUrl"), ), x_basic_info_func=forge(type_.__annotations__["x_basic_info_func"], data.get("x-basicInfoFunc")), x_bearer_info_func=forge( type_.__annotations__["x_bearer_info_func"], data.get("x-bearerInfoFunc"), ), x_token_info_func=forge(type_.__annotations__["x_token_info_func"], data.get("x-tokenInfoFunc")), x_api_key_info_func=forge( type_.__annotations__["x_api_key_info_func"], data.get("x-apiKeyInfoFunc"), ), x_scope_validate_func=forge( type_.__annotations__["x_scope_validate_func"], data.get("x-scopeValidateFunc"), ), )
def params(self, **kwargs): return dataclasses.replace(self, _params=forge(self._request.params, kwargs))