diff --git a/fet2020api/__init__.py b/fet2020api/__init__.py index bd9dac9..05d30e6 100644 --- a/fet2020api/__init__.py +++ b/fet2020api/__init__.py @@ -1,38 +1,69 @@ import requests from .djangoapi import django_crud_api -class fet2020postapi(django_crud_api): - def __init__(self,endpoint="http://localhost:8106/api/posts/"): + + +class fet2020api(django_crud_api): + def __init__( + self, + endpoint="http://localhost:8106/api/posts/", + push_ids=["legacy_id"], + pk="id", + ): super().__init__(endpoint=endpoint) - - def read_post(self, slug=None,legacy_id=None): - return self.find_one({'slug':slug,'legacy_id':legacy_id}) - - def update_or_write_by_legacy_id(self,d): - if not 'legacy_id' in d: - raise AttributeError('legacy_id muss angegeben werden') - p=self.read_post(legacy_id=d['legacy_id']) + 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) + p = self.create(d) else: - p=self.update(p['slug'], d) + p = self.update(p[self.pk], d) return p - def push(self,d): + + +class fet2020postapi(django_crud_api): + def __init__(self, endpoint="http://localhost:8106/api/posts/"): + super().__init__(endpoint=endpoint) + + def read_post(self, slug=None, legacy_id=None): + return self.find_one({"slug": slug, "legacy_id": legacy_id}) + + def update_or_write_by_legacy_id(self, d): + if not "legacy_id" in d: + raise AttributeError("legacy_id muss angegeben werden") + p = self.read_post(legacy_id=d["legacy_id"]) + if p is None: + p = self.create(d) + else: + p = self.update(p["slug"], d) + return p + + def push(self, d): return self.update_or_write_by_legacy_id(d) + 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) - - def read_post(self, slug=None,legacy_id=None): - return self.find_one({'nickname':slug,'legacy_id':legacy_id}) - - def update_or_write_by_legacy_id(self,d): - if not 'legacy_id' in d: - raise AttributeError('legacy_id muss angegeben werden') - p=self.read_post(legacy_id=d['legacy_id']) + + def read_post(self, slug=None, legacy_id=None): + return self.find_one({"nickname": slug, "legacy_id": legacy_id}) + + def update_or_write_by_legacy_id(self, d): + if not "legacy_id" in d: + raise AttributeError("legacy_id muss angegeben werden") + p = self.read_post(legacy_id=d["legacy_id"]) if p is None: - p=self.create(d) + p = self.create(d) else: - p=self.update(p['slug'], d) + p = self.update(p["slug"], d) return p - \ No newline at end of file diff --git a/fet2020api/djangoapi.py b/fet2020api/djangoapi.py index bf35533..ff0f7dd 100644 --- a/fet2020api/djangoapi.py +++ b/fet2020api/djangoapi.py @@ -2,79 +2,83 @@ import requests from urllib.parse import urljoin from datetime import date, datetime import json -class django_crud_api(): - def __init__(self,**kwargs): - self.endpoint=kwargs.get('endpoint') - + +class django_crud_api: + def __init__(self, **kwargs): + self.endpoint = kwargs.get("endpoint") + def find(self, filter): 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=r.json() + r = r.json() except requests.exceptions.RequestException as e: print(e) return None return r - def find_one(self,filter): - r=self.find(filter) + + def find_one(self, filter): + r = self.find(filter) if r is None: return None - if len(r)>1: - raise LookupError("Mehr als ein Objekt von der API zurückgegeben filter: %s" % str(filter)) - if len(r)==0: + if len(r) > 1: + raise LookupError( + "Mehr als ein Objekt von der API zurückgegeben filter: %s" % str(filter) + ) + if len(r) == 0: return None return r[0] - def get(self,pk): + def get(self, pk): try: - r = requests.request('GET', urljoin(self.endpoint,pk), params={'format':'json'}) - r=r.json() + r = requests.request( + "GET", urljoin(self.endpoint, pk), params={"format": "json"} + ) + r = r.json() except requests.exceptions.RequestException as e: print(e) return None return r - - def update(self,pk,d): - try: - files=d.pop("files",None) - headers = None #{'Content-type': 'multipart/form-data'} + + def update(self, pk, d): + try: + files = d.pop("files", None) + headers = None # {'Content-type': 'multipart/form-data'} print("Updated %s " % pk) - r = requests.request('PUT', urljoin(self.endpoint,pk)+"/", headers=headers,data=d,files=files) - r.raise_for_status() + r = requests.request( + "PUT", + urljoin(self.endpoint, pk) + "/", + headers=headers, + data=d, + files=files, + ) + r.raise_for_status() except requests.exceptions.RequestException as 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 - - - if r.status_code==200: - r=r.json() - + + if r.status_code == 200: + r = r.json() + return r def create(self, d): try: - files=d.pop("files",None) - 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) + files = d.pop("files", None) + headers = None # {'Content-type': 'application/json'} + r = requests.post(self.endpoint, headers=headers, data=d, files=files) r.raise_for_status() except requests.exceptions.RequestException as e: print(e) - if r and r.status_code==400: + if r and r.status_code == 400: print(r.text) print("Tryed to reach %s with POST Action" % self.endpoint) - return r.status_code, None - if r.status_code==201: + if r.status_code == 201: return r.status_code, r.json() - #print(r) + # print(r) return r.status_code, r