api_bindium 0.4.0

Framework for api binding crates
Documentation
use serde_json::json;

use crate::ApiRequest;
use crate::JsonParser;
use crate::endpoints::EndpointUriBuilder;

fn httpbin_post_request() -> ApiRequest<JsonParser<HttpBinPostResponse>> {
    EndpointUriBuilder::new()
        .https()
        .set_authority("httpbin.org")
        .set_path("/post")
        .into_api_request_with_body(
            crate::HTTPVerb::Post,
            json!({
                "hello": "world"
            }),
            JsonParser::default(),
        )
        .unwrap()
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct HttpBinPostResponse {
    data: String,
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct HttpBinPostResponseData {
    hello: String,
}

#[test]
#[cfg(feature = "sync")]
fn test_post_query() {
    use crate::ApiClient;

    let client = ApiClient::builder().build();
    let res = httpbin_post_request()
        .send(&client)
        .unwrap()
        .parse()
        .unwrap();

    // HTTP bin send the body as a string instead of json... So deserializing required
    let json: HttpBinPostResponseData = serde_json::from_str(&res.data).unwrap();

    assert_eq!(json.hello, "world".to_string())
}