Skip to content

Instantly share code, notes, and snippets.

@martindurant
Created March 15, 2017 22:24
Show Gist options
  • Save martindurant/02ef00b32aeaf2a7cff36247a323438d to your computer and use it in GitHub Desktop.
Save martindurant/02ef00b32aeaf2a7cff36247a323438d to your computer and use it in GitHub Desktop.
gitea endpoints
import requests
class GiTea(object):
base_url = 'http://127.0.0.1:3000'
def __init__(self, user, pword, base_url=None):
if base_url is not None:
self.base_url = base_url
self.user = user
r0 = requests.get(self.base_url+'/user/login')
r = requests.post(self.base_url+'/user/login',
data={'user_name': user, 'password': pword,
'_csrf': r0.cookies['_csrf']},
cookies=r0.cookies)
self.tea = r0.cookies['i_like_gitea']
self.csrf = r.cookies['_csrf']
self.head = {'Cookie': 'lang=en-US; i_like_gitea='+
self.tea+'; _csrf='+self.csrf,
'Host': self.base_url.split('/', 2)[-1], "Origin": 'null',
'User-Agent': "Python",
'Content-Type': 'application/x-www-form-urlencoded'}
def create_repo(self, name):
r2 = requests.post(
self.base_url+'/repo/create',
headers=self.head,
data="_csrf="+self.csrf+
"&uid=1&repo_name={}&description=&gitignores=&"
"license=&readme=Default".format(name),
allow_redirects=False)
assert r2.status_code == 302
def delete_repo(self, name):
r2 = requests.post(
self.base_url+'/{}/{}/settings'.format(self.user, name),
headers=self.head,
data="_csrf="+self.csrf+
"&repo_name={}&action=delete".format(name),
allow_redirects=False)
assert r2.status_code == 302
def download_file(self, repo, path, branch='master'):
url = '/'.join([self.base_url, self.user, repo, 'raw', branch, path])
head = self.head.copy()
head['Content-Type'] = 'x-application/octet-stream'
r = requests.get(url, headers=head)
if not r.ok:
raise IOError('Download Failed: %s' % r.reason)
return r.raw.read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment