use std::collections::HashMap;
use api_bindium::ApiClient;
use api_bindium::HTTPVerb;
use api_bindium::JsonParser;
use api_bindium::Parser;
use api_bindium::api_response::ureq_response::UreqResponseInner;
use api_bindium::endpoints::EndpointUriBuilder;
use serde_json::json;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct HttpBinPostResponse {
data: String,
}
pub struct HTTPBinParser;
impl Parser<UreqResponseInner> for HTTPBinParser {
type Output = HashMap<String, String>;
type Error = serde_json::Error;
fn parse(&self, response: UreqResponseInner) -> Result<Self::Output, Self::Error> {
let httpbin_response: HttpBinPostResponse = JsonParser::default().parse(response).unwrap();
let inner_data: HashMap<String, String> = serde_json::from_str(&httpbin_response.data)?;
Ok(inner_data)
}
}
fn main() {
let client = ApiClient::builder().build();
let mut request = EndpointUriBuilder::new()
.https()
.set_authority("httpbin.org")
.set_path("/post")
.into_api_request_with_body(
HTTPVerb::Post,
json!({
"hello": "world"
}),
HTTPBinParser,
)
.unwrap();
let res = request.send(&client).unwrap().parse().unwrap();
assert_eq!(res.get("hello").unwrap(), "world");
}