Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/uritemplate/api.py: 75%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
3uritemplate.api
4===============
6This module contains the very simple API provided by uritemplate.
8"""
10import typing as t
12from uritemplate import variable
13from uritemplate.orderedset import OrderedSet
14from uritemplate.template import URITemplate
16__all__ = ("OrderedSet", "URITemplate", "expand", "partial", "variables")
19def expand(
20 uri: str,
21 var_dict: t.Optional[variable.VariableValueDict] = None,
22 **kwargs: variable.VariableValue,
23) -> str:
24 """Expand the template with the given parameters.
26 :param str uri: The templated URI to expand
27 :param dict var_dict: Optional dictionary with variables and values
28 :param kwargs: Alternative way to pass arguments
29 :returns: str
31 Example::
33 expand('https://api.github.com{/end}', {'end': 'users'})
34 expand('https://api.github.com{/end}', end='gists')
36 .. note:: Passing values by both parts, may override values in
37 ``var_dict``. For example::
39 expand('https://{var}', {'var': 'val1'}, var='val2')
41 ``val2`` will be used instead of ``val1``.
43 """
44 return URITemplate(uri).expand(var_dict, **kwargs)
47def partial(
48 uri: str,
49 var_dict: t.Optional[variable.VariableValueDict] = None,
50 **kwargs: variable.VariableValue,
51) -> URITemplate:
52 """Partially expand the template with the given parameters.
54 If all of the parameters for the template are not given, return a
55 partially expanded template.
57 :param dict var_dict: Optional dictionary with variables and values
58 :param kwargs: Alternative way to pass arguments
59 :returns: :class:`URITemplate`
61 Example::
63 t = URITemplate('https://api.github.com{/end}')
64 t.partial() # => URITemplate('https://api.github.com{/end}')
66 """
67 return URITemplate(uri).partial(var_dict, **kwargs)
70def variables(uri: str) -> OrderedSet:
71 """Parse the variables of the template.
73 This returns all of the variable names in the URI Template.
75 :returns: Set of variable names
76 :rtype: set
78 Example::
80 variables('https://api.github.com{/end})
81 # => {'end'}
82 variables('https://api.github.com/repos{/username}{/repository}')
83 # => {'username', 'repository'}
85 """
86 return OrderedSet(URITemplate(uri).variable_names)