2022-03-16 16:10:14 +03:00

62 lines
1.6 KiB
Python

"""
Main Gitflic API wrapper.
"""
from enum import Enum
from .auth import GitflicAuth
from .exceptions import (
NotFound, NoRights, GitflicExceptions
)
API_URL = 'https://api.gitflic.ru'
class Gitflic:
"""
Gitflic API wrapper.
"""
def __init__(self, gf_session: GitflicAuth):
"""
:param gf_session: Authorized session from GitflicAuth.
"""
self.session = gf_session.session
@staticmethod
def _response_handler(response):
"""
Handles HTTP response from Gitflic API.
:param response: HTTP response.
:return: Exception or valid JSON.
"""
code = response.status_code
if code == 200:
return response.json()
url = response.url
if code == 403:
raise NoRights(f"Access denied for '{url}'")
elif code == 404:
raise NotFound(f"Location '{url}' not found")
raise GitflicExceptions(f"Gitflic sent unknown error with HTTP code: {code}. Response: {response.text}")
def call(self, method: str, *args, **kwargs):
"""
Calls API method on server side.
:param method: API method of Gitflic to call.
:return: Exception or valid JSON.
"""
response = self.session.get(API_URL + method, *args, **kwargs)
return self._response_handler(response)
def reg_call(self, method: str):
l = lambda add_to_method="", *_args, **_kwargs: self.call(method+add_to_method, *_args, **_kwargs)
l.__name__ = method.replace("/", "_")
return l