Download this file

osp-api.py    81 lines (64 with data), 2.5 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
A quick example on how to use the Allura REST API
"""
import requests
import json
from urlparse import urljoin
BASE = 'http://opensourceprojects.eu/rest/'
class ApiException(Exception):
pass
class Api:
"A quick helper class to call the api"
def __init__(self, baseurl=BASE):
self._baseurl = baseurl
def project(self, projname, neigh='p'):
"""
Retrieve project info
"""
url = urljoin(self._baseurl, '%s/%s' % (neigh, projname))
r = requests.get(url)
if r.status_code != 200:
raise ApiException('Error making API call to %s' %url)
if r.headers.get('content-type', '') != 'application/json; charset=utf-8':
raise ApiException('Expecting a json document but failed at %s' %url)
return r.json()
def fetch(self, projname, mount, neigh='p'):
"""
A helper method to fetch from a tool in a
certain mount point(path)
"""
url = urljoin(self._baseurl, '%s/%s/%s' % (neigh, projname,mount))
r = requests.get(url)
if r.status_code != 200:
raise ApiException('Error making API call to %s' % url)
if r.headers.get('content-type', '') != 'application/json; charset=utf-8':
raise ApiException('Expecting a json document but failed at %s' %url)
return r.json()
if __name__ == '__main__':
api = Api()
resp = api.project('osp')
print('Project Name: ' + resp['name'])
print('Tools in this project:')
for t in resp['tools']:
print( ' %s:%s' % (t['name'], t['label']))
try:
toolinfo = api.fetch(resp['name'], t['mount_point'] )
except ApiException:
print(' Unable to retrieve data for this tool')
continue
if t['name'] == 'wiki':
# If the tool is wiki, we can
# fetch the page list
if 'pages' in toolinfo:
print(' ' + ','.join(toolinfo['pages']))
elif t['name'] == 'tickets':
# The ticket tool provides a list of tickets
# I'm just going to print a ticket count
print(' Ticket count: %d' % toolinfo['count'])
elif t['name'] == 'git':
print(' Commit count: %d' % toolinfo['commit_count'])
else:
#
# I don't know the details for the the other tools
#
print(' ' + toolinfo)