-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgitlabAPIExec.py
More file actions
executable file
·54 lines (47 loc) · 1.27 KB
/
gitlabAPIExec.py
File metadata and controls
executable file
·54 lines (47 loc) · 1.27 KB
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
#!/usr/bin/env python
import urllib2
import urllib
import sys
import json
import os
url = "http://localhost/api/v3/";
token = "Eqj42zBo7QYyNQ6UaWxc";
if len(sys.argv) < 2:
sys.exit('No HTTP method passed')
method = sys.argv[1]
handler = urllib2.HTTPHandler()
opener = urllib2.build_opener(handler)
if len(sys.argv) < 3:
sys.exit('No API method passed')
url += sys.argv[2]
data = ''
if len(sys.argv) > 3:
try:
sys.argv[3] = json.loads(sys.argv[3])
data = urllib.urlencode(sys.argv[3])
url += "?"+data
except:
pass
request = urllib2.Request(url)
request.add_header("Content-Type",'application/json')
request.add_header('PRIVATE-TOKEN', token)
request.get_method = lambda: method.upper()
error_message = ''
try:
connection = opener.open(request)
except urllib2.HTTPError,e:
connection = e
error_message = e.read()
if connection.code < 299:
data = connection.read()
try:
data = json.loads(data)
print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
print "HTTP status is: " + str(connection.code)
print "----------------------------------------"
print json.dumps(data, indent=4, sort_keys=True)
except:
print data
else:
print 'Error: ' + str(connection)
print error_message