Example #1
0
async def invoke_api_method(
    method_name: str,
    data: BaseModel,
    output_type: Type[OutputDataT],
) -> OutputDataT:
    """
    Generic function which invokes an arbitrary method from Telegram Bot API
    :param method_name: name of the API method
    :param data: some data for the method
    :param output_type: type of response data
    :return: object of output type
    """

    url = f"{TELEGRAM_BOT_API}/{method_name}"

    async with aiohttp.ClientSession() as session:
        response: ClientResponse
        async with session.post(url, json=data.dict()) as response:
            payload = await response.json()

            if response.status != status.HTTP_200_OK:
                debug(response)
                debug(payload)
                errmsg = (
                    f"method {method_name!r}"
                    f" failed with status {response.status}"
                )
                raise RuntimeError(errmsg)

    result = output_type.parse_obj(payload)

    return result
Example #2
0
    def save(self, model: BaseModel, section: str) -> None:
        subsections = section.split('/')
        value = self.__config
        for subsection in subsections:
            if subsection not in value:
                value[subsection] = {}
            value = value[subsection]

        value.update(model.dict())

        with open(self.config_file, "w") as f:
            f.write(dump(self.__config, Dumper=Dumper))
Example #3
0
File: crud.py Project: Kitware/dive
 def create(self, item: BaseModel):
     return self.save(item.dict())