@@ -4,6 +4,7 @@ use miette::{IntoDiagnostic, Result, miette};
44use reqwest:: header:: { AUTHORIZATION , HeaderMap , HeaderValue } ;
55use reqwest:: multipart:: Part ;
66use reqwest:: { Client , multipart} ;
7+ use serde:: { Deserialize , Serialize } ;
78use std:: sync:: OnceLock ;
89use tokio:: fs:: read;
910use tracing:: { debug, error} ;
@@ -16,6 +17,15 @@ use responses::CloudflareResponse;
1617
1718use crate :: artifacts:: CloudflareManifest ;
1819
20+ #[ derive( Debug , Clone , Serialize , Deserialize ) ]
21+ pub struct WorkerScript {
22+ pub id : String ,
23+ pub created_on : String ,
24+ pub modified_on : String ,
25+ pub usage_model : Option < String > ,
26+ pub etag : String ,
27+ }
28+
1929static URL : OnceLock < Url > = OnceLock :: new ( ) ;
2030
2131fn init_url ( ) -> Url {
@@ -28,12 +38,10 @@ pub struct CloudflareClient {
2838 client : Client ,
2939 /// This is the Cloudflare account id
3040 account_id : String ,
31- /// The name of the Cloudflare worker
32- worker_name : String ,
3341}
3442
3543impl CloudflareClient {
36- pub fn new ( account_id : String , worker_name : String , token : & str ) -> Self {
44+ pub fn new ( account_id : String , token : & str ) -> Self {
3745 // TODO: Add a timeout.
3846 let mut default_headers = HeaderMap :: new ( ) ;
3947 let auth = format ! ( "Bearer {token}" ) ;
@@ -46,11 +54,7 @@ impl CloudflareClient {
4654 . build ( )
4755 . expect ( "Must be able to construct client" ) ;
4856
49- Self {
50- client,
51- account_id,
52- worker_name,
53- }
57+ Self { client, account_id }
5458 }
5559
5660 // Commented out until we verify if we need an upload session.
@@ -164,12 +168,12 @@ impl CloudflareClient {
164168 // https://developers.cloudflare.com/api/resources/workers/subresources/scripts/subresources/versions/methods/create/
165169 pub async fn upload_version (
166170 & self ,
171+ worker_name : & String ,
167172 manifest : & CloudflareManifest ,
168173 main_module : & String ,
169174 ) -> Result < UploadVersionResponse > {
170175 debug ! ( "Uploading Worker version" ) ;
171176 let account_id = & self . account_id ;
172- let worker_name = & self . worker_name ;
173177 let path = format ! ( "accounts/{account_id}/workers/scripts/{worker_name}/versions" ) ;
174178 let url = Self :: url_with_path ( & path) ;
175179
@@ -224,10 +228,9 @@ impl CloudflareClient {
224228
225229 // Corresponds to:
226230 // https://developers.cloudflare.com/api/resources/workers/subresources/scripts/subresources/deployments/methods/get/
227- pub async fn get_current_version ( & self ) -> Result < String > {
231+ pub async fn get_current_version ( & self , worker_name : & String ) -> Result < String > {
228232 debug ! ( "Getting current Worker version" ) ;
229233 let account_id = & self . account_id ;
230- let worker_name = & self . worker_name ;
231234 let path = format ! ( "accounts/{account_id}/workers/scripts/{worker_name}/deployments" ) ;
232235 let url = Self :: url_with_path ( & path) ;
233236
@@ -263,10 +266,13 @@ impl CloudflareClient {
263266
264267 // Corresponds to:
265268 // https://developers.cloudflare.com/api/resources/workers/subresources/scripts/subresources/deployments/methods/create/
266- pub async fn create_deployment ( & self , request : CreateDeploymentRequest ) -> Result < ( ) > {
269+ pub async fn create_deployment (
270+ & self ,
271+ worker_name : & String ,
272+ request : CreateDeploymentRequest ,
273+ ) -> Result < ( ) > {
267274 debug ! ( "Deploying updated version(s)" ) ;
268275 let account_id = & self . account_id ;
269- let worker_name = & self . worker_name ;
270276 let path = format ! ( "accounts/{account_id}/workers/scripts/{worker_name}/deployments" ) ;
271277 let url = Self :: url_with_path ( & path) ;
272278
@@ -291,14 +297,14 @@ impl CloudflareClient {
291297 // For the monitor to grab metrics within a time range.
292298 pub async fn collect_metrics (
293299 & self ,
294- worker_version_id : String ,
300+ worker_name : & String ,
301+ worker_version_id : & String ,
295302 status_code_range_start : u16 ,
296303 status_code_range_end : u16 ,
297304 from_time : DateTime < chrono:: Utc > ,
298305 to_time : DateTime < chrono:: Utc > ,
299306 ) -> Result < u32 > {
300307 let account_id = & self . account_id ;
301- let worker_name = & self . worker_name ;
302308 let path = format ! ( "accounts/{account_id}/workers/observability/telemetry/query" ) ;
303309 let url = Self :: url_with_path ( & path) ;
304310
@@ -390,6 +396,32 @@ impl CloudflareClient {
390396 Ok ( count)
391397 }
392398
399+ // List all workers for the account
400+ // Corresponds to: https://developers.cloudflare.com/api/resources/workers/subresources/scripts/methods/list/
401+ pub async fn list_workers ( & self ) -> Result < Vec < WorkerScript > > {
402+ debug ! ( "Listing Cloudflare Workers" ) ;
403+ let account_id = & self . account_id ;
404+ let path = format ! ( "accounts/{account_id}/workers/scripts" ) ;
405+ let url = Self :: url_with_path ( & path) ;
406+
407+ let response = self . client . get ( url) . send ( ) . await . into_diagnostic ( ) ?;
408+
409+ if !response. status ( ) . is_success ( ) {
410+ return Err ( miette ! (
411+ "Failed to list Workers. Error: {:?}" ,
412+ response. json:: <serde_json:: Value >( ) . await
413+ ) ) ;
414+ }
415+
416+ let workers_response = response
417+ . json :: < CloudflareResponse < Vec < WorkerScript > > > ( )
418+ . await
419+ . into_diagnostic ( ) ?;
420+
421+ debug ! ( "Workers listed successfully" ) ;
422+ Ok ( workers_response. result )
423+ }
424+
393425 fn base_url ( ) -> & ' static Url {
394426 URL . get_or_init ( init_url)
395427 }
0 commit comments