use std::{collections::HashMap, hash::Hash};
use bytes::Bytes;
use http::{StatusCode, status::InvalidStatusCode};
use reqwest::Method;
#[derive(Clone, Debug)]
#[cfg_attr(
feature = "python",
pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.network", from_py_object)
)]
pub struct HttpStatus {
inner: StatusCode,
}
impl HttpStatus {
#[must_use]
pub const fn new(code: StatusCode) -> Self {
Self { inner: code }
}
#[inline]
#[must_use]
pub const fn as_u16(&self) -> u16 {
self.inner.as_u16()
}
#[inline]
#[must_use]
pub fn as_str(&self) -> &str {
self.inner.as_str()
}
#[inline]
#[must_use]
pub fn is_informational(&self) -> bool {
self.inner.is_informational()
}
#[inline]
#[must_use]
pub fn is_success(&self) -> bool {
self.inner.is_success()
}
#[inline]
#[must_use]
pub fn is_redirection(&self) -> bool {
self.inner.is_redirection()
}
#[inline]
#[must_use]
pub fn is_client_error(&self) -> bool {
self.inner.is_client_error()
}
#[inline]
#[must_use]
pub fn is_server_error(&self) -> bool {
self.inner.is_server_error()
}
}
impl TryFrom<u16> for HttpStatus {
type Error = InvalidStatusCode;
fn try_from(code: u16) -> Result<Self, Self::Error> {
Ok(Self {
inner: StatusCode::from_u16(code)?,
})
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(
feature = "python",
pyo3::pyclass(
eq,
eq_int,
module = "nautilus_trader.core.nautilus_pyo3.network",
from_py_object
)
)]
#[cfg_attr(
feature = "python",
pyo3_stub_gen::derive::gen_stub_pyclass_enum(module = "nautilus_trader.network")
)]
pub enum HttpMethod {
GET,
POST,
PUT,
DELETE,
PATCH,
}
impl From<HttpMethod> for Method {
fn from(value: HttpMethod) -> Self {
match value {
HttpMethod::GET => Self::GET,
HttpMethod::POST => Self::POST,
HttpMethod::PUT => Self::PUT,
HttpMethod::DELETE => Self::DELETE,
HttpMethod::PATCH => Self::PATCH,
}
}
}
#[derive(Clone, Debug)]
#[cfg_attr(
feature = "python",
pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.network", from_py_object)
)]
#[cfg_attr(
feature = "python",
pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.network")
)]
pub struct HttpResponse {
pub status: HttpStatus,
pub headers: HashMap<String, String>,
pub body: Bytes,
}