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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
|
__metaclass__ = type
from datetime import (
datetime,
timedelta,
)
from email.utils import parseaddr
from inspect import getargspec
import json
from os.path import (
join,
)
import re
from urllib import unquote
from gridfs.errors import NoFile
from pyramid.httpexceptions import (
HTTPFound,
HTTPNotFound,
)
from pyramid.view import view_config
from webob import Response
from charmworld.models import (
Bundle,
Charm,
CharmFileSet,
getfs,
QADataSource,
)
from charmworld.search import (
InvalidCharmType,
NegativeLimit,
)
from charmworld.utils import (
quote_key,
timestamp,
)
def json_response(status, params, headers=[]):
"""Return a JSON API response.
:param status: The HTTP status code to use.
:param params: A json-serializable value to use as the body, or None for
no body.
"""
if params is None:
body = ''
else:
body = json.dumps(params, sort_keys=True, indent=2)
return Response(body,
headerlist=[
('Content-Type', 'application/json'),
("Access-Control-Allow-Origin", "*"),
("Access-Control-Allow-Headers", "X-Requested-With"),
] + headers,
status_code=status)
@view_config(route_name="api-obsolete-0")
@view_config(route_name="api-obsolete-1")
def obsolete(request):
message = 'This API version is no longer supported.'
return Response(
message,
headerlist=[
('Access-Control-Allow-Origin', '*'),
('Access-Control-Allow-Headers', 'X-Requested-With'),
('Content-Type', 'text/plain'),
('Cache-Control', 'max-age=86400, public')
],
status_code=410)
class API2:
"""Implementation of API 2.
All methods whose names do not begin with an underscore are exposed.
Methods may return a webob Response, which is returned directly, or a
json-serializable value, which will be returned as a json HTTP response.
"""
TEST_STATUSES = ('SUCCESS', 'FAILURE', 'UNSTABLE', 'ABORTED')
ICON_PATH = '/static/img/'
def __init__(self, request):
self.request = request
def _handle(self, handler, args, kwargs):
spec = getargspec(handler)
if spec.keywords is None:
for key in set(kwargs).difference(spec.args):
return json_response(406, {
'type': 'parameter_not_supported',
'parameter': key})
return handler(*args, **kwargs)
def __call__(self):
"""Dispatch the request to the appropriate method.
Find a method with the same name as 'endpoint'. Raise HTTPNotFound if
the method doesn't exist or starts with '_'.
Return a 406 response if the query parameters are not supported by the
method.
"""
if self.request.method == 'OPTIONS':
return json_response(200, None)
endpoint = self.request.matchdict['endpoint']
if endpoint.startswith('_'):
raise HTTPNotFound(self.request.path)
handler = getattr(self, endpoint, None)
remainder = self.request.matchdict.get('remainder')
if handler is None:
raise HTTPNotFound(self.request.path)
if remainder:
args = (remainder,)
else:
args = ()
kwargs = self.request.GET.dict_of_lists()
try:
kwargs['type_'] = kwargs.pop('type')
except KeyError:
pass
result = self._handle(handler, args, kwargs)
if isinstance(result, Response):
return result
return json_response(200, result)
@staticmethod
def _get_api_id(charm):
"""Return the API id for a Mongo-formatted charm."""
return re.sub('^cs:', '', charm.store_url)
@staticmethod
def _parsed_email(address):
name, address = parseaddr(address)
return {
'name': name,
'email': address,
}
@classmethod
def _format_revision(cls, revision):
created = datetime.utcfromtimestamp(round(revision['created']))
authors = [cls._parsed_email(author)
for author in revision['authors']]
return{
'message': revision['message'],
'revno': revision['revno'],
'date': timestamp(created),
'authors': authors,
}
@classmethod
def _charm_result(cls, charm):
return {
'charm': cls._format_charm(charm),
'metadata': {
}
}
def _related_charms(self, charm_data):
charm = Charm(charm_data)
charms_provide = set(charm.i_provides)
charms_require = set(charm.i_requires)
# Swap provides and requires, so we get the charms which *require*
# what these charms provide, and the charms that *provide* what these
# charms require.
r_requires, r_provides = self.request.index_client.related_charms(
charms_provide, charms_require, series=charm.series,
exclude_name=charm.name)
f_requires = {}
for key, value in r_requires.items():
f_requires[key] = [
self._format_related(payload['data'], payload['weight'])
for payload in value]
f_provides = {}
for key, value in r_provides.items():
f_provides[key] = [
self._format_related(payload['data'], payload['weight'])
for payload in value]
return f_requires, f_provides
@classmethod
def _format_charm(cls, charm):
"""Format the charm for API consumers."""
mapping = {
'summary': 'summary',
'name': 'name',
'description': 'description',
'owner': 'owner',
'downloads': 'downloads',
'downloads_in_past_30_days': 'downloads_in_past_30_days',
'distro_series': 'series',
'revision': 'revision',
'url': 'store_url',
}
output = dict(
(key, getattr(charm, value)) for key, value in mapping.items())
tested_providers = {}
for provider, result in charm.tests.items():
if result not in cls.TEST_STATUSES:
raise ValueError('Unsupported test status: %s' % result)
tested_providers[provider] = result
maintainer = charm.maintainer
bugs_link = 'https://bugs.launchpad.net/charms/+source/%s' % charm.name
revisions = [cls._format_revision(rev) for rev in charm.changes]
output.update({
'id': cls._get_api_id(charm),
'categories': charm.categories,
'rating_numerator': 0,
'rating_denominator': 0,
'date_created': charm.date_created,
'code_source': {
'type': 'bzr',
'location': charm.bzr_branch,
'revision': str(charm.last_change['revno']),
'last_log': charm.last_change['message'],
'bugs_link': bugs_link,
'revisions': revisions,
},
'maintainer': cls._parsed_email(maintainer),
'relations': {
'provides': charm.provides,
'requires': charm.requires,
},
'options': charm.options,
'files': [join(entry['subdir'], entry['filename'])
for entry in charm.files.values()],
'is_approved': charm.promulgated,
'tested_providers': tested_providers,
'is_subordinate': charm.subordinate
})
return output
@classmethod
def _format_related(cls, charm_data, weight, _now=None):
if _now is None:
now = datetime.utcnow()
else:
now = _now
since = now - timedelta(30)
charm = Charm(charm_data)
icon_key = quote_key('icon.svg')
result = {
'id': cls._get_api_id(charm),
'name': charm.name,
'code_source': {
'type': 'bzr',
'location': charm.bzr_branch,
'revision': str(charm.last_change['revno']),
'last_log': charm.last_change['message'],
},
'has_icon': icon_key in charm.files,
'categories': charm.categories,
'downloads': charm.downloads,
'downloads_in_past_30_days': charm.downloads_in_past_30_days,
'commits_in_past_30_days': len(charm.changes_since(since)),
'weight': weight,
'is_approved': charm.promulgated,
}
return result
@staticmethod
def _extract_charm_id(path):
"""Extract the charm_id from a path.
charmids begining with a '~' are considered to be 3 elements long,
and other charmids are considered to be 2 elements long.
"""
if unquote(path[0])[0] == '~':
split_at = 3
else:
split_at = 2
charm_id = '/'.join(path[:split_at])
if len(path) > split_at:
trailing = '/'.join(path[split_at:])
else:
trailing = None
return charm_id, trailing
@staticmethod
def _parse_charm_id(charm_id):
"""Split a charm id into its component parts.
:return: A tuple of (owner, series, name, revision).
"""
elements = [unquote(element) for element in charm_id.split('/')]
name, revision = elements[-1].rsplit('-', 1)
if len(elements) == 3:
owner = elements[0]
if not owner.startswith('~'):
raise ValueError('Owner should begin with a "~".')
else:
owner = owner[1:]
else:
owner = None
return owner, elements[-2], name, revision
def _find_charm(self, path):
charm_id, trailing = self._extract_charm_id(path)
try:
owner, series, name, revision = self._parse_charm_id(charm_id)
except ValueError:
charm = None
else:
if owner is None:
charm = self.request.db.charms.find_one({
'promulgated': True,
'series': series,
'name': name,
})
else:
charm = self.request.db.charms.find_one({
'owner': owner,
'series': series,
'name': name,
})
if charm is not None:
api_id = self._get_api_id(Charm(charm))
# Charm id should match up to revision.
if not api_id.startswith(charm_id.split('-', 1)[0]):
charm = None
return charm_id, trailing, charm
def charm(self, path=None):
"""Retrieve a charm according to its charm_id."""
if path is None:
raise HTTPNotFound(self.request.path)
charm_id, trailing, charm_data = self._find_charm(path)
if charm_data is None:
return json_response(
404, {'type': 'no_such_charm', 'charm_id': charm_id})
charm = Charm(charm_data)
if trailing is None:
return self._charm_details(charm_data)
elif trailing.startswith('file/'):
return self._charm_file(charm, trailing)
elif trailing == ('icon.svg'):
return self._icon(charm)
elif trailing == ('qa'):
return self._charm_qa(charm)
elif trailing == ('related'):
return self._charm_related(charm_data)
else:
raise HTTPNotFound(self.request.path)
def bundle(self, path=None):
"""Retrieve a bundle based on id."""
if path is None:
raise HTTPNotFound(self.request.path)
fullpath = '/'.join(path)
if len(path) == 4:
# We have an owner.
query = {'_id': fullpath}
elif len(path) == 3:
basket_id = join(path[0], path[1])
query = {'basket': basket_id, 'name': path[2], 'promulgated': True}
else:
raise HTTPNotFound(self.request.path)
bundle_data = self.request.db.bundles.find_one(query)
if bundle_data is None:
return json_response(
404, {'type': 'no_such_bundle', 'bundle_id': fullpath})
bundle = Bundle(bundle_data)
return {bundle.name: bundle.data}
@staticmethod
def _get_file_headers(md5sum, content_type=None):
headerlist = [
("Access-Control-Allow-Origin", "*"),
("Access-Control-Allow-Headers", "X-Requested-With"),
("Cache-Control", "max-age=86400, public"),
("Etag", '"%s"' % md5sum),
]
if content_type is not None:
headerlist.append(('Content-Type', content_type))
return headerlist
def _charm_details(self, charm_data):
h = charm_data.get('hash')
if h is not None:
if_none_match = getattr(self.request, 'if_none_match', None)
if if_none_match is not None and h in if_none_match:
return Response('', status_code=304)
result = self._charm_result(Charm(charm_data))
return json_response(200, result, [('Etag', h)])
return self._charm_result(Charm(charm_data))
def _charm_file(self, charm, trailing):
path = trailing.split('/', 1)[1]
file_data = charm.files.get(quote_key(path.split('/')[-1]))
if file_data is not None:
if_none_match = getattr(self.request, 'if_none_match', None)
if if_none_match is not None and file_data['md5'] in if_none_match:
headerlist = self._get_file_headers(
file_data['md5'], file_data.get('contentType'))
return Response('', headerlist=headerlist, status_code=304)
fs = getfs(self.request.db)
file_id = CharmFileSet.gen_fileid(charm._representation, path)
try:
charm_file = CharmFileSet.get_by_id(fs, file_id)
except NoFile:
return json_response(404, {'type': 'no_such_file', 'path': path})
headerlist = self._get_file_headers(
charm_file.md5, charm_file.contentType)
return Response(
charm_file.read(),
headerlist=headerlist,
status_code=200)
def _icon(self, charm):
if (charm.files and
charm.files.get(quote_key('icon.svg')) and
charm.promulgated):
return self._charm_file(charm, '/icon.svg')
elif charm.categories:
main_category = charm.categories[0]
icon_url = "{0}charm-{1}.svg".format(self.ICON_PATH, main_category)
else:
icon_url = self.ICON_PATH + 'charm_160.svg'
return HTTPFound(icon_url)
def _charm_related(self, charm_data):
requires, provides = self._related_charms(charm_data)
return {'result': {'requires': requires, 'provides': provides}}
@staticmethod
def _format_category(category):
return dict((key, value) for key, value in category.items()
if key != '_id')
def _charm_qa(self, charm):
qa_data_source = QADataSource.from_db(self.request.db)
qa_data = qa_data_source.get_qa_data(charm)
categories = [self._format_category(category) for category
in qa_data.qa_categories.values()]
scores = qa_data.filtered_scores()
return {'result': {'questions': categories}, 'scores': scores}
def charms(self, path=None, *args, **kwargs):
if path is None or path == ['']:
handler = self._charms
elif path[0] == 'interesting':
handler = self._interesting_charms
else:
raise HTTPNotFound(self.request.path)
return self._handle(handler, args, kwargs)
def _charms(self, limit=None, name=None, series=None, owner=None,
provides=None, requires=None, type_=None, provider=None,
scope=None, categories=None, text=None, autocomplete=False):
"""Search for charms matching parameters.
:limit: A query limit. (max number of results)
"""
autocomplete = autocomplete == ['true']
params = dict((key, value) for key, value in locals().items()
if key in ('series', 'owner', 'name', 'categories'))
if text is None:
text = ['']
params['i_provides'] = provides
params['i_requires'] = requires
filters = dict(item for item in params.items() if item[1] is not None)
if limit is not None:
if len(limit) > 1:
return json_response(406, {
'type': 'multiple_values',
'parameter': 'limit'})
limit = int(limit[0])
try:
results = self.request.index_client.api_search(
text[0], filters, type_, limit, autocomplete=autocomplete)
except InvalidCharmType as e:
return json_response(406, {
'type': 'unsupported_value',
'parameter': 'type',
'value': e[0]})
except NegativeLimit:
return json_response(406, {
'type': 'negative_value',
'parameter': 'limit'})
return {'result': self._charm_results(results)}
def _charm_results(self, charms):
return [self._charm_result(Charm(charm_data)) for charm_data in charms]
def _interesting_charms(self):
"""Generate a JSON structure of interesting charms.
Includes featured, new and popular charms.
"""
popular = self.request.index_client.api_search(
sort='downloaded', limit=10)
new = self.request.index_client.api_search(sort='new', limit=10)
featured = self.request.index_client.api_search(
filters={'is_featured': [True]})
return {
'result': {
'new': self._charm_results(new),
'featured': self._charm_results(featured),
'popular': self._charm_results(popular),
}
}
|