Skip to content

Commit

Permalink
feat: save response version
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen committed Oct 29, 2023
1 parent 25fbad7 commit e87b184
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions hitt-cli/src/printing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub const TEXT_RESET: &str = "\x1B[39m";

pub(crate) fn print_response(response: HittResponse, args: &CliArguments) {
print_status(
&response.http_version,
&response.method,
&response.url,
response.status_code.as_u16(),
Expand Down
14 changes: 12 additions & 2 deletions hitt-cli/src/printing/status.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
use hitt_parser::http;

use crate::printing::{STYLE_BOLD, STYLE_RESET, TEXT_GREEN, TEXT_RED, TEXT_RESET};

#[inline]
pub(crate) fn print_status(method: &str, url: &str, status_code: u16) {
pub(crate) fn print_status(
http_version: &http::version::Version,
method: &str,
url: &str,
status_code: u16,
) {
let text_color = if status_code < 400 {
TEXT_GREEN
} else {
TEXT_RED
};

println!("{STYLE_BOLD}{text_color}{method} {url} {status_code}{TEXT_RESET}{STYLE_RESET}");
println!(
"{STYLE_BOLD}{text_color}{:?} {method} {url} {status_code}{TEXT_RESET}{STYLE_RESET}",
http_version
);
}
2 changes: 2 additions & 0 deletions hitt-parser/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::str::FromStr;

pub use http;

#[derive(Debug)]
pub enum RequestParseError {
InvalidHttpMethod(http::method::InvalidMethod),
Expand Down
6 changes: 5 additions & 1 deletion hitt-request/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use hitt_parser::HittRequest;
use hitt_parser::{http, HittRequest};

pub struct HittResponse {
pub url: String,
pub method: String,
pub status_code: reqwest::StatusCode,
pub headers: reqwest::header::HeaderMap,
pub body: String,
pub http_version: http::version::Version,
}

pub async fn send_request(
Expand All @@ -27,6 +28,8 @@ pub async fn send_request(
let status_code = res.status();
let headers = res.headers().to_owned();

let http_version = res.version();

let body = res.text().await.unwrap_or_default();

Ok(HittResponse {
Expand All @@ -35,5 +38,6 @@ pub async fn send_request(
status_code,
headers,
body,
http_version,
})
}

0 comments on commit e87b184

Please sign in to comment.