Exemple #1
0
def link_rel(
    rel: LinkRelation,
    href: str,
    method: HTTPMethod = "get",
    content_type: str = "application/json",
    profile: Optional[str] = None,
    title: Optional[str] = None,
    parameters: Optional[Dict[str, str]] = None,
    body_params: Optional[Dict[str, Optional[str]]] = None,
) -> LinkType:
    """Link to a separate entity

    Args:
        rel:
            The rel value.

        href:
            The destination HTTP URL

        method:
            The HTTP method to user for this URL

        content_type:
            The content-type that needs to be sent for this URL to return the desired result

        profile:
            (Optional) Additional profile data to change the behaviour of the URL response.

        title:
            (Optional) A pretty printed string for UIs to render.

        body_params:
            (Optional) A dict of values which shall be sent as body paramaters.

        parameters:
            (Optional) Parameters for the rel-value. e.g. rel='foo', parameters={'baz': 'bar'}
            will result in a rel-value of 'foo;baz="bar"'

    Examples:

        >>> expected = {
        ...     'domainType': 'link',
        ...     'type': 'application/json;profile="urn:org.restfulobjects:rels/object"',
        ...     'method': 'GET',
        ...     'rel': 'urn:org.restfulobjects:rels/update',
        ...     'title': 'Update the object',
        ...     'href': 'https://localhost:5000/NO_SITE/check_mk/api/1.0/objects/foo/update'
        ... }
        >>> with _request_context():
        ...     link = link_rel('.../update', '/objects/foo/update',
        ...                     method='get', profile='.../object', title='Update the object')
        >>> assert link == expected, link

    Returns:
        A dict representing the link

    """
    content_type_params = {}
    if profile is not None:
        content_type_params["profile"] = expand_rel(profile)

    link_obj = {
        "rel": expand_rel(rel, parameters),
        "href": absolute_url(href),
        "method": method.upper(),
        "type": expand_rel(content_type, content_type_params),
        "domainType": "link",
    }
    if body_params is not None:
        link_obj["body_params"] = body_params
    if title is not None:
        link_obj["title"] = title
    return link_obj
Exemple #2
0
def link_rel(
    rel: EndpointName,
    href: str,
    method: HTTPMethod = 'get',
    content_type: str = 'application/json',
    profile: Optional[str] = None,
    title: Optional[str] = None,
    parameters: Optional[Dict[str, str]] = None,
) -> LinkType:
    """Link to a separate entity

    Args:
        rel:
            The rel value.

        href:
            The destination HTTP URL

        method:
            The HTTP method to user for this URL

        content_type:
            The content-type that needs to be sent for this URL to return the desired result

        profile:
            (Optional) Additional profile data to change the behaviour of the URL response.

        title:
            (Optional) A pretty printed string for UIs to render.

        parameters:
            (Optional) Parameters for the rel-value. e.g. rel='foo', parameters={'baz': 'bar'}
            will result in a rel-value of 'foo;baz="bar"'

    Examples:

        >>> link = link_rel('.../update', 'update',
        ...                 method='get', profile='.../object', title='Update the object')
        >>> expected = {
        ...     'domainType': 'link',
        ...     'type': 'application/json;profile="urn:org.restfulobjects:rels/object"',
        ...     'method': 'GET',
        ...     'rel': 'urn:org.restfulobjects:rels/update',
        ...     'title': 'Update the object',
        ...     'href': 'update'
        ... }
        >>> assert link == expected, link

    Returns:
        A dict representing the link

    """
    content_type_params = {}
    if profile is not None:
        content_type_params['profile'] = expand_rel(profile)

    link_obj = {
        'rel': expand_rel(rel, parameters),
        'href': href,
        'method': method.upper(),
        'type': expand_rel(content_type, content_type_params),
        'domainType': 'link',
    }
    if title is not None:
        link_obj['title'] = title
    return link_obj