opendal/services/cloudflare_kv/
backend.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  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,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::fmt::Debug;
19use std::fmt::Formatter;
20
21use bytes::Buf;
22use http::header;
23use http::Request;
24use http::StatusCode;
25use serde::Deserialize;
26
27use super::error::parse_error;
28use crate::raw::adapters::kv;
29use crate::raw::*;
30use crate::services::CloudflareKvConfig;
31use crate::ErrorKind;
32use crate::*;
33
34impl Configurator for CloudflareKvConfig {
35    type Builder = CloudflareKvBuilder;
36    fn into_builder(self) -> Self::Builder {
37        CloudflareKvBuilder {
38            config: self,
39            http_client: None,
40        }
41    }
42}
43
44#[doc = include_str!("docs.md")]
45#[derive(Default)]
46pub struct CloudflareKvBuilder {
47    config: CloudflareKvConfig,
48
49    /// The HTTP client used to communicate with CloudFlare.
50    http_client: Option<HttpClient>,
51}
52
53impl Debug for CloudflareKvBuilder {
54    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
55        f.debug_struct("CloudFlareKvBuilder")
56            .field("config", &self.config)
57            .finish()
58    }
59}
60
61impl CloudflareKvBuilder {
62    /// Set the token used to authenticate with CloudFlare.
63    pub fn token(mut self, token: &str) -> Self {
64        if !token.is_empty() {
65            self.config.token = Some(token.to_string())
66        }
67        self
68    }
69
70    /// Set the account ID used to authenticate with CloudFlare.
71    pub fn account_id(mut self, account_id: &str) -> Self {
72        if !account_id.is_empty() {
73            self.config.account_id = Some(account_id.to_string())
74        }
75        self
76    }
77
78    /// Set the namespace ID.
79    pub fn namespace_id(mut self, namespace_id: &str) -> Self {
80        if !namespace_id.is_empty() {
81            self.config.namespace_id = Some(namespace_id.to_string())
82        }
83        self
84    }
85
86    /// Set the root within this backend.
87    pub fn root(mut self, root: &str) -> Self {
88        self.config.root = if root.is_empty() {
89            None
90        } else {
91            Some(root.to_string())
92        };
93
94        self
95    }
96}
97
98impl Builder for CloudflareKvBuilder {
99    const SCHEME: Scheme = Scheme::CloudflareKv;
100    type Config = CloudflareKvConfig;
101
102    fn build(self) -> Result<impl Access> {
103        let authorization = match &self.config.token {
104            Some(token) => format_authorization_by_bearer(token)?,
105            None => return Err(Error::new(ErrorKind::ConfigInvalid, "token is required")),
106        };
107
108        let Some(account_id) = self.config.account_id.clone() else {
109            return Err(Error::new(
110                ErrorKind::ConfigInvalid,
111                "account_id is required",
112            ));
113        };
114
115        let Some(namespace_id) = self.config.namespace_id.clone() else {
116            return Err(Error::new(
117                ErrorKind::ConfigInvalid,
118                "namespace_id is required",
119            ));
120        };
121
122        let client = if let Some(client) = self.http_client {
123            client
124        } else {
125            HttpClient::new().map_err(|err| {
126                err.with_operation("Builder::build")
127                    .with_context("service", Scheme::CloudflareKv)
128            })?
129        };
130
131        let root = normalize_root(
132            self.config
133                .root
134                .clone()
135                .unwrap_or_else(|| "/".to_string())
136                .as_str(),
137        );
138
139        let url_prefix = format!(
140            r"https://api.cloudflare.com/client/v4/accounts/{account_id}/storage/kv/namespaces/{namespace_id}"
141        );
142
143        Ok(CloudflareKvBackend::new(Adapter {
144            authorization,
145            account_id,
146            namespace_id,
147            client,
148            url_prefix,
149        })
150        .with_normalized_root(root))
151    }
152}
153
154pub type CloudflareKvBackend = kv::Backend<Adapter>;
155
156#[derive(Clone)]
157pub struct Adapter {
158    authorization: String,
159    account_id: String,
160    namespace_id: String,
161    client: HttpClient,
162    url_prefix: String,
163}
164
165impl Debug for Adapter {
166    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
167        f.debug_struct("Adapter")
168            .field("account_id", &self.account_id)
169            .field("namespace_id", &self.namespace_id)
170            .finish()
171    }
172}
173
174impl Adapter {
175    fn sign<T>(&self, mut req: Request<T>) -> Result<Request<T>> {
176        req.headers_mut()
177            .insert(header::AUTHORIZATION, self.authorization.parse().unwrap());
178        Ok(req)
179    }
180}
181
182impl kv::Adapter for Adapter {
183    type Scanner = kv::Scanner;
184
185    fn info(&self) -> kv::Info {
186        kv::Info::new(
187            Scheme::CloudflareKv,
188            &self.namespace_id,
189            Capability {
190                read: true,
191                write: true,
192                list: true,
193                shared: true,
194
195                ..Default::default()
196            },
197        )
198    }
199
200    async fn get(&self, path: &str) -> Result<Option<Buffer>> {
201        let url = format!("{}/values/{}", self.url_prefix, path);
202        let mut req = Request::get(&url);
203        req = req.header(header::CONTENT_TYPE, "application/json");
204        let mut req = req.body(Buffer::new()).map_err(new_request_build_error)?;
205        req = self.sign(req)?;
206        let resp = self.client.send(req).await?;
207        let status = resp.status();
208        match status {
209            StatusCode::OK => Ok(Some(resp.into_body())),
210            _ => Err(parse_error(resp)),
211        }
212    }
213
214    async fn set(&self, path: &str, value: Buffer) -> Result<()> {
215        let url = format!("{}/values/{}", self.url_prefix, path);
216        let req = Request::put(&url);
217        let multipart = Multipart::new();
218        let multipart = multipart
219            .part(FormDataPart::new("metadata").content(serde_json::Value::Null.to_string()))
220            .part(FormDataPart::new("value").content(value.to_vec()));
221        let mut req = multipart.apply(req)?;
222        req = self.sign(req)?;
223        let resp = self.client.send(req).await?;
224        let status = resp.status();
225        match status {
226            StatusCode::OK => Ok(()),
227            _ => Err(parse_error(resp)),
228        }
229    }
230
231    async fn delete(&self, path: &str) -> Result<()> {
232        let url = format!("{}/values/{}", self.url_prefix, path);
233        let mut req = Request::delete(&url);
234        req = req.header(header::CONTENT_TYPE, "application/json");
235        let mut req = req.body(Buffer::new()).map_err(new_request_build_error)?;
236        req = self.sign(req)?;
237        let resp = self.client.send(req).await?;
238        let status = resp.status();
239        match status {
240            StatusCode::OK => Ok(()),
241            _ => Err(parse_error(resp)),
242        }
243    }
244
245    async fn scan(&self, path: &str) -> Result<Self::Scanner> {
246        let mut url = format!("{}/keys", self.url_prefix);
247        if !path.is_empty() {
248            url = format!("{url}?prefix={path}");
249        }
250        let mut req = Request::get(&url);
251        req = req.header(header::CONTENT_TYPE, "application/json");
252        let mut req = req.body(Buffer::new()).map_err(new_request_build_error)?;
253        req = self.sign(req)?;
254        let resp = self.client.send(req).await?;
255        let status = resp.status();
256        match status {
257            StatusCode::OK => {
258                let body = resp.into_body();
259                let response: CfKvScanResponse =
260                    serde_json::from_reader(body.reader()).map_err(|e| {
261                        Error::new(
262                            ErrorKind::Unexpected,
263                            format!("failed to parse error response: {e}"),
264                        )
265                    })?;
266                Ok(Box::new(kv::ScanStdIter::new(
267                    response.result.into_iter().map(|r| Ok(r.name)),
268                )))
269            }
270            _ => Err(parse_error(resp)),
271        }
272    }
273}
274
275#[derive(Debug, Deserialize)]
276pub(super) struct CfKvResponse {
277    pub(super) errors: Vec<CfKvError>,
278}
279
280#[derive(Debug, Deserialize)]
281pub(super) struct CfKvScanResponse {
282    result: Vec<CfKvScanResult>,
283    // According to https://developers.cloudflare.com/api/operations/workers-kv-namespace-list-a-namespace'-s-keys, result_info is used to determine if there are more keys to be listed
284    // result_info: Option<CfKvResultInfo>,
285}
286
287#[derive(Debug, Deserialize)]
288struct CfKvScanResult {
289    name: String,
290}
291
292// #[derive(Debug, Deserialize)]
293// struct CfKvResultInfo {
294//     count: i64,
295//     cursor: String,
296// }
297
298#[derive(Debug, Deserialize)]
299pub(super) struct CfKvError {
300    pub(super) code: i32,
301}
302
303#[cfg(test)]
304mod test {
305    use super::*;
306
307    #[test]
308    fn test_deserialize_scan_json_response() {
309        let json_str = r#"{
310			"errors": [],
311			"messages": [],
312			"result": [
313				{
314				"expiration": 1577836800,
315				"metadata": {
316					"someMetadataKey": "someMetadataValue"
317				},
318				"name": "My-Key"
319				}
320			],
321			"success": true,
322			"result_info": {
323				"count": 1,
324				"cursor": "6Ck1la0VxJ0djhidm1MdX2FyDGxLKVeeHZZmORS_8XeSuhz9SjIJRaSa2lnsF01tQOHrfTGAP3R5X1Kv5iVUuMbNKhWNAXHOl6ePB0TUL8nw"
325			}
326		}"#;
327
328        let response: CfKvScanResponse = serde_json::from_slice(json_str.as_bytes()).unwrap();
329
330        assert_eq!(response.result.len(), 1);
331        assert_eq!(response.result[0].name, "My-Key");
332        // assert!(response.result_info.is_some());
333        // if let Some(result_info) = response.result_info {
334        //     assert_eq!(result_info.count, 1);
335        //     assert_eq!(result_info.cursor, "6Ck1la0VxJ0djhidm1MdX2FyDGxLKVeeHZZmORS_8XeSuhz9SjIJRaSa2lnsF01tQOHrfTGAP3R5X1Kv5iVUuMbNKhWNAXHOl6ePB0TUL8nw");
336        // }
337    }
338
339    #[test]
340    fn test_deserialize_json_response() {
341        let json_str = r#"{
342			"errors": [],
343			"messages": [],
344			"result": {},
345			"success": true
346		}"#;
347
348        let response: CfKvResponse = serde_json::from_slice(json_str.as_bytes()).unwrap();
349
350        assert_eq!(response.errors.len(), 0);
351    }
352}