POST request body with json format
This commit is contained in:
pare
bc4a481f6e
commit
37304e0f1e
S'han modificat 2 arxius amb 64 adicions i 24 eliminacions
87
Forgejo.py
87
Forgejo.py
|
@ -1,7 +1,10 @@
|
|||
import os
|
||||
import sys
|
||||
import requests
|
||||
from getpass import getpass
|
||||
from requests.models import urlencode
|
||||
import json
|
||||
import pdb
|
||||
|
||||
###
|
||||
# Dict helper class.
|
||||
|
@ -43,9 +46,10 @@ class Forgejo:
|
|||
else:
|
||||
self.session = requests.Session()
|
||||
|
||||
def admin_users_create(self, email, username, passwd, full_name=None, must_change_password=True, restricted=False, send_notify=True, source_id='0', visibility='private'):
|
||||
def admin_users_create(self, email, username, passwd, full_name=None, must_change_password=True, restricted=False, send_notify=True, source_id=0, visibility="public"):
|
||||
|
||||
data = {'email':email,
|
||||
data = {
|
||||
'email':email,
|
||||
'full_name':username,
|
||||
'login_name':username,
|
||||
'must_change_password':must_change_password,
|
||||
|
@ -55,9 +59,9 @@ class Forgejo:
|
|||
'source_id':source_id,
|
||||
'username':username,
|
||||
'visibility':visibility
|
||||
}
|
||||
}
|
||||
|
||||
endpoint = self.api_base_url + '/api/v1/admin/users?access_token={0}'.format(self.access_token)
|
||||
endpoint = self.api_base_url + '/api/v1/admin/users?token={0}'.format(self.access_token)
|
||||
|
||||
response = self.__api_request('POST', endpoint, data)
|
||||
|
||||
|
@ -162,10 +166,10 @@ class Forgejo:
|
|||
|
||||
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):
|
||||
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):
|
||||
"""
|
||||
if since or before are specified, they must have following format: in. ex. 2022-08-13T08:09:07+02:00
|
||||
List a repository's issues
|
||||
If since or before are specified, they must have following format: in. ex. 2022-08-13T08:09:07+02:00
|
||||
"""
|
||||
params = dict()
|
||||
|
||||
|
@ -200,6 +204,22 @@ class Forgejo:
|
|||
|
||||
return response
|
||||
|
||||
def repos_owner_repo_issues_pinned(self, owner, repo):
|
||||
"""
|
||||
List a repo's pinned isssues
|
||||
"""
|
||||
params = dict()
|
||||
|
||||
formatted_params = urlencode(params)
|
||||
|
||||
endpoint = self.api_base_url + '/api/v1/repos/{0}/{1}/issues?{2}'.format(owner, repo, formatted_params)
|
||||
|
||||
response = self.__api_request('GET', endpoint)
|
||||
|
||||
response = self.__json_allow_dict_attrs(response.json())
|
||||
|
||||
return response
|
||||
|
||||
def repos_owner_repo_issues_comments(self, owner, repo, since=None, before=None, page=None, limit=None):
|
||||
"""
|
||||
if since or before are specified, they must have following format: in. ex. 2022-08-13T08:09:07+02:00
|
||||
|
@ -221,7 +241,7 @@ class Forgejo:
|
|||
|
||||
return response
|
||||
|
||||
def repo_owner_get_metada(self, owner, repo, filepath):
|
||||
def repo_owner_get_metadata(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
|
||||
"""
|
||||
|
@ -238,13 +258,19 @@ class Forgejo:
|
|||
|
||||
return response
|
||||
|
||||
def repo_owner_create_file(self, owner, repo, filepath, author_email, author_name, branch, message):
|
||||
def repo_owner_create_file(self, owner, repo, filepath, author_email, author_name, branch, content, message):
|
||||
"""
|
||||
Create a file in a repository
|
||||
"""
|
||||
|
||||
data = {"author":[{"email":author_email},{"name":author_name}],
|
||||
content = json.dumps(content.decode('UTF-8')).replace('"','')
|
||||
|
||||
data = {"author":{
|
||||
"email":author_email,
|
||||
"name":author_name
|
||||
},
|
||||
"branch":branch,
|
||||
"content":content,
|
||||
"message":message
|
||||
}
|
||||
|
||||
|
@ -254,18 +280,24 @@ class Forgejo:
|
|||
|
||||
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.__api_request('POST', endpoint, 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):
|
||||
def repo_owner_update_file(self, owner, repo, filepath, author_email, author_name, branch, content, message, sha):
|
||||
"""
|
||||
Update a file in a repository
|
||||
"""
|
||||
data = {"author":[{"email":author_email},{"name":author_name}],
|
||||
content = json.dumps(content.decode('UTF-8')).replace('"','')
|
||||
|
||||
data = {"author":{
|
||||
"email":author_email,
|
||||
"name":author_name
|
||||
},
|
||||
"branch":branch,
|
||||
"content":content,
|
||||
"message":message,
|
||||
"sha":sha
|
||||
}
|
||||
|
@ -276,7 +308,7 @@ class Forgejo:
|
|||
|
||||
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.__api_request('PUT', endpoint, data)
|
||||
|
||||
response = self.__json_allow_dict_attrs(response.json())
|
||||
|
||||
|
@ -286,9 +318,15 @@ class Forgejo:
|
|||
"""
|
||||
Delete a file in a repository
|
||||
"""
|
||||
data = {"author":[{"email":author_email},{"name":author_name}],
|
||||
data = {"author":{
|
||||
"email":author_email,
|
||||
"name":author_name
|
||||
},
|
||||
"branch":branch,
|
||||
"commiter":[{"email":author_email},{"name":author_name}],
|
||||
"commiter":{
|
||||
"email":author_email,
|
||||
"name":author_name
|
||||
},
|
||||
"message":message,
|
||||
"new_branch":branch,
|
||||
'sha':sha
|
||||
|
@ -300,7 +338,7 @@ class Forgejo:
|
|||
|
||||
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.__api_request('DELETE', endpoint, data)
|
||||
|
||||
response = self.__json_allow_dict_attrs(response.json())
|
||||
|
||||
|
@ -342,7 +380,7 @@ class Forgejo:
|
|||
os.makedirs('config')
|
||||
|
||||
self.api_base_url = input("Forgejo API base url, in ex. 'https://yourforgejo.instance': ")
|
||||
self.access_token = input("Forgejo access token: ")
|
||||
self.access_token = getpass("Forgejo access token: ")
|
||||
|
||||
if not os.path.exists(self.__forgejo_config_path):
|
||||
with open(self.__forgejo_config_path, 'w'): pass
|
||||
|
@ -350,7 +388,7 @@ class Forgejo:
|
|||
|
||||
with open(self.__forgejo_config_path, 'a') as the_file:
|
||||
print("Writing forgejo parameters to " + self.__forgejo_config_path)
|
||||
the_file.write(f'api_base_url: {self.api_base_url}\n'+f'access_token: {self.access_token}\n')
|
||||
the_file.write(f'api_base_url: {self.api_base_url}\n'+f'access_token: {self.access_token}')
|
||||
|
||||
return (self.api_base_url, self.access_token)
|
||||
|
||||
|
@ -367,13 +405,16 @@ class Forgejo:
|
|||
|
||||
def __api_request(self, method, endpoint, data={}):
|
||||
|
||||
headers = {
|
||||
'Accept':'application/json',
|
||||
'Content-Type':'application/json'
|
||||
}
|
||||
|
||||
response = None
|
||||
|
||||
try:
|
||||
|
||||
kwargs = dict(data=data)
|
||||
|
||||
response = self.session.request(method, url = endpoint, **kwargs)
|
||||
response = self.session.request(method, url = endpoint, headers=headers, json=data)
|
||||
|
||||
except Exception as e:
|
||||
|
||||
|
@ -438,7 +479,7 @@ class Forgejo:
|
|||
"""
|
||||
if isinstance(json_object, dict):
|
||||
return AttribAccessDict(json_object)
|
||||
return json_objecte
|
||||
return json_object
|
||||
|
||||
##
|
||||
# Exceptions
|
||||
|
|
|
@ -7,7 +7,6 @@ import os.path
|
|||
import string
|
||||
import secrets
|
||||
import requests
|
||||
import pdb
|
||||
|
||||
def cleanhtml(raw_html):
|
||||
cleanr = re.compile('<.*?>')
|
||||
|
|
Loading…
Referencia en una nova incidència