quickhttp/
response.rs

1use std::collections::HashMap;
2use crate::errors::RequestError;
3use crate::request::Request;
4use crate::status_code::StatusCode;
5use crate::ValidRequest;
6/// Describes the response of an HTTP request
7pub trait ValidResponse {
8    /// Resend the request that generated this response
9    fn resend(&self) -> Result<Response, RequestError>;
10}
11
12/// Describes the response of an HTTP request, and contains the response data
13#[derive(Clone)]
14pub struct Response {
15    /// The status code of the response
16    pub status_code: StatusCode,
17
18    /// The raw response, as a string
19    pub raw_response: String,
20
21    /// The headers of the response
22    pub headers: HashMap<String, String>,
23
24    /// The body of the response
25    pub body: String,
26
27    /// An exact copy of the request that generated this response
28    pub request_used: Request,
29}
30
31impl core::fmt::Debug for Response {
32    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
33        write!(f, "{}", self.raw_response)
34    }
35}
36
37impl core::fmt::Display for Response {
38    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
39        write!(f, "{}", self.raw_response)
40    }
41}
42
43impl ValidResponse for Response {
44    /// Resend the request that generated this response
45    fn resend(&self) -> Result<Response, RequestError> {
46        let request = self.request_used.clone();
47        request.send()
48    }
49}