Skip to content

Commit c987bc8

Browse files
committed
Support manual requests
1 parent b6c26cb commit c987bc8

File tree

1 file changed

+99
-42
lines changed

1 file changed

+99
-42
lines changed

gogs_client/interface.py

Lines changed: 99 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ def authenticated_user(self, auth):
4141
:raises NetworkFailure: if there is an error communicating with the server
4242
:raises ApiFailure: if the request cannot be serviced
4343
"""
44-
response = self._get("/user", auth=auth)
45-
return GogsUser.from_json(self._check_ok(response).json())
44+
response = self.get("/user", auth=auth)
45+
return GogsUser.from_json(response.json())
4646

4747
def get_tokens(self, auth, username=None):
4848
"""
@@ -61,8 +61,8 @@ def get_tokens(self, auth, username=None):
6161
"""
6262
if username is None:
6363
username = self.authenticated_user(auth).username
64-
response = self._get("/users/{u}/tokens".format(u=username), auth=auth)
65-
return [Token.from_json(o) for o in self._check_ok(response).json()]
64+
response = self.get("/users/{u}/tokens".format(u=username), auth=auth)
65+
return [Token.from_json(o) for o in response.json()]
6666

6767
def create_token(self, auth, name, username=None):
6868
"""
@@ -83,8 +83,8 @@ def create_token(self, auth, name, username=None):
8383
if username is None:
8484
username = self.authenticated_user(auth).username
8585
data = {"name": name}
86-
response = self._post("/users/{u}/tokens".format(u=username), auth=auth, data=data)
87-
return Token.from_json(self._check_ok(response).json())
86+
response = self.post("/users/{u}/tokens".format(u=username), auth=auth, data=data)
87+
return Token.from_json(response.json())
8888

8989
def ensure_token(self, auth, name, username=None):
9090
"""
@@ -143,8 +143,8 @@ def create_repo(self, auth, name, description=None, private=False, auto_init=Fal
143143
}
144144
data = {k: v for (k, v) in data.items() if v is not None}
145145
url = "/org/{0}/repos".format(organization) if organization else "/user/repos"
146-
response = self._post(url, auth=auth, data=data)
147-
return GogsRepo.from_json(self._check_ok(response).json())
146+
response = self.post(url, auth=auth, data=data)
147+
return GogsRepo.from_json(response.json())
148148

149149
def repo_exists(self, auth, username, repo_name):
150150
"""
@@ -175,7 +175,7 @@ def get_repo(self, auth, username, repo_name):
175175
:raises ApiFailure: if the request cannot be serviced
176176
"""
177177
path = "/repos/{u}/{r}".format(u=username, r=repo_name)
178-
response = self._check_ok(self._get(path, auth=auth))
178+
response = self.get(path, auth=auth)
179179
return GogsRepo.from_json(response.json())
180180

181181
def get_user_repos(self, auth, username):
@@ -191,7 +191,7 @@ def get_user_repos(self, auth, username):
191191
:raises ApiFailure: if the request cannot be serviced
192192
"""
193193
path = "/users/{u}/repos".format(u=username)
194-
response = self._check_ok(self._get(path, auth=auth))
194+
response = self.get(path, auth=auth)
195195
return [GogsRepo.from_json(repo_json) for repo_json in response.json()]
196196

197197
def get_branch(self, auth, username, repo_name, branch_name):
@@ -209,7 +209,7 @@ def get_branch(self, auth, username, repo_name, branch_name):
209209
:raises ApiFailure: if the request cannot be serviced
210210
"""
211211
path = "/repos/{u}/{r}/branches/{b}".format(u=username, r=repo_name, b=branch_name)
212-
response = self._check_ok(self._get(path, auth=auth))
212+
response = self.get(path, auth=auth)
213213
return GogsBranch.from_json(response.json())
214214

215215
def get_branches(self, auth, username, repo_name):
@@ -226,7 +226,7 @@ def get_branches(self, auth, username, repo_name):
226226
:raises ApiFailure: if the request cannot be serviced
227227
"""
228228
path = "/repos/{u}/{r}/branches".format(u=username, r=repo_name)
229-
response = self._check_ok(self._get(path, auth=auth))
229+
response = self.get(path, auth=auth)
230230
return [GogsBranch.from_json(branch_json) for branch_json in response.json()]
231231

