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 Sep 6, 2013
1 parent a72a8e4 commit f1823d7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/examples/client/client.rs
Original file line number Diff line number Diff line change
@@ -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:");
Expand Down
53 changes: 53 additions & 0 deletions src/libhttp/client/api.rs
Original file line number Diff line number Diff line change
@@ -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<ResponseReader<TcpStream>, ~RequestWriter<TcpStream>>{

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<ResponseReader<TcpStream>, ~RequestWriter<TcpStream>> {

request(method::Get, url)
}

// TODO: Add body
pub fn post(url: ~str)
-> Result<ResponseReader<TcpStream>, ~RequestWriter<TcpStream>> {

request(method::Post, url)
}

// TODO: Add body
pub fn patch(url: ~str)
-> Result<ResponseReader<TcpStream>, ~RequestWriter<TcpStream>> {

request(method::Patch, url)
}

// TODO: Add body
pub fn put(url: ~str)
-> Result<ResponseReader<TcpStream>, ~RequestWriter<TcpStream>> {

request(method::Put, url)
}

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

request(method::Delete, url)
}
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;

0 comments on commit f1823d7

Please sign in to comment.