nemoguardrails.http.request

View as Markdown

Managed request lifecycle helpers for outbound HTTP calls.

Module Contents

Functions

NameDescription
_resolve_http_clientYield an injected client or create and close an owned client.
http_callExecute one outbound HTTP request with deterministic client ownership.

API

nemoguardrails.http.request._resolve_http_client(
client: nemoguardrails.http.client.HTTPClient | None,
factory: collections.abc.Callable[[], nemoguardrails.http.client.ClosableHTTPClient]
) -> collections.abc.AsyncIterator[nemoguardrails.http.client.HTTPClient]
async

Yield an injected client or create and close an owned client.

nemoguardrails.http.request.http_call(
client: nemoguardrails.http.client.HTTPClient | None,
method: str,
url: str,
headers: typing.Mapping[str, str] | None = None,
params: typing.Mapping[str, typing.Any] | None = None,
json: typing.Any = None,
content: bytes | str | None = None,
timeout: float | None = None,
raise_for_status: bool = True,
factory: collections.abc.Callable[[], nemoguardrails.http.client.ClosableHTTPClient] = create_http_client
) -> nemoguardrails.http.types.HTTPResponse
async

Execute one outbound HTTP request with deterministic client ownership.

Parameters:

client
HTTPClient | None

Optional caller-owned client. When omitted, factory creates a client that is closed after the request.

method
str

HTTP method.

url
str

Absolute request URL.

headers
Mapping[str, str] | NoneDefaults to None

Optional request headers.

params
Mapping[str, Any] | NoneDefaults to None

Optional query parameters.

json
AnyDefaults to None

Optional JSON-serializable request body.

content
bytes | str | NoneDefaults to None

Optional raw request body.

timeout
float | NoneDefaults to None

Optional total request timeout in seconds.

raise_for_status
boolDefaults to True

Whether responses with status 400 or greater raise.

factory
Callable[[], ClosableHTTPClient]Defaults to create_http_client

Factory for an owned client when client is omitted.

Returns: HTTPResponse

The materialized transport-neutral response.

Raises:

  • HTTPStatusError: If raise_for_status is enabled and the response status is 400 or greater.
  • TypeError: If factory returns a client that cannot be closed.