Coverage for watcher/api/controllers/link.py: 92%
24 statements
« prev ^ index » next coverage.py v7.8.2, created at 2025-06-17 12:22 +0000
« prev ^ index » next coverage.py v7.8.2, created at 2025-06-17 12:22 +0000
1# -*- encoding: utf-8 -*-
2# Copyright 2013 Red Hat, Inc.
3# All Rights Reserved.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14# implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
18import pecan
19from wsme import types as wtypes
21from watcher.api.controllers import base
24def build_url(resource, resource_args, bookmark=False, base_url=None):
25 if base_url is None:
26 base_url = pecan.request.application_url
28 template = '%(url)s/%(res)s' if bookmark else '%(url)s/v1/%(res)s'
29 # FIXME(lucasagomes): I'm getting a 404 when doing a GET on
30 # a nested resource that the URL ends with a '/'.
31 # https://groups.google.com/forum/#!topic/pecan-dev/QfSeviLg5qs
32 template += '%(args)s' if resource_args.startswith('?') else '/%(args)s'
33 return template % {'url': base_url, 'res': resource, 'args': resource_args}
36class Link(base.APIBase):
37 """A link representation."""
39 href = wtypes.text
40 """The url of a link."""
42 rel = wtypes.text
43 """The name of a link."""
45 type = wtypes.text
46 """Indicates the type of document/link."""
48 @staticmethod
49 def make_link(rel_name, url, resource, resource_args,
50 bookmark=False, type=wtypes.Unset):
51 href = build_url(resource, resource_args,
52 bookmark=bookmark, base_url=url)
53 return Link(href=href, rel=rel_name, type=type)
55 @classmethod
56 def sample(cls):
57 sample = cls(href="http://localhost:6385/chassis/"
58 "eaaca217-e7d8-47b4-bb41-3f99f20eed89",
59 rel="bookmark")
60 return sample