|
a |
|
b/osp-api.py |
|
|
1 |
"""
|
|
|
2 |
A quick example on how to use the Allura REST API
|
|
|
3 |
"""
|
|
|
4 |
|
|
|
5 |
|
|
|
6 |
import requests
|
|
|
7 |
import json
|
|
|
8 |
from urlparse import urljoin
|
|
|
9 |
BASE = 'http://opensourceprojects.eu/rest/'
|
|
|
10 |
|
|
|
11 |
class ApiException(Exception):
|
|
|
12 |
pass
|
|
|
13 |
|
|
|
14 |
class Api:
|
|
|
15 |
"A quick helper class to call the api"
|
|
|
16 |
|
|
|
17 |
def __init__(self, baseurl=BASE):
|
|
|
18 |
self._baseurl = baseurl
|
|
|
19 |
|
|
|
20 |
def project(self, projname, neigh='p'):
|
|
|
21 |
"""
|
|
|
22 |
Retrieve project info
|
|
|
23 |
"""
|
|
|
24 |
|
|
|
25 |
url = urljoin(self._baseurl, '%s/%s' % (neigh, projname))
|
|
|
26 |
r = requests.get(url)
|
|
|
27 |
if r.status_code != 200:
|
|
|
28 |
raise ApiException('Error making API call to %s' %url)
|
|
|
29 |
if r.headers.get('content-type', '') != 'application/json; charset=utf-8':
|
|
|
30 |
raise ApiException('Expecting a json document but failed at %s' %url)
|
|
|
31 |
|
|
|
32 |
return r.json()
|
|
|
33 |
|
|
|
34 |
def fetch(self, projname, mount, neigh='p'):
|
|
|
35 |
"""
|
|
|
36 |
A helper method to fetch from a tool in a
|
|
|
37 |
certain mount point(path)
|
|
|
38 |
"""
|
|
|
39 |
url = urljoin(self._baseurl, '%s/%s/%s' % (neigh, projname,mount))
|
|
|
40 |
r = requests.get(url)
|
|
|
41 |
if r.status_code != 200:
|
|
|
42 |
raise ApiException('Error making API call to %s' % url)
|
|
|
43 |
if r.headers.get('content-type', '') != 'application/json; charset=utf-8':
|
|
|
44 |
raise ApiException('Expecting a json document but failed at %s' %url)
|
|
|
45 |
return r.json()
|
|
|
46 |
|
|
|
47 |
if __name__ == '__main__':
|
|
|
48 |
|
|
|
49 |
api = Api()
|
|
|
50 |
resp = api.project('osp')
|
|
|
51 |
print('Project Name: ' + resp['name'])
|
|
|
52 |
print('Tools in this project:')
|
|
|
53 |
|
|
|
54 |
for t in resp['tools']:
|
|
|
55 |
print( ' %s:%s' % (t['name'], t['label']))
|
|
|
56 |
|
|
|
57 |
try:
|
|
|
58 |
toolinfo = api.fetch(resp['name'], t['mount_point'] )
|
|
|
59 |
except ApiException:
|
|
|
60 |
print(' Unable to retrieve data for this tool')
|
|
|
61 |
continue
|
|
|
62 |
|
|
|
63 |
if t['name'] == 'wiki':
|
|
|
64 |
# If the tool is wiki, we can
|
|
|
65 |
# fetch the page list
|
|
|
66 |
if 'pages' in toolinfo:
|
|
|
67 |
print(' ' + ','.join(toolinfo['pages']))
|
|
|
68 |
elif t['name'] == 'tickets':
|
|
|
69 |
# The ticket tool provides a list of tickets
|
|
|
70 |
# I'm just going to print a ticket count
|
|
|
71 |
print(' Ticket count: %d' % toolinfo['count'])
|
|
|
72 |
elif t['name'] == 'git':
|
|
|
73 |
print(' Commit count: %d' % toolinfo['commit_count'])
|
|
|
74 |
else:
|
|
|
75 |
#
|
|
|
76 |
# I don't know the details for the the other tools
|
|
|
77 |
#
|
|
|
78 |
print(' ' + toolinfo)
|
|
|
79 |
|
|
|
80 |
|