set user-agent header

This commit is contained in:
Morgan McMillian 2021-07-12 22:17:26 -07:00
parent a5e32f9a91
commit 438e5b5e2c

21
srht.py
View file

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