wasmcloud_actor_http_client/
lib.rs

1#![doc(html_logo_url = "https://avatars2.githubusercontent.com/u/52050279?s=200&v=4")]
2//! # HTTP Client wasmCloud Actor Interface
3//!
4//! This crate provides wasmCloud actors with an interface to the HTTP client capability provider. Actors using this
5//! interface must have the claim `wasmcloud:httpclient` in order to have permission to make outbound HTTP requests,
6//! and they must have an active, configured binding to an HTTP Client capability provider.
7//!
8//! wasmCloud actors without this permission and capability binding will be unable to make outbound HTTP requests.
9//!
10//! # Example:
11//! ```
12//! use wapc_guest::HandlerResult;
13//! extern crate wasmcloud_actor_http_server as httpserver;
14//! extern crate wasmcloud_actor_http_client as httpclient;
15//! extern crate wasmcloud_actor_core as actor;
16//!
17//! const API_URL: &str = "https://wasmcloudapi.cloud.io/proxy";
18//!
19//! #[actor::init]
20//! pub fn init() {
21//!     httpserver::Handlers::register_handle_request(get_proxy);
22//! }
23//!
24//! /// This function proxys an inbound HTTP request to an external server
25//! fn get_proxy(msg: httpserver::Request) -> HandlerResult<httpserver::Response> {
26//!     // Form client request from server request
27//!     if msg.method == "GET".to_string() {
28//!         // Replace `request` with `httpclient::default().request`
29//!         let res = request(msg.method, API_URL.to_string(), msg.header, vec![])?;
30//!         // Form server response
31//!         Ok(httpserver::Response {
32//!             status_code: res.status_code,
33//!             status: res.status,
34//!             header: res.header,
35//!             body: res.body,
36//!         })
37//!     } else {
38//!         Ok(httpserver::Response::internal_server_error("Only GET requests can be proxied with this actor"))
39//!     }
40//! }
41//!
42//! # fn request(method: String, url: String, headers: std::collections::HashMap<String,String>, body: Vec<u8>) -> HandlerResult<httpclient::Response> {
43//! #   Ok(httpclient::Response {
44//! #     status: "OK".to_string(),
45//! #     status_code: 200,
46//! #     ..Default::default()
47//! #   })
48//! # }
49
50#[cfg(feature = "guest")]
51extern crate wapc_guest as guest;
52#[cfg(feature = "guest")]
53#[allow(unused)]
54use guest::prelude::*;
55mod generated;
56pub use generated::*;
57
58pub const OP_REQUEST: &str = "Request";