As of January 1, 2020 this library no longer supports Python 2 on the latest released version. Library versions released prior to that date will continue to be available. For more information please visit Python 2 support on Google Cloud.

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.delete_field(request, field)[source]

Delete the value of a field from a given dictionary.

Parameters
  • request (dict | Message) – A dictionary object or a Message.

  • field (str) – The key to the request in dot notation.

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

Expand a path template with the given variables.

>>> 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.get_field(request, field)[source]

Get the value of a field from a given dictionary.

Parameters
  • request (dict | Message) – A dictionary or a Message object.

  • field (str) – The key to the request in dot notation.

Returns

The value of the field.

google.api_core.path_template.transcode(http_options, message=None, **request_kwargs)[source]

Transcodes a grpc request pattern into a proper HTTP request following the rules outlined here, https://github.com/googleapis/googleapis/blob/master/google/api/http.proto#L44-L312

Args:
http_options (list(dict)): A list of dicts which consist of these keys,

‘method’ (str): The http method ‘uri’ (str): The path template ‘body’ (str): The body field name (optional) (This is a simplified representation of the proto option google.api.http)

message (Message) : A request object (optional) request_kwargs (dict) : A dict representing the request object

Returns:
dict: The transcoded request with these keys,

‘method’ (str) : The http method ‘uri’ (str) : The expanded uri ‘body’ (dict | Message) : A dict or a Message representing the body (optional) ‘query_params’ (dict | Message) : A dict or Message mapping query parameter variables and values

Raises:

ValueError: If the request does not match the given template.

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