Skip to content

Commit 2f7af86

Browse files
younes-ioYounes
authored and
Younes
committed
Document hostcalls::dispatch_http_call
Signed-off-by: Younes <[email protected]>
1 parent 09374ed commit 2f7af86

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/hostcalls.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,50 @@ extern "C" {
741741
) -> Status;
742742
}
743743

744+
/// Initiates an HTTP call to an upstream server.
745+
///
746+
/// This function sends an HTTP request to the specified `upstream` server with the provided
747+
/// `headers`, `body`, and `trailers`. The `timeout` parameter specifies the duration after
748+
/// which the call will time out if no response is received.
749+
///
750+
/// # Arguments
751+
///
752+
/// * `upstream`: The URL or host of the upstream server to which the HTTP call will be made.
753+
/// * `headers`: A vector of tuples representing the HTTP headers to include in the request.
754+
/// Each tuple consists of a header name and value.
755+
/// * `body`: An optional byte slice representing the request body.
756+
/// * `trailers`: An optional vector of tuples representing the HTTP trailers to include in
757+
/// the request. Each tuple consists of a trailer name and value.
758+
/// * `timeout`: The duration after which the HTTP call will time out if no response is received.
759+
///
760+
/// # Returns
761+
///
762+
/// A `Result` indicating the success or failure of the HTTP call. If the call is successful,
763+
/// the function returns an HTTP status code as a `u32`. If an error occurs, an `Error` object
764+
/// is returned.
765+
///
766+
/// # Example
767+
///
768+
/// ```rust
769+
/// use proxy_wasm::hostcalls::dispatch_http_call;
770+
///
771+
/// let upstream = "http://example.com";
772+
/// let headers = vec![("Content-Type", "application/json")];
773+
/// let body = Some(b"request body");
774+
/// let trailers = Some(vec![("Trailer-Name", "trailer value")]);
775+
/// let timeout = std::time::Duration::from_secs(5);
776+
///
777+
/// let result = dispatch_http_call(upstream, headers, body, trailers, timeout);
778+
///
779+
/// match result {
780+
/// Ok(status_code) => {
781+
/// println!("HTTP call successful. Status code: {}", status_code);
782+
/// }
783+
/// Err(error) => {
784+
/// println!("HTTP call failed. Error: {:?}", error);
785+
/// }
786+
/// }
787+
/// ```
744788
pub fn dispatch_http_call(
745789
upstream: &str,
746790
headers: Vec<(&str, &str)>,

0 commit comments

Comments
 (0)