from typings import Optional
from forestadmin.agent_toolkit.utils.context import User
from forestadmin.datasource_toolkit.collections import Collection
from forestadmin.datasource_toolkit.interfaces.records import RecordsDataAlias
import requests
class MyCollection(Collection):
def __init__(self):
self.addField('id', {
"is_read_only": True,
# ...
})
self.addField('title', {
"is_read_only": False,
# ...
})
async def create(
self, caller: User, List[RecordsDataAlias]
) -> List[RecordsDataAlias]:
records_to_return = []
for record in data:
response = requests.post('https://my-api/my-collection', json=record)
records_to_return = response.json()
return records_to_return
async def update(
self, caller: User, filter_: Optional[Filter], patch: RecordsDataAlias
) -> None:
record_ids = await self.list(caller, filter_, ["id"])
for record_id in record_ids:
requests.patch(f'https://my-api/my-collection/{record_id}', json=patch)
async def delete(
self, caller: User, filter_: Optional[Filter]
) -> None:
record_ids = await self.list(caller, filter_, ["id"])
for record_id in record_ids:
requests.delete(f'https://my-api/my-collection/{record_id}')