From 438e5b5e2c5a46ec73a02a455de1031ff0b9e53e Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Mon, 12 Jul 2021 22:17:26 -0700 Subject: [PATCH] set user-agent header --- srht.py | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/srht.py b/srht.py index 5ec42f7..68e6856 100644 --- a/srht.py +++ b/srht.py @@ -32,6 +32,7 @@ PASTE_VISIBILITY = [ access_token = os.environ["SOURCEHUT_CLI_ACCESS_TOKEN"] username = "~"+os.environ["USER"] base_url = "" +headers = {'User-Agent': "~thrrgilag srht-cli"} @click.group() @click.option('--user') @@ -40,10 +41,12 @@ base_url = "" def cli(user, token): global access_token global username + global headers if token is not None: access_token = token if user is not None: username = "~"+user + headers['Authorization'] = "Bearer " + access_token @cli.group() def paste(): @@ -55,7 +58,6 @@ def paste(): def get_pastes(): '''List pastes''' url = base_url + f"/pastes" - headers = {'Authorization': "Bearer " + access_token} r = requests.get(url, headers=headers) if r.status_code == 200: output.show_pastes(r.json()['results']) @@ -68,7 +70,6 @@ def get_pastes(): def show_paste(sha): '''Show details of a paste''' url = base_url + f"/pastes/{sha}" - headers = {'Authorization': "Bearer " + access_token} r = requests.get(url, headers=headers) if r.status_code == 200: paste = r.json() @@ -95,7 +96,6 @@ def show_paste(sha): def show_blob(sha): '''Show paste blob''' url = base_url + f"/blobs/{sha}" - headers = {'Authorization': "Bearer " + access_token} r = requests.get(url, headers=headers) if r.status_code == 200: #output.show_pastes(r.json()['results']) @@ -116,7 +116,6 @@ def submit_ticket(visibility, filename): return url = base_url + f"/pastes" - headers = {'Authorization': "Bearer " + access_token} payload = {'visibility': visibility} blob = {'contents': content} if filename is not None: @@ -132,7 +131,6 @@ def submit_ticket(visibility, filename): def delete_paste(sha): '''Delete a paste''' url = base_url + f"/pastes/{sha}" - headers = {'Authorization': "Bearer " + access_token} r = requests.delete(url, headers=headers) print(r.status_code) print(r.text) @@ -148,7 +146,6 @@ def git(): def get_repos(verbose): '''List repositories''' url = base_url + f"/{username}/repos" - headers = {'Authorization': "Bearer " + access_token} r = requests.get(url, headers=headers) if r.status_code == 200: output.show_repos(r.json()['results'], verbose) @@ -161,7 +158,6 @@ def get_repos(verbose): def get_git_webhooks(repo): '''List webhooks for a repository''' url = base_url + f"/{username}/repos/{repo}/webhooks" - headers = {'Authorization': "Bearer " + access_token} r = requests.get(url, headers=headers) if r.status_code == 200: print(json.dumps(r.json(), indent=4)) @@ -177,7 +173,6 @@ def add_git_webhook(repo, url, event): '''Add webhook to a repository''' payload = {'url': url, 'events': event} url = base_url + f"/{username}/repos/{repo}/webhooks" - headers = {'Authorization': "Bearer " + access_token} r = requests.post(url, headers=headers, json=payload) print(r.status_code) print(r.text) @@ -188,7 +183,6 @@ def add_git_webhook(repo, url, event): def del_git_webhook(repo, hookid): '''Delete webhook from a repository''' url = base_url + f"/{username}/repos/{repo}/webhooks/{hookid}" - headers = {'Authorization': "Bearer " + access_token} r = requests.delete(url, headers=headers) print(r.status_code) print(r.text) @@ -205,7 +199,6 @@ def todo(): def show_tickets(tracker, closed): '''List tickets on a tracker''' url = base_url + f"/user/{username}/trackers/{tracker}/tickets" - headers = {'Authorization': "Bearer " + access_token} r = requests.get(url, headers=headers) if r.status_code == 200: output.show_tickets(r.json()['results'], closed) @@ -226,7 +219,6 @@ def submit_ticket(tracker): description = description.lstrip() url = base_url + f"/user/{username}/trackers/{tracker}/tickets" - headers = {'Authorization': "Bearer " + access_token} payload = {'title': title, 'description': description} r = requests.post(url, headers=headers, json=payload) print(r.status_code) @@ -266,7 +258,6 @@ def comment(tracker, ticketid, status, resolution, label): print(payload) url = base_url + f"/user/{username}/trackers/{tracker}/tickets/{ticketid}" - headers = {'Authorization': "Bearer " + access_token} r = requests.put(url, headers=headers, json=payload) print(r.status_code) print(r.text) @@ -276,7 +267,6 @@ def comment(tracker, ticketid, status, resolution, label): @click.argument('ticketid') def show_ticket(tracker, ticketid): '''Show details of a ticket on a tracker''' - headers = {'Authorization': "Bearer " + access_token} url = base_url + f"/user/{username}/trackers/{tracker}/tickets/{ticketid}" r = requests.get(url, headers=headers) @@ -301,7 +291,6 @@ def show_ticket(tracker, ticketid): def get_trackers(verbose): '''List trackers''' url = base_url + f"/user/{username}/trackers" - headers = {'Authorization': "Bearer " + access_token} r = requests.get(url, headers=headers) if r.status_code == 200: output.show_trackers(r.json()['results'], verbose) @@ -314,7 +303,6 @@ def get_trackers(verbose): def get_labels(tracker): '''List labels for a tracker''' url = base_url + f"/user/{username}/trackers/{tracker}/labels" - headers = {'Authorization': "Bearer " + access_token} r = requests.get(url, headers=headers) if r.status_code == 200: output.show_labels(r.json()['results']) @@ -327,7 +315,6 @@ def get_labels(tracker): def get_tracker_webhooks(tracker): '''List webhooks for a tracker''' url = base_url + f"/user/{username}/trackers/{tracker}/webhooks" - headers = {'Authorization': "Bearer " + access_token} r = requests.get(url, headers=headers) if r.status_code == 200: print(json.dumps(r.json(), indent=4)) @@ -341,7 +328,6 @@ def get_tracker_webhooks(tracker): def del_tacker_webhook(tracker, hookid): '''Delete webhook from a tracker''' url = base_url + f"/user/{username}/trackers/{tracker}/webhooks/{hookid}" - headers = {'Authorization': "Bearer " + access_token} r = requests.delete(url, headers=headers) print(r.status_code) print(r.text) @@ -354,7 +340,6 @@ def add_tacker_webhook(tracker, url, event): '''Add webhook to a tracker''' payload = {'url': url, 'events': event} url = base_url + f"/user/{username}/trackers/{tracker}/webhooks" - headers = {'Authorization': "Bearer " + access_token} r = requests.post(url, headers=headers, json=payload) print(r.status_code) print(r.text)