Skip to main content

couchbase_core/httpx/
request.rs

1/*
2 *
3 *  * Copyright (c) 2025 Couchbase, Inc.
4 *  *
5 *  * Licensed under the Apache License, Version 2.0 (the "License");
6 *  * you may not use this file except in compliance with the License.
7 *  * You may obtain a copy of the License at
8 *  *
9 *  *    http://www.apache.org/licenses/LICENSE-2.0
10 *  *
11 *  * Unless required by applicable law or agreed to in writing, software
12 *  * distributed under the License is distributed on an "AS IS" BASIS,
13 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  * See the License for the specific language governing permissions and
15 *  * limitations under the License.
16 *
17 */
18
19use bytes::Bytes;
20use serde::Serialize;
21use std::collections::HashMap;
22
23#[derive(Debug)]
24#[non_exhaustive]
25pub struct Request {
26    pub(crate) method: http::Method,
27    pub(crate) uri: String,
28    pub(crate) auth: Option<Auth>,
29    pub(crate) user_agent: Option<String>,
30    pub(crate) content_type: Option<String>,
31    pub(crate) body: Option<Bytes>,
32    pub(crate) headers: HashMap<String, String>,
33    pub(crate) unique_id: Option<String>,
34}
35
36impl Request {
37    pub fn new(method: http::Method, uri: impl Into<String>) -> Self {
38        Self {
39            method,
40            uri: uri.into(),
41            auth: None,
42            user_agent: None,
43            content_type: None,
44            body: None,
45            headers: HashMap::new(),
46            unique_id: None,
47        }
48    }
49
50    pub fn auth(mut self, auth: impl Into<Option<Auth>>) -> Self {
51        self.auth = auth.into();
52        self
53    }
54
55    pub fn user_agent(mut self, user_agent: impl Into<Option<String>>) -> Self {
56        self.user_agent = user_agent.into();
57        self
58    }
59
60    pub fn content_type(mut self, content_type: impl Into<Option<String>>) -> Self {
61        self.content_type = content_type.into();
62        self
63    }
64
65    pub fn body(mut self, body: impl Into<Option<Bytes>>) -> Self {
66        self.body = body.into();
67        self
68    }
69
70    pub fn unique_id(mut self, unique_id: impl Into<Option<String>>) -> Self {
71        self.unique_id = unique_id.into();
72        self
73    }
74
75    pub fn add_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
76        self.headers.insert(key.into(), value.into());
77        self
78    }
79}
80
81#[derive(PartialEq, Eq, Debug, Clone)]
82pub struct BasicAuth {
83    pub(crate) username: String,
84    pub(crate) password: String,
85}
86
87impl BasicAuth {
88    pub fn new(username: impl Into<String>, password: impl Into<String>) -> Self {
89        Self {
90            username: username.into(),
91            password: password.into(),
92        }
93    }
94}
95
96#[derive(PartialEq, Eq, Debug, Clone)]
97pub struct BearerAuth {
98    pub(crate) token: String,
99}
100
101impl BearerAuth {
102    pub fn new(token: impl Into<String>) -> Self {
103        Self {
104            token: token.into(),
105        }
106    }
107}
108
109#[derive(Clone, PartialEq, Eq, Debug)]
110#[non_exhaustive]
111pub enum Auth {
112    BasicAuth(BasicAuth),
113    BearerAuth(BearerAuth),
114    OnBehalfOf(OnBehalfOfInfo),
115}
116
117#[derive(Clone, PartialEq, Eq, Debug, Serialize)]
118#[non_exhaustive]
119pub struct OnBehalfOfInfo {
120    pub(crate) username: String,
121    pub(crate) password_or_domain: OboPasswordOrDomain,
122}
123
124impl OnBehalfOfInfo {
125    pub fn new(username: impl Into<String>, password_or_domain: OboPasswordOrDomain) -> Self {
126        Self {
127            username: username.into(),
128            password_or_domain,
129        }
130    }
131}
132
133#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
134pub enum OboPasswordOrDomain {
135    Password(String),
136    Domain(String),
137}