Skip to content

Commit

Permalink
Designing the API chris-morgan#9
Browse files Browse the repository at this point in the history
  • Loading branch information
flaper87 committed Oct 5, 2013
1 parent a5c4eb3 commit 56bab8a
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 19 deletions.
32 changes: 13 additions & 19 deletions src/examples/client/client.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
extern mod http;
use http::client::RequestWriter;
use http::method::{Get, Post};
use http::headers::HeaderEnum;
use std::str;
use http::client::api::{get, post, RequestArgs};
use http::headers::HeaderEnum;
use std::rt::io::extensions::ReaderUtil;

fn get_request() {
let request = ~RequestWriter::new(Get, FromStr::from_str("http://httpbin.org/get")
.expect("Uh oh, that's *really* badly broken!"));
let params = ~[(~"test", ~"value")];
let args = RequestArgs{params: Some(params), headers: None, data: None};
let response = get(~"http://httpbin.org/get", Some(args));

let mut response = match request.read_response() {
let mut response = match response {
Ok(response) => response,
Err(_request) => fail!("This example can progress no further with no response :-("),
};
Expand All @@ -20,38 +20,32 @@ fn get_request() {
for header in response.headers.iter() {
println!(" - {}: {}", header.header_name(), header.header_value());
}

print("\n");
println("Response:");
println(str::from_utf8(response.read_to_end()));
}


fn post_request() {
let mut request = ~RequestWriter::new(Post, FromStr::from_str("http://httpbin.org/post")
.expect("Uh oh, that's *really* badly broken!"));
let args = RequestArgs{params: None, headers: None,
data: Some("Some data".as_bytes().to_owned())};

request.send("Post It!".as_bytes());
let response = post(~"http://httpbin.org/post", Some(args));

let mut response = match request.read_response() {
let mut response = match response {
Ok(response) => response,
Err(_request) => fail!("This example can progress no further with no response :-("),
};

println("Yay! Started to get the response.");
println!("Status: {}", response.status);
println("Headers:");
for header in response.headers.iter() {
println!(" - {}: {}", header.header_name(), header.header_value());
}

print("\n");
println("Response:");
println(str::from_utf8(response.read_to_end()));
}

fn main() {

// Without data
get_request();

// With data
post_request();
}
94 changes: 94 additions & 0 deletions src/libhttp/client/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use std::default::Default;
use std::rt::io::net::tcp::TcpStream;

use method;
use headers::request::HeaderCollection;

use client::request::RequestWriter;
use client::response::ResponseReader;
use extra::url::{Url, Query};

pub struct RequestArgs {

// Request data
data: Option<~[u8]>,

// Query Parameters
params: Option<Query>,

// Request Headers
headers: Option<~HeaderCollection>,
}

impl Default for RequestArgs {

fn default() -> RequestArgs {
RequestArgs{data: None, params: None, headers: None}
}
}

// Need a fix for https://github.com/mozilla/rust/issues/9056
// before we can use this.
//pub static DEFAULT_ARGS: RequestArgs = RequestArgs{params: None,
// headers: None};

// TODO: Implement a Response trait

pub fn request(method: method::Method, url: ~str, args: Option<RequestArgs>)
-> Result<ResponseReader<TcpStream>, ~RequestWriter<TcpStream>>{

let default = args.unwrap_or_default();

// Push all query params to the URL.
let mut url: Url = FromStr::from_str(url).expect("Uh oh, that's *really* badly broken!");

if default.params.is_some() {
url.query.push_all(*(default.params.get_ref()));
}

// At this point, we're ready to finally send
// the request. First thing is to write headers,
// then the request data and later get the response
// from the server.
let mut request = ~RequestWriter::new(method, url);

// Write data if there's some
if default.data.is_some() {
request.send(*(default.data.get_ref()));
}

// This will flush the request's
// stream and get the response from
// the server.
request.read_response()
}

pub fn get(url: ~str, args: Option<RequestArgs>)
-> Result<ResponseReader<TcpStream>, ~RequestWriter<TcpStream>> {

request(method::Get, url, args)
}

pub fn post(url: ~str, args: Option<RequestArgs>)
-> Result<ResponseReader<TcpStream>, ~RequestWriter<TcpStream>> {

request(method::Post, url, args)
}

pub fn patch(url: ~str, args: Option<RequestArgs>)
-> Result<ResponseReader<TcpStream>, ~RequestWriter<TcpStream>> {

request(method::Patch, url, args)
}

pub fn put(url: ~str, args: Option<RequestArgs>)
-> Result<ResponseReader<TcpStream>, ~RequestWriter<TcpStream>> {

request(method::Put, url, args)
}

pub fn delete(url: ~str, args: Option<RequestArgs>)
-> Result<ResponseReader<TcpStream>, ~RequestWriter<TcpStream>> {

request(method::Delete, url, args)
}
1 change: 1 addition & 0 deletions src/libhttp/client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub use self::request::RequestWriter;
pub use self::response::ResponseReader;

pub mod api;
pub mod request;
pub mod response;
4 changes: 4 additions & 0 deletions src/libhttp/client/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ impl<S: Stream> ResponseReader<S> {
headers: headers,
})
}

pub fn get_content(&mut self) -> ~[u8] {
self.stream.read_to_end()
}
}

impl<S: Stream> Reader for ResponseReader<S> {
Expand Down

0 comments on commit 56bab8a

Please sign in to comment.