aff0446458
--- - Cria singleton de `client` com func `configure` para inicializar e `get_instance` para buscar instância do client; - Ajusta clients para buscar headers vindo do pai `client` e fixa versão legacy no client de databases; - Adiciona inicialização de `client` no init do projeto com api_token e api_version informados pelo usuário; - Altera `NotionConfig` para inserir `database_id` no lugar de `database_name`; - Altera sistema para receber `database_id` no lugar de `database_name`; - Altera tipo de `properties` em `schemas.responses.pages.Page` de `Union[Dict[str, Any]], TDB` para `Union[Any, TDB]` para resolver reclamações de type hint; - Adiciona param `generic_response` no init de `client` e nos clients e databases e pages para pular uso de mapping ao usar `.generic`; - Adiciona param `raw_response` para pular parser e mappings e retornar resposta original da api; - Finaliza `types` com subpastas para importações mas com init mãe vazio para evitar dependência circular e permitir uso de `notion.types.` pelo usuário; - Remove importações do projeto original não relacionadas com o SDK; - Adiciona param `timezone` na func `start_date` em `orm.common.SetProperty` que antes vinha do env, para posteriormente puxar da init da integração; - Monta `LICENSE`, `README.md` e `pyproject.toml` base simples para commit inicial do projeto permitindo build de pacote; ---
93 lines
2.7 KiB
Python
Executable File
93 lines
2.7 KiB
Python
Executable File
from typing import Dict, Any, Optional, TypeVar, Generic
|
|
from ....schemas.responses.pages.Page import Page as _schmPage
|
|
from ....schemas.responses.errors.Error import Error as _schmError
|
|
from ....client import get_client as _get_client
|
|
from ...mapping.database import NotionDatabase as _NotionDatabase
|
|
from ...mapping import Mapping as _map
|
|
from ...parsers import Parser as _parser
|
|
|
|
TDB = TypeVar('TDB', bound =_NotionDatabase)
|
|
|
|
class GetPage(Generic[TDB]):
|
|
|
|
def __init__(self,
|
|
database_id : Optional[str] = None,
|
|
generic_response : bool = False
|
|
) -> None:
|
|
self._database_id : Optional[str] = database_id
|
|
self._generic_response = generic_response
|
|
self._pageid : str
|
|
|
|
def set_pageid(self,
|
|
id : str
|
|
) -> 'GetPage[TDB]':
|
|
self._pageid = id
|
|
return self
|
|
|
|
def set_database(self,
|
|
id : str
|
|
) -> 'GetPage[TDB]':
|
|
self._database_id = id
|
|
return self
|
|
|
|
async def select(self,
|
|
property : str
|
|
) -> Optional[Any]:
|
|
|
|
client = _get_client()
|
|
|
|
page : Dict[str, Any] = await client.pages.get(
|
|
page_id = self._pageid
|
|
)
|
|
|
|
if page['object'] == 'error':
|
|
error = _schmError(**page)
|
|
raise KeyError(error.__dict__)
|
|
|
|
page["properties"] = _parser.page_props(page = page)
|
|
if page["properties"] is None:
|
|
return None
|
|
allprops = page["properties"]
|
|
|
|
return allprops[property]
|
|
|
|
async def call(self,
|
|
map_properties : bool = True,
|
|
raw_response : bool = False
|
|
) -> _schmPage[TDB]:
|
|
|
|
client = _get_client()
|
|
|
|
page : Dict[str, Any] = await client.pages.get(
|
|
page_id = self._pageid
|
|
)
|
|
|
|
if page['object'] == 'error':
|
|
error = _schmError(**page)
|
|
raise KeyError(error.__dict__)
|
|
|
|
if not raw_response:
|
|
|
|
if map_properties and self._database_id:
|
|
|
|
parser = None
|
|
if not self._generic_response:
|
|
# Tenta pegar parser do registry
|
|
parser = _map.registry.get_parser(
|
|
database_id = self._database_id,
|
|
page_parser = _parser.page_props
|
|
)
|
|
|
|
# Fallback: se database não registrada, usa parser genérico
|
|
if not parser:
|
|
parser = _parser.page_props
|
|
|
|
mapped_properties = parser(page = page)
|
|
page["properties"] = mapped_properties
|
|
|
|
elif map_properties:
|
|
|
|
page["properties"] = _parser.page_props(page = page)
|
|
|
|
return _schmPage(**page)
|