diff --git a/src/examples/client/client.rs b/src/examples/client/client.rs index 49670e4..5571c31 100644 --- a/src/examples/client/client.rs +++ b/src/examples/client/client.rs @@ -1,20 +1,16 @@ extern mod http; -use http::client::RequestWriter; -use http::method::Get; +use http::client::api::get; use http::headers::HeaderEnum; use std::str; use std::rt::io::Reader; -use std::rt::io::net::ip::{SocketAddr, Ipv4Addr}; fn main() { - let mut request = ~RequestWriter::new(Get, FromStr::from_str("http://localhost/example") - .expect("Uh oh, that's *really* badly broken!")); - // Temporary measure, as hostname lookup is not yet supported in std::rt::io. - request.remote_addr = Some(SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 8001 }); - let mut response = match request.read_response() { + let response = get(~"http://localhost/example"); + 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."); printfln!("Status: %s", response.status.to_str()); println("Headers:"); diff --git a/src/libhttp/client/api.rs b/src/libhttp/client/api.rs new file mode 100644 index 0000000..d386d42 --- /dev/null +++ b/src/libhttp/client/api.rs @@ -0,0 +1,53 @@ +use std::rt::io::net::tcp::TcpStream; +use std::rt::io::net::ip::{SocketAddr, Ipv4Addr}; + +use method; +use client::request::RequestWriter; +use client::response::ResponseReader; + +// TODO: Implement a Response trait + +pub fn request(method: method::Method, url: ~str) + -> Result, ~RequestWriter>{ + + let url = FromStr::from_str(url).expect("Uh oh, that's *really* badly broken!"); + let mut request = ~RequestWriter::new(method, url); + + // TODO: https://github.com/chris-morgan/rust-http/issues/10 + // Temporary measure, as hostname lookup is not yet supported in std::rt::io. + request.remote_addr = Some(SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 8001 }); + request.read_response() +} + +pub fn get(url: ~str) + -> Result, ~RequestWriter> { + + request(method::Get, url) +} + +// TODO: Add body +pub fn post(url: ~str) + -> Result, ~RequestWriter> { + + request(method::Post, url) +} + +// TODO: Add body +pub fn patch(url: ~str) + -> Result, ~RequestWriter> { + + request(method::Patch, url) +} + +// TODO: Add body +pub fn put(url: ~str) + -> Result, ~RequestWriter> { + + request(method::Put, url) +} + +pub fn delete(url: ~str) + -> Result, ~RequestWriter> { + + request(method::Delete, url) +} diff --git a/src/libhttp/client/mod.rs b/src/libhttp/client/mod.rs index 9cec5b5..a675e4a 100644 --- a/src/libhttp/client/mod.rs +++ b/src/libhttp/client/mod.rs @@ -1,5 +1,6 @@ pub use self::request::RequestWriter; pub use self::response::ResponseReader; +pub mod api; pub mod request; pub mod response;