rewrite djangoapi
This commit is contained in:
@@ -1,38 +1,69 @@
|
|||||||
import requests
|
import requests
|
||||||
from .djangoapi import django_crud_api
|
from .djangoapi import django_crud_api
|
||||||
|
|
||||||
|
|
||||||
|
class fet2020api(django_crud_api):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
endpoint="http://localhost:8106/api/posts/",
|
||||||
|
push_ids=["legacy_id"],
|
||||||
|
pk="id",
|
||||||
|
):
|
||||||
|
super().__init__(endpoint=endpoint)
|
||||||
|
self.push_ids = push_ids
|
||||||
|
self.pk = pk
|
||||||
|
|
||||||
|
def push(self, d):
|
||||||
|
"this is a shortcut for pushing content to the database based on filter attributes"
|
||||||
|
for a in self.push_ids:
|
||||||
|
if not a in d: # all push attributes must be given
|
||||||
|
raise AttributeError(
|
||||||
|
f"{a} must be in data because it is in 'push_ids' = {self.push_ids}."
|
||||||
|
)
|
||||||
|
id = {key: d[key] for key in self.push_ids}
|
||||||
|
p = self.find_one(id)
|
||||||
|
|
||||||
|
if p is None:
|
||||||
|
p = self.create(d)
|
||||||
|
else:
|
||||||
|
p = self.update(p[self.pk], d)
|
||||||
|
return p
|
||||||
|
|
||||||
|
|
||||||
class fet2020postapi(django_crud_api):
|
class fet2020postapi(django_crud_api):
|
||||||
def __init__(self, endpoint="http://localhost:8106/api/posts/"):
|
def __init__(self, endpoint="http://localhost:8106/api/posts/"):
|
||||||
super().__init__(endpoint=endpoint)
|
super().__init__(endpoint=endpoint)
|
||||||
|
|
||||||
def read_post(self, slug=None, legacy_id=None):
|
def read_post(self, slug=None, legacy_id=None):
|
||||||
return self.find_one({'slug':slug,'legacy_id':legacy_id})
|
return self.find_one({"slug": slug, "legacy_id": legacy_id})
|
||||||
|
|
||||||
def update_or_write_by_legacy_id(self, d):
|
def update_or_write_by_legacy_id(self, d):
|
||||||
if not 'legacy_id' in d:
|
if not "legacy_id" in d:
|
||||||
raise AttributeError('legacy_id muss angegeben werden')
|
raise AttributeError("legacy_id muss angegeben werden")
|
||||||
p=self.read_post(legacy_id=d['legacy_id'])
|
p = self.read_post(legacy_id=d["legacy_id"])
|
||||||
if p is None:
|
if p is None:
|
||||||
p = self.create(d)
|
p = self.create(d)
|
||||||
else:
|
else:
|
||||||
p=self.update(p['slug'], d)
|
p = self.update(p["slug"], d)
|
||||||
return p
|
return p
|
||||||
|
|
||||||
def push(self, d):
|
def push(self, d):
|
||||||
return self.update_or_write_by_legacy_id(d)
|
return self.update_or_write_by_legacy_id(d)
|
||||||
|
|
||||||
|
|
||||||
class fet2020memberapi(django_crud_api):
|
class fet2020memberapi(django_crud_api):
|
||||||
def __init__(self, endpoint="http://localhost:8103/api/members/"):
|
def __init__(self, endpoint="http://localhost:8103/api/members/"):
|
||||||
super().__init__(endpoint=endpoint)
|
super().__init__(endpoint=endpoint)
|
||||||
|
|
||||||
def read_post(self, slug=None, legacy_id=None):
|
def read_post(self, slug=None, legacy_id=None):
|
||||||
return self.find_one({'nickname':slug,'legacy_id':legacy_id})
|
return self.find_one({"nickname": slug, "legacy_id": legacy_id})
|
||||||
|
|
||||||
def update_or_write_by_legacy_id(self, d):
|
def update_or_write_by_legacy_id(self, d):
|
||||||
if not 'legacy_id' in d:
|
if not "legacy_id" in d:
|
||||||
raise AttributeError('legacy_id muss angegeben werden')
|
raise AttributeError("legacy_id muss angegeben werden")
|
||||||
p=self.read_post(legacy_id=d['legacy_id'])
|
p = self.read_post(legacy_id=d["legacy_id"])
|
||||||
if p is None:
|
if p is None:
|
||||||
p = self.create(d)
|
p = self.create(d)
|
||||||
else:
|
else:
|
||||||
p=self.update(p['slug'], d)
|
p = self.update(p["slug"], d)
|
||||||
return p
|
return p
|
||||||
|
|
||||||
@@ -2,32 +2,40 @@ import requests
|
|||||||
from urllib.parse import urljoin
|
from urllib.parse import urljoin
|
||||||
from datetime import date, datetime
|
from datetime import date, datetime
|
||||||
import json
|
import json
|
||||||
class django_crud_api():
|
|
||||||
|
class django_crud_api:
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
self.endpoint=kwargs.get('endpoint')
|
self.endpoint = kwargs.get("endpoint")
|
||||||
|
|
||||||
def find(self, filter):
|
def find(self, filter):
|
||||||
try:
|
try:
|
||||||
r = requests.request('GET', self.endpoint, params={'format':'json',**filter})
|
r = requests.request(
|
||||||
|
"GET", self.endpoint, params={"format": "json", **filter}
|
||||||
|
)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
r = r.json()
|
r = r.json()
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
print(e)
|
print(e)
|
||||||
return None
|
return None
|
||||||
return r
|
return r
|
||||||
|
|
||||||
def find_one(self, filter):
|
def find_one(self, filter):
|
||||||
r = self.find(filter)
|
r = self.find(filter)
|
||||||
if r is None:
|
if r is None:
|
||||||
return None
|
return None
|
||||||
if len(r) > 1:
|
if len(r) > 1:
|
||||||
raise LookupError("Mehr als ein Objekt von der API zurückgegeben filter: %s" % str(filter))
|
raise LookupError(
|
||||||
|
"Mehr als ein Objekt von der API zurückgegeben filter: %s" % str(filter)
|
||||||
|
)
|
||||||
if len(r) == 0:
|
if len(r) == 0:
|
||||||
return None
|
return None
|
||||||
return r[0]
|
return r[0]
|
||||||
|
|
||||||
def get(self, pk):
|
def get(self, pk):
|
||||||
try:
|
try:
|
||||||
r = requests.request('GET', urljoin(self.endpoint,pk), params={'format':'json'})
|
r = requests.request(
|
||||||
|
"GET", urljoin(self.endpoint, pk), params={"format": "json"}
|
||||||
|
)
|
||||||
r = r.json()
|
r = r.json()
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
print(e)
|
print(e)
|
||||||
@@ -39,17 +47,19 @@ class django_crud_api():
|
|||||||
files = d.pop("files", None)
|
files = d.pop("files", None)
|
||||||
headers = None # {'Content-type': 'multipart/form-data'}
|
headers = None # {'Content-type': 'multipart/form-data'}
|
||||||
print("Updated %s " % pk)
|
print("Updated %s " % pk)
|
||||||
r = requests.request('PUT', urljoin(self.endpoint,pk)+"/", headers=headers,data=d,files=files)
|
r = requests.request(
|
||||||
|
"PUT",
|
||||||
|
urljoin(self.endpoint, pk) + "/",
|
||||||
|
headers=headers,
|
||||||
|
data=d,
|
||||||
|
files=files,
|
||||||
|
)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
print(e)
|
print(e)
|
||||||
if r:
|
|
||||||
print(r.text)
|
|
||||||
print(d.keys())
|
|
||||||
print("Tryed to reach %s with PUT Action" % urljoin(self.endpoint, pk))
|
print("Tryed to reach %s with PUT Action" % urljoin(self.endpoint, pk))
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
if r.status_code == 200:
|
if r.status_code == 200:
|
||||||
r = r.json()
|
r = r.json()
|
||||||
|
|
||||||
@@ -59,11 +69,6 @@ class django_crud_api():
|
|||||||
try:
|
try:
|
||||||
files = d.pop("files", None)
|
files = d.pop("files", None)
|
||||||
headers = None # {'Content-type': 'application/json'}
|
headers = None # {'Content-type': 'application/json'}
|
||||||
#print("Create %s" % d["slug"])
|
|
||||||
# print("Create with data %s" % str(d))
|
|
||||||
#for k in d:
|
|
||||||
# if type(d[k]) is date:
|
|
||||||
# d[k]=str(d[k])
|
|
||||||
r = requests.post(self.endpoint, headers=headers, data=d, files=files)
|
r = requests.post(self.endpoint, headers=headers, data=d, files=files)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
@@ -72,7 +77,6 @@ class django_crud_api():
|
|||||||
print(r.text)
|
print(r.text)
|
||||||
print("Tryed to reach %s with POST Action" % self.endpoint)
|
print("Tryed to reach %s with POST Action" % self.endpoint)
|
||||||
|
|
||||||
|
|
||||||
return r.status_code, None
|
return r.status_code, None
|
||||||
if r.status_code == 201:
|
if r.status_code == 201:
|
||||||
return r.status_code, r.json()
|
return r.status_code, r.json()
|
||||||
|
|||||||
Reference in New Issue
Block a user