1313# See the License for the specific language governing permissions and
1414# limitations under the License.
1515
16- from typing import Dict , Union
16+ from typing import (
17+ Dict ,
18+ Optional ,
19+ Union ,
20+ )
1721
1822from datacustomcode .named_credential .types .http_method import HTTPMethod
1923from datacustomcode .named_credential .types .http_request import HTTPRequest
2024
25+ # Control header carrying the per-request callout response timeout (seconds).
26+ CALLOUT_RESPONSE_TIMEOUT_HEADER = "ctx-callout-response-timeout-seconds"
27+
2128
2229class HTTPRequestBuilder :
2330 def __init__ (self ) -> None :
2431 self ._url = ""
2532 self ._method : Union [str , HTTPMethod ] = HTTPMethod .GET
2633 self ._headers : Dict [str , str ] = {}
34+ self ._response_timeout_seconds : Optional [int ] = None
2735
2836 def set_url (self , url : str ) -> "HTTPRequestBuilder" :
2937 """Set the symbolic Named Credential reference.
@@ -44,12 +52,37 @@ def set_method(self, method: Union[str, HTTPMethod]) -> "HTTPRequestBuilder":
4452 return self
4553
4654 def set_headers (self , headers : Dict [str , str ]) -> "HTTPRequestBuilder" :
55+ """Set the HTTP headers.
56+
57+ Args:
58+ headers: a dictionary of HTTP headers
59+ """
4760 self ._headers = headers
4861 return self
4962
63+ def set_response_timeout_seconds (self , seconds : int ) -> "HTTPRequestBuilder" :
64+ """Set the per-request callout response timeout, in seconds.
65+
66+ The ``ctx-callout-response-timeout-seconds`` control header allows to
67+ configure the timeout for the callout response. This is optional.
68+ If not set, the platform will use the default timeout.
69+ """
70+ if isinstance (seconds , bool ) or not isinstance (seconds , int ) or seconds <= 0 :
71+ raise ValueError (
72+ "response timeout must be a positive integer number of seconds, "
73+ f"got { seconds !r} ."
74+ )
75+ self ._response_timeout_seconds = seconds
76+ return self
77+
5078 def build (self ) -> HTTPRequest :
79+ headers = dict (self ._headers )
80+ if self ._response_timeout_seconds is not None :
81+ headers [CALLOUT_RESPONSE_TIMEOUT_HEADER ] = str (
82+ self ._response_timeout_seconds
83+ )
5184 return HTTPRequest (
5285 url = self ._url ,
5386 method = self ._method , # type: ignore[arg-type]
54- headers = self . _headers ,
87+ headers = headers ,
5588 )
0 commit comments