-
Notifications
You must be signed in to change notification settings - Fork 13
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
Add context::Builder #24
Open
Meralis40
wants to merge
6
commits into
mehcode:master
Choose a base branch
from
Meralis40:contextbuilder
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fdca1bd
Add context::Builder
Meralis40 e968765
Add request::Builder & remove duplicated data from context::Builder
Meralis40 406966d
Merge branch 'master' into Meralis40_context_builder
mehcode 7e97e0a
Change methods names & error type
Meralis40 44cab3d
Merge branch 'contextbuilder' of github.com:Meralis40/shio-rs into co…
Meralis40 4a9d50c
Corrected build errors with introduction of http module
Meralis40 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
|
||
use tokio_core::reactor::Handle; | ||
use std::sync::Arc; | ||
use unsafe_any::UnsafeAny; | ||
use hyper::{Method, error, HttpVersion, Body}; | ||
use hyper::header::Header; | ||
|
||
use super::Context; | ||
use state::State; | ||
use util::typemap::TypeMap; | ||
use request; | ||
use data; | ||
|
||
/// Helper struct for construct a [`Context`] | ||
/// | ||
/// [`Context`]: struct.Context.html | ||
pub struct Builder { | ||
handle: Handle, | ||
|
||
state: State, | ||
|
||
// for request | ||
request: request::Builder, | ||
body: Body, | ||
} | ||
|
||
impl Builder { | ||
/// Create a new `Builder` from a tokio_core `Handle` | ||
pub fn new(handle: Handle) -> Self { | ||
Self { | ||
handle, | ||
state: State::default(), | ||
request: request::Builder::default(), | ||
body: Body::default(), | ||
} | ||
} | ||
|
||
/// Set the shared data. | ||
pub fn shared(mut self, shared: Arc<TypeMap<UnsafeAny + Send + Sync>>) -> Self { | ||
self.state.shared = shared; | ||
self | ||
} | ||
|
||
/// Set the HTTP method to `method` | ||
pub fn method(mut self, method: Method) -> Self { | ||
self.request = self.request.method(method); | ||
self | ||
} | ||
|
||
/// Set the uri | ||
pub fn uri(mut self, uri: &str) -> Self { | ||
self.request = self.request.uri(uri); | ||
self | ||
} | ||
|
||
/// Set the HTTP version | ||
pub fn version(mut self, ver: HttpVersion) -> Self { | ||
self.request = self.request.version(ver); | ||
self | ||
} | ||
|
||
/// Set an header | ||
pub fn set_header<H: Header>(mut self, value: H) -> Self { | ||
self.request = self.request.set_header(value); | ||
self | ||
} | ||
|
||
/// Set the request data | ||
pub fn set_data<B: Into<Body>>(mut self, body: B) -> Self { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as |
||
self.body = body.into(); | ||
self | ||
} | ||
|
||
/// Create the `Context`, returning any error that occurs during build. | ||
pub fn finalize(self) -> Result<Context, error::UriError> { | ||
let Self { | ||
handle, state, | ||
request, | ||
body | ||
} = self; | ||
|
||
Ok(Context::new(handle, request.finalize()?, state, data::Data::new(body))) | ||
} | ||
} | ||
|
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,69 @@ | ||
|
||
use hyper::{Method, Uri, error, HttpVersion, Headers}; | ||
use hyper::header::Header; | ||
|
||
use super::Request; | ||
|
||
/// Helper struct for construct a [`Request`] | ||
/// | ||
/// [`Request`]: struct.Context.html | ||
pub struct Builder { | ||
method: Method, | ||
uri: Result<Uri, error::UriError>, | ||
version: HttpVersion, | ||
headers: Headers, | ||
} | ||
|
||
impl Default for Builder { | ||
fn default() -> Self { | ||
Self { | ||
method: Method::Get, | ||
uri: "/".parse::<Uri>(), | ||
version: HttpVersion::Http11, | ||
headers: Headers::new(), | ||
} | ||
} | ||
} | ||
|
||
impl Builder { | ||
pub fn new() -> Self { | ||
Self::default() | ||
} | ||
|
||
/// Set the HTTP method to `method` | ||
pub fn method(mut self, method: Method) -> Self { | ||
self.method = method; | ||
self | ||
} | ||
|
||
/// Set the uri | ||
pub fn uri(mut self, uri: &str) -> Self { | ||
self.uri = uri.parse::<Uri>(); | ||
self | ||
} | ||
|
||
/// Set the HTTP version | ||
pub fn version(mut self, ver: HttpVersion) -> Self { | ||
self.version = ver; | ||
self | ||
} | ||
|
||
/// Set an header | ||
pub fn set_header<H: Header>(mut self, value: H) -> Self { | ||
self.headers.set(value); | ||
self | ||
} | ||
|
||
/// Create the `Context`, returning any error that occurs during build. | ||
pub fn finalize(self) -> Result<Request, error::UriError> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably be |
||
let Self { | ||
method, uri, | ||
version, headers, | ||
} = self; | ||
|
||
let uri = uri?; | ||
|
||
Ok(Request::new((method, uri, version, headers))) | ||
} | ||
} | ||
|
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Methods on a builder should be just
header
and notset_header
. See a similar method onhttp::request::Builder
: https://docs.rs/http/0.1.0/http/request/struct.Builder.html#method.header