|
| 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 | +} |
0 commit comments