Skip to content

Commit 5bd694d

Browse files
committed
Designing the API chris-morgan#9
1 parent a5c4eb3 commit 5bd694d

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/libhttp/client/api.rs

Lines changed: 53 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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)