2021-11-02 08:54:07 +01:00
|
|
|
from logging import error
|
2021-10-28 16:09:10 +02:00
|
|
|
import requests
|
|
|
|
|
|
|
|
from config import *
|
2021-11-02 08:54:07 +01:00
|
|
|
from .error_handler import KratosError
|
|
|
|
|
|
|
|
class KratosApi:
|
|
|
|
@staticmethod
|
|
|
|
def __handleError(res):
|
|
|
|
if res.status_code >= 400:
|
|
|
|
message = res.json()["error"]["message"]
|
|
|
|
raise KratosError(message, res.status_code)
|
2021-10-28 16:09:10 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get(url):
|
|
|
|
try:
|
2022-04-04 14:24:30 +02:00
|
|
|
res = requests.get("{}{}".format(KRATOS_ADMIN_URL, url))
|
2021-11-02 08:54:07 +01:00
|
|
|
KratosApi.__handleError(res)
|
|
|
|
return res
|
2022-01-18 10:48:18 +01:00
|
|
|
except KratosError as err:
|
|
|
|
raise err
|
2021-10-28 16:09:10 +02:00
|
|
|
except:
|
2021-11-02 08:54:07 +01:00
|
|
|
raise KratosError()
|
2021-10-28 16:09:10 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def post(url, data):
|
|
|
|
try:
|
2022-04-04 14:24:30 +02:00
|
|
|
res = requests.post("{}{}".format(KRATOS_ADMIN_URL, url), json=data)
|
2021-11-02 08:54:07 +01:00
|
|
|
KratosApi.__handleError(res)
|
|
|
|
return res
|
|
|
|
except KratosError as err:
|
|
|
|
raise err
|
2021-10-28 16:09:10 +02:00
|
|
|
except:
|
2021-11-02 08:54:07 +01:00
|
|
|
raise KratosError()
|
2021-10-28 16:09:10 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def put(url, data):
|
|
|
|
try:
|
2022-04-04 14:24:30 +02:00
|
|
|
res = requests.put("{}{}".format(KRATOS_ADMIN_URL, url), json=data)
|
2021-11-02 08:54:07 +01:00
|
|
|
KratosApi.__handleError(res)
|
|
|
|
return res
|
|
|
|
except KratosError as err:
|
|
|
|
raise err
|
2021-10-28 16:09:10 +02:00
|
|
|
except:
|
2021-11-02 08:54:07 +01:00
|
|
|
raise KratosError()
|
2021-10-28 16:09:10 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def delete(url):
|
|
|
|
try:
|
2022-04-04 14:24:30 +02:00
|
|
|
res = requests.delete("{}{}".format(KRATOS_ADMIN_URL, url))
|
2021-11-02 08:54:07 +01:00
|
|
|
KratosApi.__handleError(res)
|
|
|
|
return res
|
|
|
|
except KratosError as err:
|
|
|
|
raise err
|
2021-10-28 16:09:10 +02:00
|
|
|
except:
|
2021-11-02 08:54:07 +01:00
|
|
|
raise KratosError()
|