Path Templates

Expand and validate URL path templates.

This module provides the expand() and validate() functions for interacting with Google-style URL path templates which are commonly used in Google APIs for resource names.

google.api_core.path_template.expand(tmpl, *args, **kwargs)[source]

Expand a path template with the given variables.

..code-block:: python

>>> expand('users/*/messages/*', 'me', '123')
users/me/messages/123
>>> expand('/v1/{name=shelves/*/books/*}', name='shelves/1/books/3')
/v1/shelves/1/books/3
Parameters
  • tmpl (str) – The path template.

  • args – The positional variables for the path.

  • kwargs – The named variables for the path.

Returns

The expanded path

Return type

str

Raises

ValueError – If a positional or named variable is required by the template but not specified or if an unexpected template expression is encountered.

google.api_core.path_template.validate(tmpl, path)[source]

Validate a path against the path template.

>>> validate('users/*/messages/*', 'users/me/messages/123')
True
>>> validate('users/*/messages/*', 'users/me/drafts/123')
False
>>> validate('/v1/{name=shelves/*/books/*}', /v1/shelves/1/books/3)
True
>>> validate('/v1/{name=shelves/*/books/*}', /v1/shelves/1/tapes/3)
False
Parameters
  • tmpl (str) – The path template.

  • path (str) – The expanded path.

Returns

True if the path matches.

Return type

bool