-
-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
59 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use quick_error::quick_error; | ||
use std::io; | ||
quick_error! { | ||
#[derive(Debug)] | ||
pub enum Error { | ||
Detail(description: String) { | ||
display("{}", description) | ||
} | ||
PostBody(err: io::Error) { | ||
display("An IO error occurred while uploading the body of a POST request") | ||
from() | ||
source(err) | ||
} | ||
} | ||
} | ||
|
||
pub struct GetResponse<H, B> { | ||
pub headers: H, | ||
pub body: B, | ||
} | ||
|
||
pub struct PostResponse<H, B, PB> { | ||
/// **Note**: Implementations should drop the handle to avoid deadlocks | ||
pub post_body: PB, | ||
pub headers: H, | ||
pub body: B, | ||
} | ||
|
||
impl<A, B, C> From<PostResponse<A, B, C>> for GetResponse<A, B> { | ||
fn from(v: PostResponse<A, B, C>) -> Self { | ||
GetResponse { | ||
headers: v.headers, | ||
body: v.body, | ||
} | ||
} | ||
} | ||
|
||
#[allow(clippy::type_complexity)] | ||
pub trait Http { | ||
type Headers: io::BufRead; | ||
type ResponseBody: io::BufRead; | ||
type PostBody: io::Write; | ||
|
||
fn get( | ||
&mut self, | ||
url: &str, | ||
headers: impl IntoIterator<Item = impl AsRef<str>>, | ||
) -> Result<GetResponse<Self::Headers, Self::ResponseBody>, Error>; | ||
fn post( | ||
&mut self, | ||
url: &str, | ||
headers: impl IntoIterator<Item = impl AsRef<str>>, | ||
) -> Result<PostResponse<Self::Headers, Self::ResponseBody, Self::PostBody>, Error>; | ||
} |