initial commit
This commit is contained in:
143
src/komgapi/endpoints/baseapi.py
Normal file
143
src/komgapi/endpoints/baseapi.py
Normal file
@@ -0,0 +1,143 @@
|
||||
import requests
|
||||
from komgapi.errors import KomgaError, LoginError, ResultErrror
|
||||
from typing import Any, Union
|
||||
from limit import limit
|
||||
|
||||
|
||||
class BaseAPI:
|
||||
def __init__(self, username, password, url, timeout=20, api_version=1) -> None:
|
||||
self._username = username
|
||||
self._password = password
|
||||
self.url = url + f"api/v{api_version}/"
|
||||
self.timeout = timeout
|
||||
|
||||
def setParams(self, locals: dict) -> dict:
|
||||
return {
|
||||
param_name: param
|
||||
for param_name, param in locals.items()
|
||||
if param is not None and param_name not in ["self", "series_idurl"]
|
||||
}
|
||||
|
||||
def test_connection(self):
|
||||
"""Test the connection to the server.
|
||||
|
||||
Returns:
|
||||
bool: True if the connection is successful, False otherwise.
|
||||
"""
|
||||
try:
|
||||
requests.get(self.url, timeout=self.timeout)
|
||||
return True
|
||||
except requests.exceptions.RequestException:
|
||||
return False
|
||||
|
||||
def overwriteVersion(self, version: int):
|
||||
self.url = self.url.replace("api/v1/", f"api/v{version}/")
|
||||
return self
|
||||
|
||||
@limit(1, 1)
|
||||
def getRequest(self, url, params: Union[dict, None] = None) -> Any:
|
||||
if params is None:
|
||||
params = {}
|
||||
try:
|
||||
# ic(url, params)
|
||||
response = requests.get(
|
||||
url,
|
||||
auth=(self._username, self._password),
|
||||
params=params,
|
||||
timeout=self.timeout,
|
||||
)
|
||||
response.raise_for_status()
|
||||
# print(response.content)
|
||||
return response.json()
|
||||
except ConnectionError as e:
|
||||
message = f"Connection Error: {e}"
|
||||
raise KomgaError(message) from e
|
||||
except requests.exceptions.Timeout as e:
|
||||
raise KomgaError(f"Timeout Error: {e}") from e
|
||||
|
||||
def postRequest(self, url, data: Union[dict, None] = None):
|
||||
if data is None:
|
||||
data = {}
|
||||
try:
|
||||
response = requests.post(
|
||||
url,
|
||||
auth=(self._username, self._password),
|
||||
json=data,
|
||||
timeout=self.timeout,
|
||||
)
|
||||
response.raise_for_status()
|
||||
status_code = response.status_code
|
||||
if status_code != 202:
|
||||
raise ResultErrror(f"Result Error: {response.json()}")
|
||||
elif status_code == 200:
|
||||
return response.json()
|
||||
except ConnectionError as e:
|
||||
message = f"Connection Error: {e}"
|
||||
raise KomgaError(message) from e
|
||||
except requests.exceptions.Timeout as e:
|
||||
raise KomgaError(f"Timeout Error: {e}") from e
|
||||
|
||||
def patchRequest(self, url, data: Union[dict, None] = None):
|
||||
if data is None:
|
||||
data = {}
|
||||
try:
|
||||
response = requests.patch(
|
||||
url,
|
||||
auth=(self._username, self._password),
|
||||
json=data,
|
||||
timeout=self.timeout,
|
||||
)
|
||||
response.raise_for_status()
|
||||
if response.status_code != 204:
|
||||
raise ResultErrror(f"Result Error: {response.json()}")
|
||||
except ConnectionError as e:
|
||||
message = f"Connection Error: {e}"
|
||||
raise KomgaError(message) from e
|
||||
except requests.exceptions.Timeout as e:
|
||||
raise KomgaError(f"Timeout Error: {e}") from e
|
||||
|
||||
def putRequest(self, url, data: Union[dict, None] = None):
|
||||
if data is None:
|
||||
data = {}
|
||||
try:
|
||||
response = requests.put(
|
||||
url,
|
||||
auth=(self._username, self._password),
|
||||
json=data,
|
||||
timeout=self.timeout,
|
||||
)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
except ConnectionError as e:
|
||||
message = f"Connection Error: {e}"
|
||||
raise KomgaError(message) from e
|
||||
except requests.exceptions.Timeout as e:
|
||||
raise KomgaError(f"Timeout Error: {e}") from e
|
||||
|
||||
def deleteRequest(self, url):
|
||||
try:
|
||||
response = requests.delete(
|
||||
url, auth=(self._username, self._password), timeout=self.timeout
|
||||
)
|
||||
response.raise_for_status()
|
||||
return response.json()
|
||||
except ConnectionError as e:
|
||||
message = f"Connection Error: {e}"
|
||||
raise KomgaError(message) from e
|
||||
except requests.exceptions.Timeout as e:
|
||||
raise KomgaError(f"Timeout Error: {e}") from e
|
||||
|
||||
@classmethod
|
||||
def from_env(cls):
|
||||
"""Create a KOMGA API object from environment variables.
|
||||
|
||||
Returns:
|
||||
KOMGAPI_REST: The KOMGA API object.
|
||||
"""
|
||||
import os
|
||||
|
||||
return cls(
|
||||
os.environ["KOMGA_USERNAME"],
|
||||
os.environ["KOMGA_PASSWORD"],
|
||||
os.environ["KOMGA_URL"],
|
||||
)
|
||||
Reference in New Issue
Block a user