Added private api_request method
This commit is contained in:
pare
1151661016
commit
84571c9d0f
S'han modificat 1 arxius amb 120 adicions i 7 eliminacions
127
Gitea.py
127
Gitea.py
|
@ -7,7 +7,7 @@ class Gitea:
|
||||||
|
|
||||||
name = 'Gitea API wrapper'
|
name = 'Gitea API wrapper'
|
||||||
|
|
||||||
def __init__(self, api_base_url=None, access_token=None):
|
def __init__(self, api_base_url=None, access_token=None, session=None):
|
||||||
|
|
||||||
self.__gitea_config_path = "config/gitea.txt"
|
self.__gitea_config_path = "config/gitea.txt"
|
||||||
|
|
||||||
|
@ -20,7 +20,12 @@ class Gitea:
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
||||||
self.__api_base_url, self.__access_token = self.setup(self)
|
self.__api_base_url, self.__access_token = self.__setup(self)
|
||||||
|
|
||||||
|
if session:
|
||||||
|
self.session = session
|
||||||
|
else:
|
||||||
|
self.session = requests.Session()
|
||||||
|
|
||||||
def register_user(self, email, username, passwd):
|
def register_user(self, email, username, passwd):
|
||||||
|
|
||||||
|
@ -35,8 +40,10 @@ class Gitea:
|
||||||
'username':username,
|
'username':username,
|
||||||
'visibility':'public'
|
'visibility':'public'
|
||||||
}
|
}
|
||||||
API_ENDPOINT = self.__api_base_url + '/api/v1/admin/users?'
|
|
||||||
response = requests.post(url = API_ENDPOINT + 'access_token=' + self.__access_token, data = data)
|
endpoint = self._Gitea__api_base_url + f'/api/v1/admin/users?access_token={self._Gitea__access_token}'
|
||||||
|
|
||||||
|
response = self.__api_request('POST', endpoint, data)
|
||||||
|
|
||||||
is_registered = response.ok
|
is_registered = response.ok
|
||||||
|
|
||||||
|
@ -49,8 +56,9 @@ class Gitea:
|
||||||
data = {
|
data = {
|
||||||
}
|
}
|
||||||
|
|
||||||
API_ENDPOINT = self._Gitea__api_base_url + '/api/v1/admin/users?'
|
endpoint = self._Gitea__api_base_url + f'/api/v1/admin/users?token={self._Gitea__access_token}'
|
||||||
response = requests.get(url = API_ENDPOINT + 'token=' + self._Gitea__access_token, data = data)
|
|
||||||
|
response = self.__api_request('GET', endpoint, data)
|
||||||
|
|
||||||
response_ok = response.ok
|
response_ok = response.ok
|
||||||
|
|
||||||
|
@ -71,7 +79,7 @@ class Gitea:
|
||||||
return is_setup
|
return is_setup
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def setup(self):
|
def __setup(self):
|
||||||
|
|
||||||
if not os.path.exists('config'):
|
if not os.path.exists('config'):
|
||||||
os.makedirs('config')
|
os.makedirs('config')
|
||||||
|
@ -99,3 +107,108 @@ class Gitea:
|
||||||
|
|
||||||
print(f'{file_path} Missing parameter {parameter}')
|
print(f'{file_path} Missing parameter {parameter}')
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
def __api_request(self, method, endpoint, data):
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
|
response = self.session.request(method, url = endpoint, data = data)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
|
||||||
|
raise GiteaNetworkError(f"Could not complete request: {e}")
|
||||||
|
|
||||||
|
if response is None:
|
||||||
|
|
||||||
|
raise GiteaIllegalArgumentError("Illegal request.")
|
||||||
|
|
||||||
|
if not response.ok:
|
||||||
|
|
||||||
|
try:
|
||||||
|
if isinstance(response, dict) and 'error' in response:
|
||||||
|
error_msg = response['error']
|
||||||
|
elif isinstance(response, str):
|
||||||
|
error_msg = response
|
||||||
|
else:
|
||||||
|
error_msg = None
|
||||||
|
except ValueError:
|
||||||
|
error_msg = None
|
||||||
|
|
||||||
|
if response.status_code == 404:
|
||||||
|
ex_type = GiteaNotFoundError
|
||||||
|
if not error_msg:
|
||||||
|
error_msg = 'Endpoint not found.'
|
||||||
|
# this is for compatibility with older versions
|
||||||
|
# which raised GiteaAPIError('Endpoint not found.')
|
||||||
|
# on any 404
|
||||||
|
elif response.status_code == 401:
|
||||||
|
ex_type = GiteaUnauthorizedError
|
||||||
|
elif response.status_code == 422:
|
||||||
|
return response
|
||||||
|
elif response.status_code == 500:
|
||||||
|
ex_type = GiteaInternalServerError
|
||||||
|
elif response.status_code == 502:
|
||||||
|
ex_type = GiteaBadGatewayError
|
||||||
|
elif response.status_code == 503:
|
||||||
|
ex_type = GiteaServiceUnavailableError
|
||||||
|
elif response.status_code == 504:
|
||||||
|
ex_type = GiteaGatewayTimeoutError
|
||||||
|
elif response.status_code >= 500 and \
|
||||||
|
response.status_code <= 511:
|
||||||
|
ex_type = GiteaServerError
|
||||||
|
else:
|
||||||
|
ex_type = GiteaAPIError
|
||||||
|
|
||||||
|
raise ex_type(
|
||||||
|
'Gitea API returned error',
|
||||||
|
response.status_code,
|
||||||
|
response.reason,
|
||||||
|
error_msg)
|
||||||
|
|
||||||
|
else:
|
||||||
|
|
||||||
|
return response
|
||||||
|
|
||||||
|
##
|
||||||
|
# Exceptions
|
||||||
|
##
|
||||||
|
class GiteaError(Exception):
|
||||||
|
"""Base class for Gitea.py exceptions"""
|
||||||
|
|
||||||
|
class GiteaIOError(IOError, GiteaError):
|
||||||
|
"""Base class for Gitea.py I/O errors"""
|
||||||
|
|
||||||
|
class GiteaNetworkError(GiteaIOError):
|
||||||
|
"""Raised when network communication with the server fails"""
|
||||||
|
pass
|
||||||
|
class GiteaAPIError(GiteaError):
|
||||||
|
"""Raised when the gitea API generates a response that cannot be handled"""
|
||||||
|
pass
|
||||||
|
class GiteaServerError(GiteaAPIError):
|
||||||
|
"""Raised if the Server is malconfigured and returns a 5xx error code"""
|
||||||
|
pass
|
||||||
|
class GiteaInternalServerError(GiteaServerError):
|
||||||
|
"""Raised if the Server returns a 500 error"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
class GiteaBadGatewayError(GiteaServerError):
|
||||||
|
"""Raised if the Server returns a 502 error"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
class GiteaServiceUnavailableError(GiteaServerError):
|
||||||
|
"""Raised if the Server returns a 503 error"""
|
||||||
|
pass
|
||||||
|
class GiteaGatewayTimeoutError(GiteaServerError):
|
||||||
|
"""Raised if the Server returns a 504 error"""
|
||||||
|
pass
|
||||||
|
class GiteaNotFoundError(GiteaAPIError):
|
||||||
|
"""Raised when the gitea API returns a 404 Not Found error"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
class GiteaUnauthorizedError(GiteaAPIError):
|
||||||
|
"""Raised when the gitea API returns a 401 Unauthorized error
|
||||||
|
|
||||||
|
This happens when an OAuth token is invalid or has been revoked,
|
||||||
|
or when trying to access an endpoint that can't be used without
|
||||||
|
authentication without providing credentials."""
|
||||||
|
pass
|
||||||
|
|
Loading…
Referencia en una nova incidència