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
use assert_impl::assert_impl;
use qiniu_credential::HeaderValue;
use qiniu_http::{
    Response as HttpResponse, ResponseErrorKind as HttpResponseErrorKind, ResponseParts as HttpResponseParts,
};
use serde::{de::DeserializeOwned, Deserialize};
use std::{
    io::copy as io_copy,
    mem::take,
    ops::{Deref, DerefMut},
};
use tap::TapFallible;

mod error;
pub(super) use error::XHeaders;
pub use error::{Error as ResponseError, ErrorKind as ResponseErrorKind};

/// API 响应结果
pub type ApiResult<T> = Result<T, ResponseError>;

use qiniu_http::SyncResponseBody;

#[cfg(feature = "async")]
use futures::io::copy as async_io_copy;

#[cfg(feature = "async")]
use qiniu_http::AsyncResponseBody;

const X_REQ_ID_HEADER_NAME: &str = "x-reqid";
const X_LOG_HEADER_NAME: &str = "x-log";

/// HTTP 响应
#[derive(Default, Debug)]
pub struct Response<B>(HttpResponse<B>);

impl<B> Response<B> {
    /// 转换为 HTTP 响应体
    #[inline]
    pub fn into_body(self) -> B {
        self.0.into_body()
    }

    /// 转换为 HTTP 响应信息和响应体
    #[inline]
    pub fn into_parts_and_body(self) -> (HttpResponseParts, B) {
        self.0.into_parts_and_body()
    }

    /// 根据 HTTP 响应信息和响应体创建 HTTP 响应
    #[inline]
    pub fn from_parts_and_body(parts: HttpResponseParts, body: B) -> Self {
        Self(HttpResponse::from_parts_and_body(parts, body))
    }

    /// 获取 HTTP 响应的 X-ReqId 信息
    #[inline]
    pub fn x_reqid(&self) -> Option<&HeaderValue> {
        self.header(X_REQ_ID_HEADER_NAME)
    }

    /// 获取 HTTP 响应的 X-Log 信息
    #[inline]
    pub fn x_log(&self) -> Option<&HeaderValue> {
        self.header(X_LOG_HEADER_NAME)
    }
}

impl<B> From<HttpResponse<B>> for Response<B> {
    #[inline]
    fn from(response: HttpResponse<B>) -> Self {
        Self(response)
    }
}

impl<B> From<Response<B>> for HttpResponse<B> {
    #[inline]
    fn from(response: Response<B>) -> Self {
        response.0
    }
}

impl<B> Deref for Response<B> {
    type Target = HttpResponse<B>;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<B> DerefMut for Response<B> {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<B: Sync + Send> Response<B> {
    #[allow(dead_code)]
    fn assert() {
        assert_impl!(Send: Self);
        assert_impl!(Sync: Self);
    }
}

impl Response<SyncResponseBody> {
    /// 解析 JSON 响应体
    pub fn parse_json<T: DeserializeOwned>(self) -> ApiResult<Response<T>> {
        let x_headers = XHeaders::from(self.parts());
        let mut got_body = Vec::new();
        let json_response = self
            .fulfill()?
            .try_map_body(|mut body| parse_json_from_slice(&body).tap_err(|_| got_body = take(&mut body)))
            .map_err(|err| {
                ResponseError::from_http_response_error(
                    err.into_response_error(HttpResponseErrorKind::ReceiveError),
                    x_headers,
                    Some(ResponseErrorKind::ParseResponseError),
                )
                .set_response_body_sample(got_body)
            })?;
        Ok(Response::from(json_response))
    }

    pub(super) fn fulfill(self) -> ApiResult<HttpResponse<Vec<u8>>> {
        let x_headers = XHeaders::from(self.parts());
        self.0
            .try_map_body(|mut body| {
                let mut buf = Vec::new();
                io_copy(&mut body, &mut buf).map(|_| buf)
            })
            .map_err(|err| {
                ResponseError::from_http_response_error(
                    err.into_response_error(HttpResponseErrorKind::ReceiveError),
                    x_headers,
                    None,
                )
            })
    }
}

#[cfg(feature = "async")]
impl Response<AsyncResponseBody> {
    /// 异步解析 JSON 响应体
    pub async fn parse_json<T: DeserializeOwned>(self) -> ApiResult<Response<T>> {
        let x_headers = XHeaders::from(self.parts());
        let mut got_body = Vec::new();
        let json_response = self
            .fulfill()
            .await?
            .try_map_body(|mut body| parse_json_from_slice(&body).tap_err(|_| got_body = take(&mut body)))
            .map_err(|err| {
                ResponseError::from_http_response_error(
                    err.into_response_error(HttpResponseErrorKind::ReceiveError),
                    x_headers,
                    Some(ResponseErrorKind::ParseResponseError),
                )
                .set_response_body_sample(got_body)
            })?;
        Ok(Response::from(json_response))
    }

    pub(super) async fn fulfill(self) -> ApiResult<HttpResponse<Vec<u8>>> {
        let x_headers = XHeaders::from(self.parts());
        self.0
            .try_async_map_body(|mut body| async move {
                let mut buf = Vec::new();
                async_io_copy(&mut body, &mut buf).await.map(|_| buf)
            })
            .await
            .map_err(|err| {
                ResponseError::from_http_response_error(
                    err.into_response_error(HttpResponseErrorKind::ReceiveError),
                    x_headers,
                    None,
                )
            })
    }
}

fn parse_json_from_slice<'a, T: Deserialize<'a>>(v: &'a [u8]) -> serde_json::Result<T> {
    // Sometimes the API are supposed to response with JSON but actually Empty!
    if v.as_ref().is_empty() {
        serde_json::from_slice(b"{}".as_slice())
    } else {
        serde_json::from_slice(v)
    }
}

/// 阻塞 HTTP 响应
pub type SyncResponse = Response<SyncResponseBody>;

/// 异步 HTTP 响应
#[cfg(feature = "async")]
#[cfg_attr(feature = "docs", doc(cfg(feature = "async")))]
pub type AsyncResponse = Response<AsyncResponseBody>;