wascc_actor/
http_client.rs

1//! # HTTP Client
2//!
3//! This module contains the HTTP client through which actors consume
4//! the currently bound `wascap:http_client` capability provider
5
6use wapc_guest::host_call;
7use wascc_codec::{deserialize, http::*, serialize};
8
9use crate::HandlerResult;
10
11const CAPID_HTTPCLIENT: &str = "wascc:http_client";
12
13/// An abstraction around a host runtime capability for an HTTP client
14pub struct HttpClientHostBinding {
15    binding: String,
16}
17
18impl Default for HttpClientHostBinding {
19    fn default() -> Self {
20        HttpClientHostBinding {
21            binding: "default".to_string(),
22        }
23    }
24}
25
26/// Creates a named host binding for the HTTP client capability
27pub fn host(binding: &str) -> HttpClientHostBinding {
28    HttpClientHostBinding {
29        binding: binding.to_string(),
30    }
31}
32
33/// Creates the default host binding for the key-value store capability
34pub fn default() -> HttpClientHostBinding {
35    HttpClientHostBinding::default()
36}
37
38impl HttpClientHostBinding {
39    pub fn request(&self, request: Request) -> HandlerResult<Response> {
40        host_call(
41            &self.binding,
42            CAPID_HTTPCLIENT,
43            OP_PERFORM_REQUEST,
44            &serialize(request)?,
45        )
46        .map(|r| deserialize::<Response>(r.as_ref()).unwrap())
47        .map_err(|e| e.into())
48    }
49}