some cleanup

This commit is contained in:
NichtJens
2021-06-01 09:30:43 +02:00
parent 650f7be38d
commit f36e2534c4
2 changed files with 11 additions and 12 deletions

View File

@ -1,6 +1,6 @@
import requests
import functools import functools
import json import json
import requests
from .authmixin import AuthMixin, AuthError, HEADER_JSON from .authmixin import AuthMixin, AuthError, HEADER_JSON
@ -57,9 +57,6 @@ class HttpClient(AuthMixin):
def _login(self, payload=None, headers=None, timeout=10): def _login(self, payload=None, headers=None, timeout=10):
return requests.post(self.address + "/users/login", json=payload, headers=headers, timeout=timeout, verify=self._verify_certificate).json() return requests.post(self.address + "/users/login", json=payload, headers=headers, timeout=timeout, verify=self._verify_certificate).json()
def typename(self, obj):
return type(obj).__name__
@staticmethod @staticmethod
def make_filter(where:dict=None, limit:int=0, skip:int=0, fields:dict=None, include:dict=None, order:list=None): def make_filter(where:dict=None, limit:int=0, skip:int=0, fields:dict=None, include:dict=None, order:list=None):
filt = dict() filt = dict()

View File

@ -1,5 +1,6 @@
import functools import functools
from typing import get_type_hints from typing import get_type_hints
from .utils import typename
def typechecked(func): def typechecked(func):
@ -13,7 +14,7 @@ def typechecked(func):
return typechecked_call return typechecked_call
def property_maker(cls, name, type_name): def property_maker(name, type_name):
storage_name = '_' + name storage_name = '_' + name
@property @property
@ -31,6 +32,7 @@ def property_maker(cls, name, type_name):
class Snippet: class Snippet:
def __init__(self, **kwargs): def __init__(self, **kwargs):
super().__init__()
self._properties = [] self._properties = []
self.set_properties(**kwargs) self.set_properties(**kwargs)
@ -38,7 +40,7 @@ class Snippet:
for key, value in kwargs.items(): for key, value in kwargs.items():
storage_name = '_' + key storage_name = '_' + key
setattr(Snippet, storage_name, None) setattr(Snippet, storage_name, None)
setattr(Snippet, key, property_maker(self, key, value)) setattr(Snippet, key, property_maker(key, value))
self._properties.append(key) self._properties.append(key)
def to_dict(self, include_none=True): def to_dict(self, include_none=True):
@ -53,16 +55,16 @@ class Snippet:
@classmethod @classmethod
def from_dict(cls, properties): def from_dict(cls, properties):
tmp = cls() new = cls()
tmp.import_dict(properties) new.import_dict(properties)
return tmp return new
def __str__(self): def __str__(self):
return f"{type(self).__name__}" return typename(self)
@classmethod @classmethod
def from_http_response(cls, response): def from_http_response(cls, response):
if type(response) == list: if isinstance(response, list):
return [cls.from_dict(resp) for resp in response] return [cls.from_dict(resp) for resp in response]
else: else:
return cls.from_dict(response) return cls.from_dict(response)