|
| 1 | +import pecan |
| 2 | + |
| 3 | +from bigbang.api.controllers import base |
| 4 | +from bigbang.api.controllers import types |
| 5 | + |
| 6 | + |
| 7 | +def build_url(resource, resource_args, bookmark=False, base_url=None): |
| 8 | + if base_url is None: |
| 9 | + base_url = pecan.request.host_url |
| 10 | + |
| 11 | + template = '%(url)s/%(res)s' if bookmark else '%(url)s/v1/%(res)s' |
| 12 | + # FIXME(lucasagomes): I'm getting a 404 when doing a GET on |
| 13 | + # a nested resource that the URL ends with a '/'. |
| 14 | + # https://groups.google.com/forum/#!topic/pecan-dev/QfSeviLg5qs |
| 15 | + template += '%(args)s' if resource_args.startswith('?') else '/%(args)s' |
| 16 | + return template % {'url': base_url, 'res': resource, 'args': resource_args} |
| 17 | + |
| 18 | + |
| 19 | +class Link(base.APIBase): |
| 20 | + """A link representation.""" |
| 21 | + |
| 22 | + fields = { |
| 23 | + 'href': { |
| 24 | + 'validate': types.Text.validate |
| 25 | + }, |
| 26 | + 'rel': { |
| 27 | + 'validate': types.Text.validate |
| 28 | + }, |
| 29 | + 'type': { |
| 30 | + 'validate': types.Text.validate |
| 31 | + }, |
| 32 | + } |
| 33 | + |
| 34 | + @staticmethod |
| 35 | + def make_link(rel_name, url, resource, resource_args, |
| 36 | + bookmark=False, type=None): |
| 37 | + href = build_url(resource, resource_args, |
| 38 | + bookmark=bookmark, base_url=url) |
| 39 | + if type is None: |
| 40 | + return Link(href=href, rel=rel_name) |
| 41 | + else: |
| 42 | + return Link(href=href, rel=rel_name, type=type) |
| 43 | + |
| 44 | + @classmethod |
| 45 | + def sample(cls): |
| 46 | + sample = cls(href="http://localhost:8080/containers/" |
| 47 | + "eaaca217-e7d8-47b4-bb41-3f99f20eed89", |
| 48 | + rel="bookmark") |
| 49 | + return sample |
0 commit comments