Skip to content

Commit 56bab8a

Browse files
committed
Designing the API chris-morgan#9
1 parent a5c4eb3 commit 56bab8a

File tree

4 files changed

+112
-19
lines changed

4 files changed

+112
-19
lines changed

src/examples/client/client.rs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
extern mod http;
2-
use http::client::RequestWriter;
3-
use http::method::{Get, Post};
4-
use http::headers::HeaderEnum;
52
use std::str;
3+
use http::client::api::{get, post, RequestArgs};
4+
use http::headers::HeaderEnum;
65
use std::rt::io::extensions::ReaderUtil;
76

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

12-
let mut response = match request.read_response() {
12+
let mut response = match response {
1313
Ok(response) => response,
1414
Err(_request) => fail!("This example can progress no further with no response :-("),
1515
};
@@ -20,38 +20,32 @@ fn get_request() {
2020
for header in response.headers.iter() {
2121
println!(" - {}: {}", header.header_name(), header.header_value());
2222
}
23+
2324
print("\n");
2425
println("Response:");
2526
println(str::from_utf8(response.read_to_end()));
2627
}
2728

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

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

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

40-
println("Yay! Started to get the response.");
41-
println!("Status: {}", response.status);
42-
println("Headers:");
43-
for header in response.headers.iter() {
44-
println!(" - {}: {}", header.header_name(), header.header_value());
45-
}
46-
47-
print("\n");
4840
println("Response:");
4941
println(str::from_utf8(response.read_to_end()));
5042
}
5143

5244
fn main() {
5345

46+
// Without data
5447
get_request();
5548

49+
// With data
5650
post_request();
5751
}

src/libhttp/client/api.rs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
use std::default::Default;
2+
use std::rt::io::net::tcp::TcpStream;
3+
4+
use method;
5+
use headers::request::HeaderCollection;
6+
7+
use client::request::RequestWriter;
8+
use client::response::ResponseReader;
9+
use extra::url::{Url, Query};
10+
11+
pub struct RequestArgs {
12+
13+
// Request data
14+
data: Option<~[u8]>,
15+
16+
// Query Parameters
17+
params: Option<Query>,
18+
19+
// Request Headers
20+
headers: Option<~HeaderCollection>,
21+
}
22+
23+
impl Default for RequestArgs {
24+
25+
fn default() -> RequestArgs {
26+
RequestArgs{data: None, params: None, headers: None}
27+
}
28+
}
29+
30+
// Need a fix for https://github.com/mozilla/rust/issues/9056
31+
// before we can use this.
32+
//pub static DEFAULT_ARGS: RequestArgs = RequestArgs{params: None,
33+
// headers: None};
34+
35+
// TODO: Implement a Response trait
36+
37+
pub fn request(method: method::Method, url: ~str, args: Option<RequestArgs>)
38+
-> Result<ResponseReader<TcpStream>, ~RequestWriter<TcpStream>>{
39+
40+
let default = args.unwrap_or_default();
41+
42+
// Push all query params to the URL.
43+
let mut url: Url = FromStr::from_str(url).expect("Uh oh, that's *really* badly broken!");
44+
45+
if default.params.is_some() {
46+
url.query.push_all(*(default.params.get_ref()));
47+
}
48+
49+
// At this point, we're ready to finally send
50+
// the request. First thing is to write headers,
51+
// then the request data and later get the response
52+
// from the server.
53+
let mut request = ~RequestWriter::new(method, url);
54+
55+
// Write data if there's some
56+
if default.data.is_some() {
57+
request.send(*(default.data.get_ref()));
58+
}
59+
60+
// This will flush the request's
61+
// stream and get the response from
62+
// the server.
63+
request.read_response()
64+
}
65+
66+
pub fn get(url: ~str, args: Option<RequestArgs>)
67+
-> Result<ResponseReader<TcpStream>, ~RequestWriter<TcpStream>> {
68+
69+
request(method::Get, url, args)
70+
}
71+
72+
pub fn post(url: ~str, args: Option<RequestArgs>)
73+
-> Result<ResponseReader<TcpStream>, ~RequestWriter<TcpStream>> {
74+
75+
request(method::Post, url, args)
76+
}
77+
78+
pub fn patch(url: ~str, args: Option<RequestArgs>)
79+
-> Result<ResponseReader<TcpStream>, ~RequestWriter<TcpStream>> {
80+
81+
request(method::Patch, url, args)
82+
}
83+
84+
pub fn put(url: ~str, args: Option<RequestArgs>)
85+
-> Result<ResponseReader<TcpStream>, ~RequestWriter<TcpStream>> {
86+
87+
request(method::Put, url, args)
88+
}
89+
90+
pub fn delete(url: ~str, args: Option<RequestArgs>)
91+
-> Result<ResponseReader<TcpStream>, ~RequestWriter<TcpStream>> {
92+
93+
request(method::Delete, url, args)
94+
}

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;

src/libhttp/client/response.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ impl<S: Stream> ResponseReader<S> {
141141
headers: headers,
142142
})
143143
}
144+
145+
pub fn get_content(&mut self) -> ~[u8] {
146+
self.stream.read_to_end()
147+
}
144148
}
145149

146150
impl<S: Stream> Reader for ResponseReader<S> {

0 commit comments

Comments
 (0)