Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: response http version #21

Merged
merged 1 commit into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
})
}