|
| 1 | +// Copyright 2026-Present Datadog, Inc. https://www.datadoghq.com/ |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! HTTP capability trait and types. |
| 5 | +
|
| 6 | +use crate::maybe_send::MaybeSend; |
| 7 | +use core::fmt; |
| 8 | +use core::future::Future; |
| 9 | + |
| 10 | +#[derive(Debug, Clone)] |
| 11 | +pub struct HttpResponse { |
| 12 | + pub status: u16, |
| 13 | + pub body: Vec<u8>, |
| 14 | +} |
| 15 | + |
| 16 | +#[derive(Debug, Clone)] |
| 17 | +pub enum HttpError { |
| 18 | + Network(String), |
| 19 | + Timeout, |
| 20 | + InvalidRequest(String), |
| 21 | + Other(String), |
| 22 | +} |
| 23 | + |
| 24 | +impl fmt::Display for HttpError { |
| 25 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 26 | + match self { |
| 27 | + HttpError::Network(msg) => write!(f, "Network error: {}", msg), |
| 28 | + HttpError::Timeout => write!(f, "Request timed out"), |
| 29 | + HttpError::InvalidRequest(msg) => write!(f, "Invalid request: {}", msg), |
| 30 | + HttpError::Other(msg) => write!(f, "HTTP error: {}", msg), |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +impl std::error::Error for HttpError {} |
| 36 | + |
| 37 | +/// Request without body (GET, HEAD, DELETE, OPTIONS). |
| 38 | +#[derive(Debug, Clone, Default)] |
| 39 | +pub struct RequestHead { |
| 40 | + pub url: String, |
| 41 | + pub headers: Vec<(String, String)>, |
| 42 | +} |
| 43 | + |
| 44 | +impl RequestHead { |
| 45 | + pub fn new(url: impl Into<String>) -> Self { |
| 46 | + Self { |
| 47 | + url: url.into(), |
| 48 | + headers: Vec::new(), |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + pub fn with_header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self { |
| 53 | + self.headers.push((name.into(), value.into())); |
| 54 | + self |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/// Request with body (POST, PUT, PATCH). |
| 59 | +#[derive(Debug, Clone)] |
| 60 | +pub struct RequestWithBody { |
| 61 | + pub head: RequestHead, |
| 62 | + pub body: Vec<u8>, |
| 63 | +} |
| 64 | + |
| 65 | +impl RequestWithBody { |
| 66 | + pub fn new(url: impl Into<String>, body: Vec<u8>) -> Self { |
| 67 | + Self { |
| 68 | + head: RequestHead::new(url), |
| 69 | + body, |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + pub fn with_header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self { |
| 74 | + self.head = self.head.with_header(name, value); |
| 75 | + self |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +#[derive(Debug, Clone)] |
| 80 | +pub enum HttpRequest { |
| 81 | + Get(RequestHead), |
| 82 | + Head(RequestHead), |
| 83 | + Delete(RequestHead), |
| 84 | + Options(RequestHead), |
| 85 | + Post(RequestWithBody), |
| 86 | + Put(RequestWithBody), |
| 87 | + Patch(RequestWithBody), |
| 88 | +} |
| 89 | + |
| 90 | +impl HttpRequest { |
| 91 | + pub fn get(url: impl Into<String>) -> Self { |
| 92 | + Self::Get(RequestHead::new(url)) |
| 93 | + } |
| 94 | + |
| 95 | + pub fn head(url: impl Into<String>) -> Self { |
| 96 | + Self::Head(RequestHead::new(url)) |
| 97 | + } |
| 98 | + |
| 99 | + pub fn delete(url: impl Into<String>) -> Self { |
| 100 | + Self::Delete(RequestHead::new(url)) |
| 101 | + } |
| 102 | + |
| 103 | + pub fn options(url: impl Into<String>) -> Self { |
| 104 | + Self::Options(RequestHead::new(url)) |
| 105 | + } |
| 106 | + |
| 107 | + pub fn post(url: impl Into<String>, body: Vec<u8>) -> Self { |
| 108 | + Self::Post(RequestWithBody::new(url, body)) |
| 109 | + } |
| 110 | + |
| 111 | + pub fn put(url: impl Into<String>, body: Vec<u8>) -> Self { |
| 112 | + Self::Put(RequestWithBody::new(url, body)) |
| 113 | + } |
| 114 | + |
| 115 | + pub fn patch(url: impl Into<String>, body: Vec<u8>) -> Self { |
| 116 | + Self::Patch(RequestWithBody::new(url, body)) |
| 117 | + } |
| 118 | + |
| 119 | + pub fn with_header(self, name: impl Into<String>, value: impl Into<String>) -> Self { |
| 120 | + match self { |
| 121 | + Self::Get(head) => Self::Get(head.with_header(name, value)), |
| 122 | + Self::Head(head) => Self::Head(head.with_header(name, value)), |
| 123 | + Self::Delete(head) => Self::Delete(head.with_header(name, value)), |
| 124 | + Self::Options(head) => Self::Options(head.with_header(name, value)), |
| 125 | + Self::Post(req) => Self::Post(req.with_header(name, value)), |
| 126 | + Self::Put(req) => Self::Put(req.with_header(name, value)), |
| 127 | + Self::Patch(req) => Self::Patch(req.with_header(name, value)), |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + pub fn url(&self) -> &str { |
| 132 | + match self { |
| 133 | + Self::Get(h) | Self::Head(h) | Self::Delete(h) | Self::Options(h) => &h.url, |
| 134 | + Self::Post(r) | Self::Put(r) | Self::Patch(r) => &r.head.url, |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + pub fn headers(&self) -> &[(String, String)] { |
| 139 | + match self { |
| 140 | + Self::Get(h) | Self::Head(h) | Self::Delete(h) | Self::Options(h) => &h.headers, |
| 141 | + Self::Post(r) | Self::Put(r) | Self::Patch(r) => &r.head.headers, |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + pub fn body(&self) -> &[u8] { |
| 146 | + match self { |
| 147 | + Self::Get(_) | Self::Head(_) | Self::Delete(_) | Self::Options(_) => &[], |
| 148 | + Self::Post(r) | Self::Put(r) | Self::Patch(r) => &r.body, |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + pub fn into_body(self) -> Vec<u8> { |
| 153 | + match self { |
| 154 | + Self::Get(_) | Self::Head(_) | Self::Delete(_) | Self::Options(_) => Vec::new(), |
| 155 | + Self::Post(r) | Self::Put(r) | Self::Patch(r) => r.body, |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + pub fn method_str(&self) -> &'static str { |
| 160 | + match self { |
| 161 | + Self::Get(_) => "GET", |
| 162 | + Self::Head(_) => "HEAD", |
| 163 | + Self::Delete(_) => "DELETE", |
| 164 | + Self::Options(_) => "OPTIONS", |
| 165 | + Self::Post(_) => "POST", |
| 166 | + Self::Put(_) => "PUT", |
| 167 | + Self::Patch(_) => "PATCH", |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +pub trait HttpClientTrait { |
| 173 | + fn request( |
| 174 | + req: HttpRequest, |
| 175 | + ) -> impl Future<Output = Result<HttpResponse, HttpError>> + MaybeSend; |
| 176 | +} |
0 commit comments