1#![doc = include_str!("../README.md")]
2use http::{header, Request};
3
4use crate::{
5 client::{AsyncClient, Client},
6 endpoint_prelude::Endpoint,
7 error::ApiError,
8 query::{AsyncQuery, Query},
9};
10
11pub mod client;
15pub mod endpoint;
16pub mod endpoint_prelude;
17pub mod error;
18pub mod params;
19pub mod query;
20
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub struct Raw<E> {
24 endpoint: E,
25}
26
27pub fn raw<E>(endpoint: E) -> Raw<E> {
29 Raw { endpoint }
30}
31
32impl<E, C> Query<Vec<u8>, C> for Raw<E>
33where
34 E: Endpoint,
35 C: Client,
36{
37 fn query(&self, client: &C) -> Result<Vec<u8>, ApiError<C::Error>> {
38 let mut url = client.rest_endpoint(&self.endpoint.endpoint())?;
39 self.endpoint.parameters().add_to_url(&mut url);
40
41 let req = Request::builder()
42 .method(self.endpoint.method())
43 .uri(query::url_to_http_uri(url));
44 let (req, data) = if let Some((mime, data)) = self.endpoint.body()? {
45 let req = req.header(header::CONTENT_TYPE, mime);
46 (req, data)
47 } else {
48 (req, Vec::new())
49 };
50 let rsp = client.rest(req, data)?;
51 if !rsp.status().is_success() {
52 return Err(ApiError::server_error(rsp.status(), rsp.body()));
53 }
54
55 Ok(rsp.into_body().as_ref().into())
56 }
57}
58
59impl<E, C> AsyncQuery<Vec<u8>, C> for Raw<E>
60where
61 E: Endpoint + Sync,
62 C: AsyncClient + Sync,
63{
64 async fn query_async(&self, client: &C) -> Result<Vec<u8>, ApiError<C::Error>> {
65 let mut url = client.rest_endpoint(&self.endpoint.endpoint())?;
66 self.endpoint.parameters().add_to_url(&mut url);
67
68 let req = Request::builder()
69 .method(self.endpoint.method())
70 .uri(query::url_to_http_uri(url));
71 let (req, data) = if let Some((mime, data)) = self.endpoint.body()? {
72 let req = req.header(header::CONTENT_TYPE, mime);
73 (req, data)
74 } else {
75 (req, Vec::new())
76 };
77 let rsp = client.rest_async(req, data).await?;
78 if !rsp.status().is_success() {
79 return Err(ApiError::server_error(rsp.status(), rsp.body()));
80 }
81
82 Ok(rsp.into_body().as_ref().into())
83 }
84}