232232
def delete_repo(self, auth, username, repo_name):
@@ -240,7 +240,7 @@ def delete_repo(self, auth, username, repo_name):
240240
:raises ApiFailure: if the request cannot be serviced
241241
"""
242242
path = "/repos/{u}/{r}".format(u=username, r=repo_name)
243-
self._check_ok(self._delete(path, auth=auth))
243+
self.delete(path, auth=auth)
244244

245245
def migrate_repo(self, auth, clone_addr,
246246
uid, repo_name, auth_username=None, auth_password=None,
@@ -273,8 +273,8 @@ def migrate_repo(self, auth, clone_addr,
273273
}
274274
data = {k: v for (k, v) in data.items() if v is not None}
275275
url = "/repos/migrate"
276-
response = self._post(url, auth=auth, data=data)
277-
return GogsRepo.from_json(self._check_ok(response).json())
276+
response = self.post(url, auth=auth, data=data)
277+
return GogsRepo.from_json(response.json())
278278

279279
def create_user(self, auth, login_name, username, email, password, send_notify=False):
280280
"""
@@ -300,8 +300,7 @@ def create_user(self, auth, login_name, username, email, password, send_notify=F
300300
"password": password,
301301
"send_notify": send_notify
302302
}
303-
response = self._post("/admin/users", auth=auth, data=data)
304-
self._check_ok(response)
303+
response = self.post("/admin/users", auth=auth, data=data)
305304
return GogsUser.from_json(response.json())
306305

307306
def user_exists(self, username):
@@ -330,7 +329,7 @@ def search_users(self, username_keyword, limit=10):
330329
:raises ApiFailure: if the request cannot be serviced
331330
"""
332331
params = {"q": username_keyword, "limit": limit}
333-
response = self._check_ok(self._get("/users/search", params=params))
332+
response = self.get("/users/search", params=params)
334333
return [GogsUser.from_json(user_json) for user_json in response.json()["data"]]
335334

336335
def get_user(self, auth, username):
@@ -345,7 +344,7 @@ def get_user(self, auth, username):
345344
:raises ApiFailure: if the request cannot be serviced
346345
"""
347346
path = "/users/{}".format(username)
348-
response = self._check_ok(self._get(path, auth=auth))
347+
response = self.get(path, auth=auth)
349348
return GogsUser.from_json(response.json())
350349

351350
def update_user(self, auth, username, update):
@@ -361,7 +360,7 @@ def update_user(self, auth, username, update):
361360
:raises ApiFailure: if the request cannot be serviced
362361
"""
363362
path = "/admin/users/{}".format(username)
364-
response = self._check_ok(self._patch(path, auth=auth, data=update.as_dict()))
363+
response = self.patch(path, auth=auth, data=update.as_dict())
365364
return GogsUser.from_json(response.json())
366365

367366
def delete_user(self, auth, username):
@@ -373,7 +372,7 @@ def delete_user(self, auth, username):
373372
:param str username: username of user to delete
374373
"""
375374
path = "/admin/users/{}".format(username)
376-
self._check_ok(self._delete(path, auth=auth))
375+
self.delete(path, auth=auth)
377376

378377
def get_repo_hooks(self, auth, username, repo_name):
379378
"""
@@ -389,7 +388,7 @@ def get_repo_hooks(self, auth, username, repo_name):
389388
:raises ApiFailure: if the request cannot be serviced
390389
"""
391390
path = "/repos/{u}/{r}/hooks".format(u=username, r=repo_name)
392-
response = self._check_ok(self._get(path, auth=auth))
391+
response = self.get(path, auth=auth)
393392
return [GogsRepo.Hook.from_json(hook) for hook in response.json()]
394393

