refactor/feat: Response::bytes() and text(), rename body->stream
This commit is contained in:
parent
2ca51a5a20
commit
16af1c1bd0
2 changed files with 21 additions and 5 deletions
|
@ -11,6 +11,7 @@ categories = ["network-programming"]
|
|||
|
||||
[dependencies]
|
||||
base64ct = "1.6.0"
|
||||
bytes = "1.7.1"
|
||||
mime = "0.3.17"
|
||||
num_enum = "0.7.3"
|
||||
sha2 = "0.10.8"
|
||||
|
|
|
@ -1,20 +1,23 @@
|
|||
use crate::{status::Status, LibError};
|
||||
|
||||
use bytes::Bytes;
|
||||
use tokio::io::AsyncReadExt;
|
||||
|
||||
type BodyStream = tokio_rustls::client::TlsStream<tokio::net::TcpStream>;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Response {
|
||||
status: Status,
|
||||
message: String,
|
||||
body: BodyStream,
|
||||
stream: BodyStream,
|
||||
}
|
||||
|
||||
impl Response {
|
||||
pub fn new(status: Status, message: String, body: BodyStream) -> Self {
|
||||
pub fn new(status: Status, message: String, stream: BodyStream) -> Self {
|
||||
Response {
|
||||
status,
|
||||
message,
|
||||
body,
|
||||
stream,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -30,7 +33,19 @@ impl Response {
|
|||
self.message.parse().map_err(|e| LibError::InvalidMime(e))
|
||||
}
|
||||
|
||||
pub fn body(self: &mut Self) -> &mut BodyStream {
|
||||
&mut self.body
|
||||
pub fn stream(self: &mut Self) -> &mut BodyStream {
|
||||
&mut self.stream
|
||||
}
|
||||
|
||||
pub async fn bytes(self: &mut Self) -> Result<Bytes, LibError> {
|
||||
let mut buf = Vec::new();
|
||||
self.stream.read_to_end(&mut buf).await?;
|
||||
Ok(Bytes::from(buf))
|
||||
}
|
||||
|
||||
pub async fn text(self: &mut Self) -> Result<String, LibError> {
|
||||
let mut buf = String::new();
|
||||
self.stream.read_to_string(&mut buf).await?;
|
||||
Ok(buf)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue