A bit rework / refactoring.

This commit is contained in:
Kirill Zhosul
2022-03-16 14:12:44 +04:00
parent 0fa096f9ba
commit 1dc980eef5
5 changed files with 139 additions and 56 deletions

View File

@@ -1,38 +1,61 @@
"""
Main Gitflic API wrapper.
"""
from enum import Enum
from .auth import GitflicAuth
from .exceptions import NotFound, NoRights, GitflicExceptions
from .exceptions import (
NotFound, NoRights, GitflicExceptions
)
API_URL = 'https://api.gitflic.ru'
class GitflicAPIMethods(Enum):
""" Methods that you may use for calling API. """
USER_ME = "/user/me"
USER = "/user"
# There is not all methods, please expand if you need some other method.
class Gitflic:
"""
Gitflic API wrapper.
"""
def __init__(self, gf_session: GitflicAuth):
"""
:param gf_session:
: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"No rights for '{url}'")
raise NoRights(f"Access denied for '{url}'")
elif code == 404:
raise NotFound(f"Response '{url}' not found")
raise NotFound(f"Location '{url}' not found")
raise GitflicExceptions(f"Gitflic send error: {code}. {response.text}")
raise GitflicExceptions(f"Gitflic sent unknown error with HTTP code: {code}. Response: {response.text}")
def call(self, method, *args, **kwargs):
def call(self, method: str, *args, **kwargs):
"""
:param method:
:param args:
:param kwargs:
:return:
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)