395394
def create_hook(self, auth, repo_name, hook_type, config, events=None, organization=None, active=False):
@@ -421,8 +420,7 @@ def create_hook(self, auth, repo_name, hook_type, config, events=None, organizat
421420

422421
url = "/repos/{o}/{r}/hooks".format(o=organization, r=repo_name) if organization is not None \
423422
else "/repos/{r}/hooks".format(r=repo_name)
424-
response = self._post(url, auth=auth, data=data)
425-
self._check_ok(response)
423+
response = self.post(url, auth=auth, data=data)
426424
return GogsRepo.Hook.from_json(response.json())
427425

428426
def update_hook(self, auth, repo_name, hook_id, update, organization=None):
@@ -443,7 +441,7 @@ def update_hook(self, auth, repo_name, hook_id, update, organization=None):
443441
path = "/repos/{o}/{r}/hooks/{i}".format(o=organization, r=repo_name, i=hook_id)
444442
else:
445443
path = "/repos/{r}/hooks/{i}".format(r=repo_name, i=hook_id)
446-
response = self._check_ok(self._patch(path, auth=auth, data=update.as_dict()))
444+
response = self._patch(path, auth=auth, data=update.as_dict())
447445
return GogsRepo.Hook.from_json(response.json())
448446

449447
def delete_hook(self, auth, username, repo_name, hook_id):
@@ -459,7 +457,7 @@ def delete_hook(self, auth, username, repo_name, hook_id):
459457
:raises ApiFailure: if the request cannot be serviced
460458
"""
461459
path = "/repos/{u}/{r}/hooks/{i}".format(u=username, r=repo_name, i=hook_id)
462-
self._check_ok(self._delete(path, auth=auth))
460+
self.delete(path, auth=auth)
463461

464462
def create_organization(self, auth, owner_name, org_name, full_name=None, description=None,
465463
website=None, location=None):
@@ -487,8 +485,7 @@ def create_organization(self, auth, owner_name, org_name, full_name=None, descri
487485
}
488486

489487
url = "/admin/users/{u}/orgs".format(u=owner_name)
490-
response = self._post(url, auth=auth, data=data)
491-
self._check_ok(response)
488+
response = self.post(url, auth=auth, data=data)
492489
return GogsOrg.from_json(response.json())
493490

494491
def create_organization_team(self, auth, org_name, name, description=None, permission="read"):
@@ -512,8 +509,7 @@ def create_organization_team(self, auth, org_name, name, description=None, permi
512509
}
513510

514511
url = "/admin/orgs/{o}/teams".format(o=org_name)
515-
response = self._post(url, auth=auth, data=data)
516-
self._check_ok(response)
512+
response = self.post(url, auth=auth, data=data)
517513
return GogsTeam.from_json(response.json())
518514

519515
def add_team_membership(self, auth, team_id, username):
@@ -527,7 +523,7 @@ def add_team_membership(self, auth, team_id, username):
527523
:raises ApiFailure: if the request cannot be serviced
528524
"""
529525
url = "/admin/teams/{t}/members/{u}".format(t=team_id, u=username)
530-
self._check_ok(self._put(url, auth=auth))
526+
self.put(url, auth=auth)
531527

532528
def remove_team_membership(self, auth, team_id, username):
533529
"""
@@ -540,7 +536,7 @@ def remove_team_membership(self, auth, team_id, username):
540536
:raises ApiFailure: if the request cannot be serviced
541537
"""
542538
url = "/admin/teams/{t}/members/{u}".format(t=team_id, u=username)
543-
self._check_ok(self._delete(url, auth=auth))
539+
self.delete(url, auth=auth)
544540

545541
def add_repo_to_team(self, auth, team_id, repo_name):
546542
"""
@@ -553,7 +549,7 @@ def add_repo_to_team(self, auth, team_id, repo_name):
553549
:raises ApiFailure: if the request cannot be serviced
554550
"""
555551
url = "/admin/teams/{t}/repos/{r}".format(t=team_id, r=repo_name)
556-
self._check_ok(self._put(url, auth=auth))
552+
self.put(url, auth=auth)
557553

558554
def remove_repo_from_team(self, auth, team_id, repo_name):
559555
"""
@@ -566,7 +562,7 @@ def remove_repo_from_team(self, auth, team_id, repo_name):
566562
:raises ApiFailure: if the request cannot be serviced
567563
"""
568564
url = "/admin/teams/{t}/repos/{r}".format(t=team_id, r=repo_name)
569-
self._check_ok(self._delete(url, auth=auth))
565+
self.delete(url, auth=auth)
570566

571567
def list_deploy_keys(self, auth, username, repo_name):
572568
"""
@@ -580,7 +576,7 @@ def list_deploy_keys(self, auth, username, repo_name):
580576
:raises NetworkFailure: if there is an error communicating with the server
581577
:raises ApiFailure: if the request cannot be serviced
582578
"""
583-
response = self._check_ok(self._get("/repos/{u}/{r}/keys".format(u=username, r=repo_name), auth=auth))
579+
response = self.get("/repos/{u}/{r}/keys".format(u=username, r=repo_name), auth=auth)
584580
return [GogsRepo.DeployKey.from_json(key_json) for key_json in response.json()]
585581

586582
def get_deploy_key(self, auth, username, repo_name, key_id):
@@ -596,8 +592,7 @@ def get_deploy_key(self, auth, username, repo_name, key_id):
596592
:raises NetworkFailure: if there is an error communicating with the server
597593
:raises ApiFailure: if the request cannot be serviced
598594
"""
599-
response = self._check_ok(
600-
self._get("/repos/{u}/{r}/keys/{k}".format(u=username, r=repo_name, k=key_id), auth=auth))
595+
response = self.get("/repos/{u}/{r}/keys/{k}".format(u=username, r=repo_name, k=key_id), auth=auth)
601596
return GogsRepo.DeployKey.from_json(response.json())
602597

603598
def add_deploy_key(self, auth, username, repo_name, title, key_content):
@@ -618,8 +613,7 @@ def add_deploy_key(self, auth, username, repo_name, title, key_content):
618613
"title": title,
619614
"key": key_content
620615
}
621-
response = self._check_ok(
622-
self._post("/repos/{u}/{r}/keys".format(u=username, r=repo_name), auth=auth, data=data))
616+
response = self.post("/repos/{u}/{r}/keys".format(u=username, r=repo_name), auth=auth, data=data)
623617
return GogsRepo.DeployKey.from_json(response.json())
624618

625619
def delete_deploy_key(self, auth, username, repo_name, key_id):
@@ -633,9 +627,7 @@ def delete_deploy_key(self, auth, username, repo_name, key_id):
633627
:raises NetworkFailure: if there is an error communicating with the server
634628
:raises ApiFailure: if the request cannot be serviced
635629
"""
636-
response = self._check_ok(
637-
self._delete("/repos/{u}/{r}/keys/{k}".format(u=username, r=repo_name, k=key_id), auth=auth))
638-
self._check_ok(response)
630+
self.delete("/repos/{u}/{r}/keys/{k}".format(u=username, r=repo_name, k=key_id), auth=auth)
639631

640632
# Helper methods
641633

@@ -647,6 +639,19 @@ def _delete(self, path, auth=None, **kwargs):
647639
except requests.RequestException as exc:
648640
raise NetworkFailure(exc)
649641

642+
def delete(self, path, auth=None, **kwargs):
643+
"""
644+
Manually make a DELETE request.
645+
646+
:param str path: relative url of the request (e.g. `/users/username`)
647+
:param auth.Authentication auth: authentication object
648+
:param kwargs dict: Extra arguments for the request, as supported by
649+
`requests <http://docs.python-requests.org/>`_ library.
650+
:raises NetworkFailure: if there is an error communicating with the server
651+
:raises ApiFailure: if the request cannot be serviced
652+
"""
653+
return self._check_ok(self._delete(path, auth=auth, **kwargs))
654+
650655
def _get(self, path, auth=None, **kwargs):
651656
if auth is not None:
652657
auth.update_kwargs(kwargs)
@@ -655,6 +660,19 @@ def _get(self, path, auth=None, **kwargs):
655660
except requests.RequestException as exc:
656661
raise NetworkFailure(exc)
657662

663+
def get(self, path, auth=None, **kwargs):
664+
"""
665+
Manually make a GET request.
666+
667+
:param str path: relative url of the request (e.g. `/users/username`)
668+
:param auth.Authentication auth: authentication object
669+
:param kwargs dict: Extra arguments for the request, as supported by
670+
`requests <http://docs.python-requests.org/>`_ library.
671+
:raises NetworkFailure: if there is an error communicating with the server
672+
:raises ApiFailure: if the request cannot be serviced
673+
"""
674+
return self._check_ok(self._get(path, auth=auth, **kwargs))
675+
658676
def _patch(self, path, auth=None, **kwargs):
659677
if auth is not None:
660678
auth.update_kwargs(kwargs)
@@ -663,6 +681,19 @@ def _patch(self, path, auth=None, **kwargs):
663681
except requests.RequestException as exc:
664682
raise NetworkFailure(exc)
665683

684+
def patch(self, path, auth=None, **kwargs):
685+
"""
686+
Manually make a PATCH request.
687+
688+
:param str path: relative url of the request (e.g. `/users/username`)
689+
:param auth.Authentication auth: authentication object
690+
:param kwargs dict: Extra arguments for the request, as supported by
691+
`requests <http://docs.python-requests.org/>`_ library.
692+
:raises NetworkFailure: if there is an error communicating with the server
693+
:raises ApiFailure: if the request cannot be serviced
694+
"""
695+
return self._check_ok(self._patch(path, auth=auth, **kwargs))
696+
666697
def _post(self, path, auth=None, **kwargs):
667698
if auth is not None:
668699
auth.update_kwargs(kwargs)
@@ -671,6 +702,19 @@ def _post(self, path, auth=None, **kwargs):
671702
except requests.RequestException as exc:
672703
raise NetworkFailure(exc)
673704

705+
def post(self, path, auth=None, **kwargs):
706+
"""
707+
Manually make a POST request.
708+
709+
:param str path: relative url of the request (e.g. `/users/username`)
710+
:param auth.Authentication auth: authentication object
711+
:param kwargs dict: Extra arguments for the request, as supported by
712+
`requests <http://docs.python-requests.org/>`_ library.
713+
:raises NetworkFailure: if there is an error communicating with the server
714+
:raises ApiFailure: if the request cannot be serviced
715+
"""
716+
return self._check_ok(self._post(path, auth=auth, **kwargs))
717+
674718
def _put(self, path, auth=None, **kwargs):
675719
if auth is not None:
676720
auth.update_kwargs(kwargs)
@@ -679,6 +723,19 @@ def _put(self, path, auth=None, **kwargs):
679723
except requests.RequestException as exc:
680724
raise NetworkFailure(exc)
681725

726+
def put(self, path, auth=None, **kwargs):
727+
"""
728+
Manually make a PUT request.
729+
730+
:param str path: relative url of the request (e.g. `/users/username`)
731+
:param auth.Authentication auth: authentication object
732+
:param kwargs dict: Extra arguments for the request, as supported by
733+
`requests <http://docs.python-requests.org/>`_ library.
734+
:raises NetworkFailure: if there is an error communicating with the server
735+
:raises ApiFailure: if the request cannot be serviced
736+
"""
737+
return self._check_ok(self._put(path, auth=auth, **kwargs))
738+
682739
@staticmethod
683740
def _check_ok(response):
684741
"""

0 commit comments

Comments
 (0)