Skip to content

Commit f1823d7

Browse files
committed
Designing the API chris-morgan#9
1 parent a72a8e4 commit f1823d7

File tree

3 files changed

+58
-8
lines changed

3 files changed

+58
-8
lines changed

src/examples/client/client.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
extern mod http;
2-
use http::client::RequestWriter;
3-
use http::method::Get;
2+
use http::client::api::get;
43
use http::headers::HeaderEnum;
54
use std::str;
65
use std::rt::io::Reader;
7-
use std::rt::io::net::ip::{SocketAddr, Ipv4Addr};
86

97
fn main() {
10-
let mut request = ~RequestWriter::new(Get, FromStr::from_str("http://localhost/example")
11-
.expect("Uh oh, that's *really* badly broken!"));
12-
// Temporary measure, as hostname lookup is not yet supported in std::rt::io.
13-
request.remote_addr = Some(SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 8001 });
14-
let mut response = match request.read_response() {
8+
let response = get(~"http://localhost/example");
9+
let mut response = match response {
1510
Ok(response) => response,
1611
Err(_request) => fail!("This example can progress no further with no response :-("),
1712
};
13+
1814
println("Yay! Started to get the response.");
1915
printfln!("Status: %s", response.status.to_str());
2016
println("Headers:");

src/libhttp/client/api.rs

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use std::rt::io::net::tcp::TcpStream;
2+
use std::rt::io::net::ip::{SocketAddr, Ipv4Addr};
3+
4+
use method;
5+
use client::request::RequestWriter;
6+
use client::response::ResponseReader;
7+
8+
// TODO: Implement a Response trait
9+
10+
pub fn request(method: method::Method, url: ~str)
11+
-> Result<ResponseReader<TcpStream>, ~RequestWriter<TcpStream>>{
12+
13+
let url = FromStr::from_str(url).expect("Uh oh, that's *really* badly broken!");
14+
let mut request = ~RequestWriter::new(method, url);
15+
16+
// TODO: https://github.com/chris-morgan/rust-http/issues/10
17+
// Temporary measure, as hostname lookup is not yet supported in std::rt::io.
18+
request.remote_addr = Some(SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 8001 });
19+
request.read_response()
20+
}
21+
22+
pub fn get(url: ~str)
23+
-> Result<ResponseReader<TcpStream>, ~RequestWriter<TcpStream>> {
24+
25+
request(method::Get, url)
26+
}
27+
28+
// TODO: Add body
29+
pub fn post(url: ~str)
30+
-> Result<ResponseReader<TcpStream>, ~RequestWriter<TcpStream>> {
31+
32+
request(method::Post, url)
33+
}
34+
35+
// TODO: Add body
36+
pub fn patch(url: ~str)
37+
-> Result<ResponseReader<TcpStream>, ~RequestWriter<TcpStream>> {
38+
39+
request(method::Patch, url)
40+
}
41+
42+
// TODO: Add body
43+
pub fn put(url: ~str)
44+
-> Result<ResponseReader<TcpStream>, ~RequestWriter<TcpStream>> {
45+
46+
request(method::Put, url)
47+
}
48+
49+
pub fn delete(url: ~str)
50+
-> Result<ResponseReader<TcpStream>, ~RequestWriter<TcpStream>> {
51+
52+
request(method::Delete, url)
53+
}

src/libhttp/client/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub use self::request::RequestWriter;
22
pub use self::response::ResponseReader;
33

4+
pub mod api;
45
pub mod request;
56
pub mod response;

0 commit comments

Comments
 (0)