Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/api_core/gapic_v1/routing_header.py: 69%

13 statements  

« prev     ^ index     » next       coverage.py v7.3.1, created at 2023-09-25 06:37 +0000

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 

17These headers are used by Google infrastructure to determine how to route 

18requests, especially for services that are regional. 

19 

20Generally, these headers are specified as gRPC metadata. 

21""" 

22 

23from enum import Enum 

24from urllib.parse import urlencode 

25 

26ROUTING_METADATA_KEY = "x-goog-request-params" 

27 

28 

29def to_routing_header(params, qualified_enums=True): 

30 """Returns a routing header string for the given request parameters. 

31 

32 Args: 

33 params (Mapping[str, Any]): A dictionary containing the request 

34 parameters used for routing. 

35 qualified_enums (bool): Whether to represent enum values 

36 as their type-qualified symbol names instead of as their 

37 unqualified symbol names. 

38 

39 Returns: 

40 str: The routing header string. 

41 

42 """ 

43 if not qualified_enums: 

44 if isinstance(params, dict): 

45 tuples = params.items() 

46 else: 

47 tuples = params 

48 params = [(x[0], x[1].name) if isinstance(x[1], Enum) else x for x in tuples] 

49 return urlencode( 

50 params, 

51 # Per Google API policy (go/api-url-encoding), / is not encoded. 

52 safe="/", 

53 ) 

54 

55 

56def to_grpc_metadata(params, qualified_enums=True): 

57 """Returns the gRPC metadata containing the routing headers for the given 

58 request parameters. 

59 

60 Args: 

61 params (Mapping[str, Any]): A dictionary containing the request 

62 parameters used for routing. 

63 qualified_enums (bool): Whether to represent enum values 

64 as their type-qualified symbol names instead of as their 

65 unqualified symbol names. 

66 

67 Returns: 

68 Tuple(str, str): The gRPC metadata containing the routing header key 

69 and value. 

70 """ 

71 return (ROUTING_METADATA_KEY, to_routing_header(params, qualified_enums))