Added new endpoints

This commit is contained in:
spla 2022-12-08 19:22:06 +01:00
parent b5a2ed22ab
commit 99158f33c8
2 changed files with 163 additions and 23 deletions

178
Gitea.py
View file

@ -3,6 +3,22 @@ import sys
import requests
from requests.models import urlencode
###
# Dict helper class.
# Defined at top level so it can be pickled.
###
class AttribAccessDict(dict):
def __getattr__(self, attr):
if attr in self:
return self[attr]
else:
raise AttributeError("Attribute not found: " + str(attr))
def __setattr__(self, attr, val):
if attr in self:
raise AttributeError("Attribute-style access is read only")
super(AttribAccessDict, self).__setattr__(attr, val)
class Gitea:
name = 'Gitea API wrapper'
@ -45,11 +61,9 @@ class Gitea:
response = self.__api_request('POST', endpoint, data)
is_registered = response.ok
response = self.__json_allow_dict_attrs(response.json())
message = response.json()['message']
return (is_registered, message)
return response
def admin_users_list(self, page=None, limit=None):
@ -65,11 +79,9 @@ class Gitea:
response = self.__api_request('GET', endpoint)
response_ok = response.ok
response = self.__json_allow_dict_attrs(response.json())
user_list = response.json()
return (response_ok, user_list)
return response
def notifications_new(self):
@ -77,11 +89,27 @@ class Gitea:
response = self.__api_request('GET', endpoint)
response_ok = response.ok
response = self.__json_allow_dict_attrs(response.json())
notifications = response.json()
return response
return (response_ok, notifications)
###
### repository
###
def repos_get_repo(self, owner, repo):
params = dict()
params['token'] = self.access_token
formatted_params = urlencode(params)
endpoint = self.api_base_url + '/api/v1/repos/{0}/{1}'.format(owner, repo, formatted_params)
response = self.__api_request('GET', endpoint)
response = self.__json_allow_dict_attrs(response.json())
return response
def repos_issues_search(self, owner, state=None, labels=None, q=None, milestones=None, priority_repo_id=None, issue_type=None, since=None, before=None, assigned=None,
created=None, mentioned=None, review_requested=None, team=None, page=None, limit=None):
@ -128,11 +156,9 @@ class Gitea:
response = self.__api_request('GET', endpoint)
response_ok = response.ok
response = self.__json_allow_dict_attrs(response.json())
issues = response.json()
return (response_ok, issues)
return response
def repos_owner_repo_issues(self, owner, repo, state=None, labels=None, q=None, issue_type=None, milestones=None, since=None, before=None, created_by=None, assigned_by=None,
mentioned_by=None, page=None, limit=None):
@ -168,11 +194,9 @@ class Gitea:
response = self.__api_request('GET', endpoint)
response_ok = response.ok
response = self.__json_allow_dict_attrs(response.json())
notifications = response.json()
return (response_ok, notifications)
return response
def repos_owner_repo_issues_comments(self, owner, repo, since=None, before=None, page=None, limit=None):
"""
@ -191,11 +215,111 @@ class Gitea:
response = self.__api_request('GET', endpoint)#, data)
response_ok = response.ok
response = self.__json_allow_dict_attrs(response.json())
comments = response.json()
return response
return (response_ok, comments)
def repo_owner_get_metada(self, owner, repo, filepath):
"""
Gets the metadata and contents (if a file) of an entry in a repository, or list of entries if a dir
"""
params = dict()
params['token'] = self.access_token
formatted_params = urlencode(params)
endpoint = self.api_base_url + '/api/v1/repos/{0}/{1}/contents/{2}?{3}'.format(owner, repo, filepath, formatted_params)
response = self.__api_request('GET', endpoint)
response = self.__json_allow_dict_attrs(response.json())
return response
def repo_owner_create_file(self, owner, repo, filepath, author_email, author_name, branch, message):
"""
Create a file in a repository
"""
data = {"author":[{"email":author_email},{"name":author_name}],
"branch":branch,
"message":message
}
params = dict()
params['token'] = self.access_token
formatted_params = urlencode(params)
endpoint = self.api_base_url + '/api/v1/repos/{0}/{1}/contents/{2}?{3}'.format(owner, repo, filepath, formatted_params)
response = self.__api_request('POST', endpoint, data=data)
response = self.__json_allow_dict_attrs(response.json())
return response
def repo_owner_update_file(self, owner, repo, filepath, author_email, author_name, branch, message, sha):
"""
Update a file in a repository
"""
data = {"author":[{"email":author_email},{"name":author_name}],
"branch":branch,
"message":message,
"sha":sha
}
params = dict()
params['token'] = self.access_token
formatted_params = urlencode(params)
endpoint = self.api_base_url + '/api/v1/repos/{0}/{1}/contents/{2}?{3}'.format(owner, repo, filepath, formatted_params)
response = self.__api_request('PUT', endpoint, data=data)
response = self.__json_allow_dict_attrs(response.json())
return response
def repo_owner_delete_file(self, owner, repo, filepath, author_email, author_name, branch, message, sha):
"""
Delete a file in a repository
"""
data = {"author":[{"email":author_email},{"name":author_name}],
"branch":branch,
"commiter":[{"email":author_email},{"name":author_name}],
"message":message,
"new_branch":branch,
'sha':sha
}
params = dict()
params['token'] = self.access_token
formatted_params = urlencode(params)
endpoint = self.api_base_url + '/api/v1/repos/{0}/{1}/contents/{2}?{3}'.format(owner, repo, filepath, formatted_params)
response = self.__api_request('DELETE', endpoint, data=data)
response = self.__json_allow_dict_attrs(response.json())
return response
def user(self):
"""
Get the authenticated user
"""
params = dict()
params['token'] = self.access_token
formatted_params = urlencode(params)
endpoint = self.api_base_url + '/api/v1/user?{0}'.format(formatted_params)
response = self.__api_request('GET', endpoint)
response = self.__json_allow_dict_attrs(response.json())
return response
@staticmethod
def __check_setup(self):
@ -304,6 +428,16 @@ class Gitea:
return response
@staticmethod
def __json_allow_dict_attrs(json_object):
"""
Makes it possible to use attribute notation to access a dicts
elements, while still allowing the dict to act as a dict.
"""
if isinstance(json_object, dict):
return AttribAccessDict(json_object)
return json_objecte
##
# Exceptions
##

View file

@ -15,10 +15,16 @@ Python Wrapper for Gitea API (WIP)
gitea.admin_users_create()
gitea.admin_users_list()
gitea.notifications_new()
gitea.notifications_new()
gitea.repos_get_repo()
gitea.repos_issues_search()
gitea.repos_owner_repo_issues()
gitea.repos_owner_repo_issues_comments()
gitea.repo_owner_get_metada()
gitea.repo_owner_create_file()
gitea.repo_owner_update_file()
gitea.repo_owner_delete_file()
gitea.user()
Some of above methods will need required params that will be shown to you when running them. In ex.: