use base64::{prelude::BASE64_STANDARD, Engine};
use flawless_wasabi::{submit, Input};
use crate::{Config, Error, Response};
use super::HTTPTransport;
pub struct Idempotent;
pub struct Flawless;
type Headers = Vec<(String, String)>;
type Ret = Result<Response, Error>;
impl HTTPTransport for Flawless {
fn send(self, config: Config, method: String, url: String, headers: Headers, body: Vec<u8>) -> Ret {
send(config, method, url, headers, body)
}
}
impl HTTPTransport for Idempotent {
fn send(self, config: Config, method: String, url: String, headers: Headers, body: Vec<u8>) -> Ret {
send(config, method, url, headers, body)
}
}
fn send(config: Config, method: String, url: String, headers: Headers, body: Vec<u8>) -> Ret {
let body = BASE64_STANDARD.encode(body);
let req = flawless_wasabi::HttpRequest { config: config.into(), method, url, headers, body };
match submit(&Input::HTTPRequest(req)).expect("http_request(req) call shouldn't fail") {
flawless_wasabi::Output::HTTPResponse(res) => res.map(|r| r.into()).map_err(|e| e.into()),
_ => unreachable!("http_request(req) returns other type"),
}
}