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