opendal/services/vercel_artifacts/
core.rs1use std::fmt::Debug;
19use std::sync::Arc;
20
21use http::header;
22use http::Request;
23use http::Response;
24
25use crate::raw::*;
26use crate::*;
27
28pub struct VercelArtifactsCore {
29 pub info: Arc<AccessorInfo>,
30 pub(crate) access_token: String,
31}
32
33impl Debug for VercelArtifactsCore {
34 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35 let mut de = f.debug_struct("VercelArtifactsCore");
36 de.field("access_token", &self.access_token);
37 de.finish()
38 }
39}
40
41impl VercelArtifactsCore {
42 pub(crate) async fn vercel_artifacts_get(
43 &self,
44 hash: &str,
45 range: BytesRange,
46 _: &OpRead,
47 ) -> Result<Response<HttpBody>> {
48 let url: String = format!(
49 "https://api.vercel.com/v8/artifacts/{}",
50 percent_encode_path(hash)
51 );
52
53 let mut req = Request::get(&url);
54
55 if !range.is_full() {
56 req = req.header(header::RANGE, range.to_header());
57 }
58
59 let auth_header_content = format!("Bearer {}", self.access_token);
60 req = req.header(header::AUTHORIZATION, auth_header_content);
61
62 let req = req.body(Buffer::new()).map_err(new_request_build_error)?;
63
64 self.info.http_client().fetch(req).await
65 }
66
67 pub(crate) async fn vercel_artifacts_put(
68 &self,
69 hash: &str,
70 size: u64,
71 body: Buffer,
72 ) -> Result<Response<Buffer>> {
73 let url = format!(
74 "https://api.vercel.com/v8/artifacts/{}",
75 percent_encode_path(hash)
76 );
77
78 let mut req = Request::put(&url);
79
80 let auth_header_content = format!("Bearer {}", self.access_token);
81 req = req.header(header::CONTENT_TYPE, "application/octet-stream");
82 req = req.header(header::AUTHORIZATION, auth_header_content);
83 req = req.header(header::CONTENT_LENGTH, size);
84
85 let req = req.body(body).map_err(new_request_build_error)?;
86
87 self.info.http_client().send(req).await
88 }
89
90 pub(crate) async fn vercel_artifacts_stat(&self, hash: &str) -> Result<Response<Buffer>> {
91 let url = format!(
92 "https://api.vercel.com/v8/artifacts/{}",
93 percent_encode_path(hash)
94 );
95
96 let mut req = Request::head(&url);
97
98 let auth_header_content = format!("Bearer {}", self.access_token);
99 req = req.header(header::AUTHORIZATION, auth_header_content);
100 req = req.header(header::CONTENT_LENGTH, 0);
101
102 let req = req.body(Buffer::new()).map_err(new_request_build_error)?;
103
104 self.info.http_client().send(req).await
105 }
106}