1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//! # HTTP Client
//!
//! This module contains the HTTP client through which actors consume
//! the currently bound `wascap:http_client` capability provider

use wapc_guest::host_call;
use wascc_codec::{deserialize, http::*, serialize};

use crate::HandlerResult;

const CAPID_HTTPCLIENT: &str = "wascc:http_client";

/// An abstraction around a host runtime capability for an HTTP client
pub struct HttpClientHostBinding {
    binding: String,
}

impl Default for HttpClientHostBinding {
    fn default() -> Self {
        HttpClientHostBinding {
            binding: "default".to_string(),
        }
    }
}

/// Creates a named host binding for the HTTP client capability
pub fn host(binding: &str) -> HttpClientHostBinding {
    HttpClientHostBinding {
        binding: binding.to_string(),
    }
}

/// Creates the default host binding for the key-value store capability
pub fn default() -> HttpClientHostBinding {
    HttpClientHostBinding::default()
}

impl HttpClientHostBinding {
    pub fn request(&self, request: Request) -> HandlerResult<Response> {
        host_call(
            &self.binding,
            CAPID_HTTPCLIENT,
            OP_PERFORM_REQUEST,
            &serialize(request)?,
        )
        .map(|r| deserialize::<Response>(r.as_ref()).unwrap())
        .map_err(|e| e.into())
    }
}