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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
//! `HttpRequest` derive
#![deny(
bad_style,
dead_code,
improper_ctypes,
non_shorthand_field_patterns,
no_mangle_generic_items,
overflowing_literals,
path_statements,
patterns_in_fns_without_body,
private_interfaces,
private_bounds,
unconditional_recursion,
unused,
unused_allocation,
unused_comparisons,
unused_parens,
while_true,
missing_debug_implementations,
missing_docs,
trivial_casts,
trivial_numeric_casts,
unused_extern_crates,
unused_import_braces,
unused_qualifications,
unused_results
)]
#[allow(unused_extern_crates)]
extern crate self as http_request_derive;
mod error;
mod from_http_response;
mod http_request;
mod http_request_body;
mod http_request_query_params;
#[doc(hidden)]
pub mod __exports {
pub use http;
}
pub use error::Error;
pub use from_http_response::FromHttpResponse;
pub use http_request::HttpRequest;
pub use http_request_body::HttpRequestBody;
pub use http_request_query_params::HttpRequestQueryParams;
/// [`derive@HttpRequest`] can be derived by structs that needs to implement the [`HttpRequest`] trait.
///
/// It generates the request types (e.g. Response, Body or Query) and the getter functions
///
/// # Examples:
///
/// ## GET Endpoint with dynamic path
///
/// ```
/// # use http_request_derive::HttpRequest;
/// #[derive(HttpRequest)]
/// #[http_request(
/// method = "GET",
/// response = GetItemResponse,
/// path = "/v1/item/{0}"
/// )]
/// pub struct GetItemRequest(pub usize);
///
/// # #[derive(serde::Deserialize)]
/// # pub struct GetItemResponse;
/// ```
///
/// ## GET Endpoint with query parameters
///
/// ```
/// # use http_request_derive::HttpRequest;
/// # pub type PostEventInviteQuery = String;
/// #[derive(HttpRequest)]
/// #[http_request(
/// method = "GET",
/// response = String,
/// path = "/v1/items"
/// )]
/// pub struct GetItemsWithQueryRequest {
/// #[http_request(query)]
/// pub query: PostEventInviteQuery,
/// }
/// ```
///
/// ## POST Endpoint with multiple attributes
///
/// ```
/// # use http_request_derive::HttpRequest;
/// # pub type SampleQuery = String;
/// # pub type SampleBody = String;
/// # pub type SampleResponse = String;
/// #[derive(HttpRequest)]
/// #[http_request(
/// method = "POST",
/// response = SampleResponse,
/// path = "/v1/item/{id}"
/// )]
/// pub struct SampleRequest {
/// pub id: usize,
///
/// #[http_request(query)]
/// pub query: SampleQuery,
///
/// #[http_request(body)]
/// pub body: SampleBody,
///
/// #[http_request(header)]
/// pub header: http::HeaderMap,
/// }
/// ```
pub use http_request_derive_macros::HttpRequest;
#[cfg(test)]
mod tests {
use http::HeaderValue;
use pretty_assertions::assert_eq;
use serde::{Deserialize, Serialize};
use serde_json::json;
use url::Url;
use crate::HttpRequest;
#[test]
fn simple_get_request() {
#[derive(HttpRequest)]
#[http_request(method="GET", response = String, path = "/info")]
struct SimpleGetRequest;
let base_url = Url::parse("https://example.com/").expect("parse url");
let request = SimpleGetRequest;
let http_request = request
.to_http_request(&base_url)
.expect("build http request");
assert_eq!(&http_request.uri().to_string(), "https://example.com/info");
assert_eq!(http_request.method(), http::Method::GET);
assert!(http_request.into_body().is_empty());
}
#[test]
fn simple_json_post_request() {
#[derive(Serialize)]
struct MyRequestQuery {
start_x: usize,
end_x: usize,
start_y: usize,
end_y: usize,
}
#[derive(Serialize)]
struct MyRequestBody {
x: usize,
y: usize,
}
#[derive(HttpRequest)]
#[http_request(method = "POST", response = MyResponse, path = "/books/{id}/abstract")]
struct MyRequest {
#[http_request(query)]
query: MyRequestQuery,
#[http_request(body)]
body: MyRequestBody,
id: usize,
}
#[derive(Deserialize)]
struct MyResponse {}
let base_url = Url::parse("https://example.com/").expect("parse url");
let query = MyRequestQuery {
start_x: 10,
end_x: 50,
start_y: 20,
end_y: 30,
};
let body = MyRequestBody { x: 10, y: 20 };
let request = MyRequest {
query,
body,
id: 55,
};
let http_request = request
.to_http_request(&base_url)
.expect("build http request");
assert_eq!(
&http_request.uri().to_string(),
"https://example.com/books/55/abstract?start_x=10&end_x=50&start_y=20&end_y=30"
);
assert_eq!(http_request.method(), http::Method::POST);
assert_eq!(http_request.headers().len(), 1);
assert_eq!(
http_request.headers().get("content-type"),
Some(&HeaderValue::from_str("application/json").unwrap())
);
let http_request_body: serde_json::Value =
serde_json::from_slice(&http_request.into_body()).expect("json");
assert_eq!(http_request_body, json!({"x": 10, "y": 20}));
}
}