added convenience functions get and post

This commit is contained in:
Frederik Palmø 2024-03-04 12:05:42 +01:00
parent 9e52588e0a
commit 309ed0b874
2 changed files with 40 additions and 2 deletions

View file

@ -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
View file