-
Notifications
You must be signed in to change notification settings - Fork 0
/
nexus-api.py
76 lines (60 loc) · 2.35 KB
/
nexus-api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/python3
#
# Author: Artemie Jurgenson (ajurgenson@sonatype.com)
# This script serves as a generic request wrapper for the nexus repo API.
import os, sys, requests, json
import config
# Parser for command line flags
def get_parser():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-r', '--repo-url',
action='store',
default='http://localhost:8081')
parser.add_argument('-e', '--endpoint',
action='store', required=True,
help='the api endpoint e.g. \'tags/associate/{tagName}\'')
parser.add_argument('-t', '--request-type',
action='store', required=True,
choices=config.REQ_TYPES,
help='the type of your http request: get, post, put, delete')
return parser
# Makes the different types of requests
def make_request(req_type, url):
if req_type == 'get':
return requests.get(url, auth=config.AUTH)
elif req_type == 'post':
return requests.post(url, auth=config.AUTH)
elif req_type == 'delete':
return requests.delete(url, auth=config.AUTH)
elif req_type == 'put':
return requests.put(url, auth=config.AUTH)
else:
raise Exception('No valid http requests type', req_type)
def main(argv):
r = make_request(argv.request_type, argv.repo_url + config.BASE_URL + argv.endpoint)
# Checks for request errors
try:
r.raise_for_status()
except requests.exceptions.Timeout:
print('Request timed out - retrying...')
r = make_request(argv.request_type, argv.repo_url + config.BASE_URL + argv.endpoint)
except requests.exceptions.TooManyRedirects:
print('Bad url, use a different one.')
sys.exit(1)
except requests.exceptions.RequestException as e:
print(e)
sys.exit(1)
# prints headers and status
status = r.status_code
print('Status:', status)
print(r.headers)
# checks if there's content before dumping to json and printing/piping to file
if status != 204:
r_json = json.dumps(r.json(), indent=4)
print(r_json)
f = open('response.json', 'w+')
f.write(r_json)
f.close()
if __name__ == "__main__":
main(get_parser().parse_args(sys.argv[1:]))