wiseflow/core/utils/pb_api.py

90 lines
3.7 KiB
Python
Raw Normal View History

2024-04-07 09:37:47 +08:00
import os
from pocketbase import PocketBase # Client also works the same
from pocketbase.client import FileUpload
from typing import BinaryIO
class PbTalker:
2024-04-29 23:06:17 +08:00
def __init__(self, logger) -> None:
2024-04-17 14:02:25 +08:00
# 1. base initialization
2024-06-15 15:41:31 +08:00
url = os.environ.get('PB_API_BASE', "http://127.0.0.1:8090")
2024-04-29 23:06:17 +08:00
self.logger = logger
2024-04-07 09:37:47 +08:00
self.logger.debug(f"initializing pocketbase client: {url}")
self.client = PocketBase(url)
auth = os.environ.get('PB_API_AUTH', '')
if not auth or "|" not in auth:
2024-06-13 21:08:58 +08:00
self.logger.warnning("invalid email|password found, will handle with not auth, make sure you have set the collection rule by anyone")
2024-04-07 09:37:47 +08:00
else:
email, password = auth.split('|')
2024-06-13 21:08:58 +08:00
try:
admin_data = self.client.admins.auth_with_password(email, password)
if admin_data:
self.logger.info(f"pocketbase ready authenticated as admin - {email}")
except:
user_data = self.client.collection("users").auth_with_password(email, password)
if user_data:
self.logger.info(f"pocketbase ready authenticated as user - {email}")
else:
raise Exception("pocketbase auth failed")
2024-04-07 09:37:47 +08:00
def read(self, collection_name: str, fields: list[str] = None, filter: str = '', skiptotal: bool = True) -> list:
results = []
for i in range(1, 10):
try:
2024-06-13 21:08:58 +08:00
res = self.client.collection(collection_name).get_list(i, 500,
{"filter": filter,
"fields": ','.join(fields) if fields else '',
"skiptotal": skiptotal})
2024-04-07 09:37:47 +08:00
except Exception as e:
self.logger.error(f"pocketbase get list failed: {e}")
continue
if not res.items:
break
for _res in res.items:
attributes = vars(_res)
results.append(attributes)
return results
def add(self, collection_name: str, body: dict) -> str:
try:
res = self.client.collection(collection_name).create(body)
except Exception as e:
self.logger.error(f"pocketbase create failed: {e}")
return ''
return res.id
def update(self, collection_name: str, id: str, body: dict) -> str:
try:
res = self.client.collection(collection_name).update(id, body)
except Exception as e:
self.logger.error(f"pocketbase update failed: {e}")
return ''
return res.id
2024-06-16 20:42:01 +08:00
def delete(self, collection_name: str, id: str) -> bool:
2024-04-07 09:37:47 +08:00
try:
res = self.client.collection(collection_name).delete(id)
except Exception as e:
self.logger.error(f"pocketbase update failed: {e}")
2024-06-16 20:42:01 +08:00
return False
2024-04-07 09:37:47 +08:00
if res:
2024-06-16 20:42:01 +08:00
return True
return False
2024-04-07 09:37:47 +08:00
def upload(self, collection_name: str, id: str, key: str, file_name: str, file: BinaryIO) -> str:
try:
res = self.client.collection(collection_name).update(id, {key: FileUpload((file_name, file))})
except Exception as e:
self.logger.error(f"pocketbase update failed: {e}")
return ''
return res.id
2024-06-13 21:08:58 +08:00
def view(self, collection_name: str, item_id: str, fields: list[str] = None) -> dict:
try:
2024-06-15 15:41:31 +08:00
res = self.client.collection(collection_name).get_one(item_id, {"fields": ','.join(fields) if fields else ''})
2024-06-13 21:08:58 +08:00
return vars(res)
except Exception as e:
self.logger.error(f"pocketbase view item failed: {e}")
return {}