added convenience functions get
and post
This commit is contained in:
parent
9e52588e0a
commit
309ed0b874
2 changed files with 40 additions and 2 deletions
42
src/lib.rs
42
src/lib.rs
|
@ -2,6 +2,9 @@
|
||||||
#![deny(unsafe_code)]
|
#![deny(unsafe_code)]
|
||||||
#![doc = include_str!("../README.md")]
|
#![doc = include_str!("../README.md")]
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
mod server;
|
||||||
|
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use std::{
|
use std::{
|
||||||
|
@ -12,6 +15,42 @@ use std::{
|
||||||
net::{IpAddr, Ipv4Addr, SocketAddr, TcpStream, UdpSocket},
|
net::{IpAddr, Ipv4Addr, SocketAddr, TcpStream, UdpSocket},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// GET the resource at an URL.
|
||||||
|
///
|
||||||
|
/// This is a convenience function over using [`Request::get`] and [`Request::send`].
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// May error if the provided URL is invalid, or if network issues arise.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// let response = request::get("localhost:8000").unwrap();
|
||||||
|
/// assert_eq!(response.status, 200);
|
||||||
|
/// ```
|
||||||
|
pub fn get(url: &str) -> Result<Response, io::Error> {
|
||||||
|
Request::get(url).send()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// POST a body to the URL.
|
||||||
|
///
|
||||||
|
/// This is a convenience function over using [`Request::post`] and [`Request::send`].
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// May error if the provided URL is invalid, or if network issues arise.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// let response = request::post("localhost:8000", "hello server!").unwrap();
|
||||||
|
/// assert_eq!(response.status, 200);
|
||||||
|
/// ```
|
||||||
|
pub fn post(url: &str, body: &str) -> Result<Response, io::Error> {
|
||||||
|
Request::post(url, body).send()
|
||||||
|
}
|
||||||
|
|
||||||
/// An HTTP request.
|
/// An HTTP request.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
|
@ -21,8 +60,7 @@ use std::{
|
||||||
/// use request::Request;
|
/// use request::Request;
|
||||||
///
|
///
|
||||||
/// // ... start a local server on port 8000 ...
|
/// // ... start a local server on port 8000 ...
|
||||||
/// let request = Request::get("localhost:8000");
|
/// let response = request::get("localhost:8000").unwrap();
|
||||||
/// let response = request.send().unwrap();
|
|
||||||
/// assert_eq!(response.status, 200);
|
/// assert_eq!(response.status, 200);
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
|
|
0
src/server.rs
Normal file
0
src/server.rs
Normal file
Loading…
Reference in a new issue