use hyper::method::Method;
use super::response::{HttpResponse, http_response};
pub trait HttpEndpoint: Send + Copy {
fn hostname(&self) -> &'static str;
fn port(&self) -> u16;
fn host(&self) -> String {
format!("{}:{}", self.hostname(), self.port())
}
fn protocol(&self) -> &'static str {
match self.port() {
443 => "https",
_ => "http"
}
}
fn url(&self) -> String {
match self.port() {
443 | 80 => format!("{}://{}", self.protocol(), self.hostname()),
_ => format!("{}://{}", self.protocol(), self.host())
}
}
fn url_with_path(&self, path: &str) -> String {
format!("{}{}", self.url(), path)
}
fn options(&self, path: &'static str) -> HttpResponse<Self> {
http_response(*self, Method::Options, path)
}
fn get(&self, path: &'static str) -> HttpResponse<Self> {
http_response(*self, Method::Get, path)
}
fn post(&self, path: &'static str) -> HttpResponse<Self> {
http_response(*self, Method::Post, path)
}
fn put(&self, path: &'static str) -> HttpResponse<Self> {
http_response(*self, Method::Put, path)
}
fn delete(&self, path: &'static str) -> HttpResponse<Self> {
http_response(*self, Method::Delete, path)
}
fn head(&self, path: &'static str) -> HttpResponse<Self> {
http_response(*self, Method::Head, path)
}
fn trace(&self, path: &'static str) -> HttpResponse<Self> {
http_response(*self, Method::Trace, path)
}
fn connect(&self, path: &'static str) -> HttpResponse<Self> {
http_response(*self, Method::Connect, path)
}
fn patch(&self, path: &'static str) -> HttpResponse<Self> {
http_response(*self, Method::Patch, path)
}
fn ext(&self, http_verb: &'static str, path: &'static str) -> HttpResponse<Self> {
http_response(*self, Method::Extension(http_verb.to_string()), path)
}
}