mdrs-client-python/mdrsclient/api/base.py

36 lines
1.4 KiB
Python
Raw Permalink Normal View History

2023-05-01 20:00:32 +09:00
from abc import ABC
import requests
from pydantic import TypeAdapter
2023-05-01 20:00:32 +09:00
from requests import Response
from mdrsclient.connection import MDRSConnection
2023-05-01 20:00:32 +09:00
from mdrsclient.exceptions import (
BadRequestException,
ForbiddenException,
UnauthorizedException,
UnexpectedException,
)
2023-05-10 14:46:08 +09:00
from mdrsclient.models.error import DRFStandardizedErrors
2023-05-01 20:00:32 +09:00
class BaseApi(ABC):
connection: MDRSConnection
def __init__(self, connection: MDRSConnection) -> None:
self.connection = connection
2023-05-01 20:00:32 +09:00
def _raise_response_error(self, response: Response) -> None:
if response.status_code >= 300:
if response.status_code < 400 or response.status_code >= 500:
raise UnexpectedException(f"Unexpected status code returned: {response.status_code}.")
errors = TypeAdapter(DRFStandardizedErrors).validate_python(response.json())
2023-05-01 20:00:32 +09:00
if response.status_code == requests.codes.bad_request:
raise BadRequestException(errors.errors[0].detail)
elif response.status_code == requests.codes.unauthorized:
raise UnauthorizedException("Login required.")
elif response.status_code == requests.codes.forbidden:
raise ForbiddenException("You do not have enough permissions. Access is denied.")
else:
raise UnexpectedException(errors.errors[0].detail)