Skip to content

Commit a5ddf46

Browse files
evaogbeJon Wayne Parrott
authored andcommitted
api_core: Add routing header (#4336)
* api_core: Add routing header * Fix linting * Review fixes * More review fixes * Documentation fixes
1 parent 4500dc7 commit a5ddf46

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

google/api_core/gapic_v1/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414

1515
from google.api_core.gapic_v1 import config
1616
from google.api_core.gapic_v1 import method
17+
from google.api_core.gapic_v1 import routing_header
1718

1819
__all__ = [
1920
'config',
2021
'method',
22+
'routing_header',
2123
]
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright 2017 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Helpers for constructing routing headers.
16+
17+
These headers are used by Google infrastructure to determine how to route
18+
requests, especially for services that are regional.
19+
20+
Generally, these headers are specified as gRPC metadata.
21+
"""
22+
23+
from six.moves.urllib.parse import urlencode
24+
25+
ROUTING_METADATA_KEY = 'x-goog-header-params'
26+
27+
28+
def to_routing_header(params):
29+
"""Returns a routing header string for the given request parameters.
30+
31+
Args:
32+
params (Mapping[str, Any]): A dictionary containing the request
33+
parameters used for routing.
34+
35+
Returns:
36+
str: The routing header string.
37+
"""
38+
return urlencode(params)
39+
40+
41+
def to_grpc_metadata(params):
42+
"""Returns the gRPC metadata containing the routing headers for the given
43+
request parameters.
44+
45+
Args:
46+
params (Mapping[str, Any]): A dictionary containing the request
47+
parameters used for routing.
48+
49+
Returns:
50+
Tuple(str, str): The gRPC metadata containing the routing header key
51+
and value.
52+
"""
53+
return (ROUTING_METADATA_KEY, to_routing_header(params))
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2017 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
from google.api_core.gapic_v1 import routing_header
17+
18+
19+
def test_to_routing_header():
20+
params = [('name', 'meep'), ('book.read', '1')]
21+
value = routing_header.to_routing_header(params)
22+
assert value == "name=meep&book.read=1"
23+
24+
25+
def test_to_grpc_metadata():
26+
params = [('name', 'meep'), ('book.read', '1')]
27+
metadata = routing_header.to_grpc_metadata(params)
28+
assert metadata == (
29+
routing_header.ROUTING_METADATA_KEY, "name=meep&book.read=1")

0 commit comments

Comments
 (0)