Added Api. Pocketbase. Python sdk
This commit is contained in:
0
Api/__init__.py
Normal file
0
Api/__init__.py
Normal file
40
Api/api.py
Normal file
40
Api/api.py
Normal file
@@ -0,0 +1,40 @@
|
||||
import json
|
||||
import requests
|
||||
|
||||
from login import LoginApi
|
||||
|
||||
|
||||
class Api:
|
||||
def __init__(self, collection_name: str, ):
|
||||
self.access_token: str
|
||||
self.collection_name = collection_name
|
||||
self.api_basepath = "http://127.0.0.1:8090/api"
|
||||
self.email = "juliancuni@gmail.com"
|
||||
self.password = "MatraPaPuPa..11"
|
||||
|
||||
login = LoginApi(email=self.email, password=self.password).login()
|
||||
self.access_token = login["token"]
|
||||
|
||||
def list_collection(self, filter=""):
|
||||
url = f"{self.api_basepath}/collections/{self.collection_name}/records?{filter}"
|
||||
res = requests.get(url=url, headers={
|
||||
'Authorization': f'Admin {self.access_token}'})
|
||||
return res.json()
|
||||
|
||||
def create_collection(self, obj, filter=""):
|
||||
url = f"{self.api_basepath}/collections/{self.collection_name}/records"
|
||||
exists = self.list_collection(filter=filter)
|
||||
if len(exists["items"]) > 0:
|
||||
return exists
|
||||
else:
|
||||
try:
|
||||
# obj = json.dumps(obj, allow_nan=False)
|
||||
print('saving')
|
||||
res = requests.post(url=url, json=obj, headers={
|
||||
'Authorization': f'Admin {self.access_token}'})
|
||||
return res.json()
|
||||
except ValueError as e:
|
||||
print(e)
|
||||
print(obj)
|
||||
return None
|
||||
return None
|
||||
16
Api/login.py
Normal file
16
Api/login.py
Normal file
@@ -0,0 +1,16 @@
|
||||
import requests
|
||||
|
||||
|
||||
class LoginApi:
|
||||
def __init__(self, email: str, password: str):
|
||||
self.email = email
|
||||
self.password = password
|
||||
self.api_basepath = "http://127.0.0.1:8090/api"
|
||||
self.login_endpoing = "/admins/auth-via-email"
|
||||
|
||||
def login(self):
|
||||
credentials = {"email": self.email, "password": self.password}
|
||||
print(self.api_basepath + self.login_endpoing)
|
||||
res = requests.post(self.api_basepath +
|
||||
self.login_endpoing, credentials)
|
||||
return res.json()
|
||||
81
Api/main.py
Normal file
81
Api/main.py
Normal file
@@ -0,0 +1,81 @@
|
||||
import os
|
||||
from posixpath import join
|
||||
from re import M
|
||||
import pandas as pd
|
||||
from api import Api
|
||||
|
||||
|
||||
class Main:
|
||||
def __init__(self):
|
||||
self.pacientet_api = Api("pacientet")
|
||||
self.klinikat_api = Api("klinikat")
|
||||
self.pacientet_klinikat_api = Api("pacientet_klinikat")
|
||||
|
||||
pass
|
||||
|
||||
def read_excel(self):
|
||||
xlsx_path = os.path.join(os.path.dirname(__file__), '../Excel/')
|
||||
for dirpath, dirname, filename in os.walk(xlsx_path):
|
||||
for name in filename:
|
||||
if name.startswith('kirurgji_') or name.startswith('OBS-GYN_') or name.startswith('urgjenca_'):
|
||||
klinika_emer = name.replace('.xlsx', '').split('_')[0]
|
||||
pacientet_klinikat_filter = f"filter=(emer='{klinika_emer}')"
|
||||
klinika_res = self.klinikat_api.create_collection(
|
||||
{"emer": klinika_emer}, filter=pacientet_klinikat_filter)
|
||||
klinika_id = klinika_res['items'][0]['id']
|
||||
df = pd.read_excel(os.path.join(dirpath, name), parse_dates=[
|
||||
'DATELINDJA_KORIGJ'], sheet_name='Sheet1')
|
||||
df.dropna(subset=['EMER'], inplace=True)
|
||||
for index, row in df.iterrows():
|
||||
emer = row['EMER'].strip()
|
||||
mbiemer = row['MBIEMER'].strip()
|
||||
if pd.isnull(df.loc[index, 'DATELINDJA_KORIGJ']):
|
||||
datelindja = ''
|
||||
else:
|
||||
datelindja = row['DATELINDJA_KORIGJ']
|
||||
mosha = row['MOSHA']
|
||||
if pd.isnull(df.loc[index, 'ERROR']):
|
||||
error = ''
|
||||
else:
|
||||
error = row['ERROR']
|
||||
|
||||
new_pacient = {"emer": emer, "mbiemer": mbiemer,
|
||||
"datelindja": str(datelindja), "mosha": str(mosha), "error": str(error)}
|
||||
pacient_filter = f"filter=(emer='{emer}' %26%26 mbiemer='{mbiemer}' %26%26 datelindja='{datelindja}')"
|
||||
|
||||
pacienti_res = self.pacientet_api.create_collection(
|
||||
new_pacient, filter=pacient_filter)
|
||||
pacienti_id = pacienti_res['items'][0]['id']
|
||||
|
||||
new_pacientet_klinikat = {
|
||||
"pacienti_id": pacienti_id, "klinika_id": klinika_id}
|
||||
pacientet_klinikat_filter = f"filter=(pacienti_id='{pacienti_id}' %26%26 klinika_id='{klinika_id}')"
|
||||
|
||||
pacient_klinika_res = self.pacientet_klinikat_api.create_collection(
|
||||
new_pacientet_klinikat, pacientet_klinikat_filter)
|
||||
print(pacient_klinika_res)
|
||||
|
||||
def test_backend(self) :
|
||||
# pacientet_klinikat_res = self.pacientet_klinikat_api.list_collection()
|
||||
# print(pacientet_klinikat_res["totalItems"])
|
||||
pacient_filter = f"expand=klinika_id"
|
||||
pacientet_res = self.pacientet_api.list_collection(filter=pacient_filter)
|
||||
print(pacientet_res)
|
||||
# print(pacientet_res["totalItems"])
|
||||
|
||||
main = Main()
|
||||
main.test_backend()
|
||||
# main.read_excel()
|
||||
# api = Api("pacientet")
|
||||
# new_pacient = {"emer": "Test", "mbiemer": "test",
|
||||
# "mosha": 100, "datelindja": datetime.now().isoformat()}
|
||||
# filter = f"(emer='{new_pacient['emer']}'&&mbiemer='{new_pacient['mbiemer']}'&&datelindja={new_pacient['datelindja']})"
|
||||
# pacientet = api.list_collection(filter=filter)
|
||||
# if len(pacientet["items"]) > 0:
|
||||
# print('egziston')
|
||||
# else:
|
||||
# print('saving')
|
||||
# res = api.create_collection(new_pacient)
|
||||
|
||||
|
||||
# # r = requests.post('http://127.0.0.1:8090/api/collections/pacientet/records', json=json=json.dumps(new_pacient), headers=headers)
|
||||
11
Api/pacienti.py
Normal file
11
Api/pacienti.py
Normal file
@@ -0,0 +1,11 @@
|
||||
class Pacienti:
|
||||
def __init__(self, emer: str, mbiemer: str, mosha: int, datelindja: str, error: str, access_token: str):
|
||||
self.emer = emer
|
||||
self.mbiemer = mbiemer
|
||||
self.mosha = mosha
|
||||
self.datelindja = datelindja
|
||||
self.error = error
|
||||
|
||||
def krijo_pactient(self):
|
||||
pactienti = {"emer": self.emer,
|
||||
"mbiemer": self.mbiemer, "mosha": self.mosha, "datelindja": self.datelindja, "error": self.error}
|
||||
16
Api/singleton.py
Normal file
16
Api/singleton.py
Normal file
@@ -0,0 +1,16 @@
|
||||
class Singleton:
|
||||
def __init__(self, decorated):
|
||||
self._decorated = decorated
|
||||
|
||||
def instance(self):
|
||||
try:
|
||||
return self._instance
|
||||
except AttributeError:
|
||||
self._instance = self._decorated()
|
||||
return self._instance
|
||||
|
||||
def __call__(self):
|
||||
raise TypeError('Singletons must be accessed through `instance()`.')
|
||||
|
||||
def __instancecheck__(self, inst):
|
||||
return isinstance(inst, self._decorated)
|
||||
@@ -1,9 +1,11 @@
|
||||
autopep8==1.7.0
|
||||
certifi==2022.6.15.2
|
||||
charset-normalizer==2.1.1
|
||||
et-xmlfile==1.1.0
|
||||
idna==3.4
|
||||
lxml==4.9.1
|
||||
numpy==1.23.3
|
||||
openpyxl==3.0.10
|
||||
pandas==1.4.4
|
||||
pycodestyle==2.9.1
|
||||
python-dateutil==2.8.2
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
|
||||
import requests
|
||||
|
||||
|
||||
class SaveToApi:
|
||||
pass
|
||||
|
||||
|
||||
class Pacienti:
|
||||
def __init__(self, emer: str, mbiemer: str, mosha: int):
|
||||
self.emer = emer
|
||||
self.mbiemer = mbiemer
|
||||
self.mosha = mosha
|
||||
self.api_basepath = "http://127.0.0.1:8090/api/"
|
||||
self.krijo_endpoint = "/api/collections/pacientet/records"
|
||||
|
||||
def krijo_pactient(self):
|
||||
pactienti = {"emer": self.emer,
|
||||
"mbiemer": self.mbiemer, "mosha": self.mosha}
|
||||
|
||||
Reference in New Issue
Block a user