diff --git a/cloudflare-examples/src/main.rs b/cloudflare-examples/src/main.rs index fcca03b3..58272adc 100644 --- a/cloudflare-examples/src/main.rs +++ b/cloudflare-examples/src/main.rs @@ -135,6 +135,44 @@ fn list_routes(arg_matches: &ArgMatches, api_client: & print_response_json(response); } +fn list_scripts(arg_matches: &ArgMatches, api_client: &ApiClientType) { + let usage = "usage: list_scripts ACCOUNT_ID"; + + let account_id_missing = format!("missing '{}': {}", "ACCOUNT_ID", usage); + let account_identifier = arg_matches + .value_of("account_identifier") + .expect(&account_id_missing); + + let response = api_client.request(&workers::ListScripts { account_identifier }); + + print_response_json(response); +} + + +fn create_script(arg_matches: &ArgMatches, api_client: &ApiClientType) { + let usage = "usage: create_script ACCOUNT_ID SCRIPT_NAME"; + + let script_name_missing = format!("missing '{}': {}", "SCRIPT_NAME", usage); + let script_name = arg_matches + .value_of("script_name") + .expect(&script_name_missing); + + let script_content = String::from("addEventListener('fetch', event => { event.respondWith(fetch(event.request)) })"); + + let account_id_missing = format!("missing '{}': {}", "ACCOUNT_ID", usage); + let account_identifier = arg_matches + .value_of("account_identifier") + .expect(&account_id_missing); + + let response = api_client.request(&workers::CreateScript { + account_identifier, + script_name, + script_content, + }); + + print_response_json(response); +} + fn list_accounts(_arg_matches: &ArgMatches, api_client: &ApiClientType) { let response = api_client.request(&account::ListAccounts { params: None }); @@ -228,6 +266,21 @@ fn main() -> Result<(), Box> { description: "Activate a Worker on a Route", function: list_routes }, + "list_scripts" => Section{ + args: vec![ + Arg::with_name("account_identifier").required(true), + ], + description: "List Workers on an account", + function: list_scripts + }, + "create_script" => Section{ + args: vec![ + Arg::with_name("account_identifier").required(true), + Arg::with_name("script_name").required(true), + ], + description: "Create or update a Worker", + function: create_script + }, "list_accounts" => Section{ args: vec![], description: "List accounts", diff --git a/cloudflare/src/endpoints/workers/create_script.rs b/cloudflare/src/endpoints/workers/create_script.rs new file mode 100644 index 00000000..d872b3af --- /dev/null +++ b/cloudflare/src/endpoints/workers/create_script.rs @@ -0,0 +1,30 @@ +use super::WorkersScript; + +use crate::framework::endpoint::{Endpoint, Method}; + +/// Create Script +/// Makes a blep +/// https://api.cloudflare.com/#worker-script-upload-worker +pub struct CreateScript<'a> { + pub account_identifier: &'a str, + pub script_name: &'a str, + pub script_content: String, +} + +impl<'a> Endpoint for CreateScript<'a> { + fn method(&self) -> Method { + Method::Put + } + fn path(&self) -> String { + format!( + "accounts/{}/workers/scripts/{}", + self.account_identifier, self.script_name + ) + } + fn serialized_body(&self) -> Option { + Some(self.script_content.clone()) + } + fn content_type(&self) -> String { + "application/javascript".to_owned() + } +} diff --git a/cloudflare/src/endpoints/workers/list_scripts.rs b/cloudflare/src/endpoints/workers/list_scripts.rs new file mode 100644 index 00000000..cdd2aa15 --- /dev/null +++ b/cloudflare/src/endpoints/workers/list_scripts.rs @@ -0,0 +1,22 @@ +use super::WorkersScript; + +use crate::framework::endpoint::{Endpoint, Method}; + +/// List Scripts +/// Lists all scripts for a given account +/// https://api.cloudflare.com/#worker-script-list-workers +pub struct ListScripts<'a> { + pub account_identifier: &'a str, +} + +impl<'a> Endpoint> for ListScripts<'a> { + fn method(&self) -> Method { + Method::Get + } + fn path(&self) -> String { + format!( + "accounts/{}/workers/scripts", + self.account_identifier + ) + } +} diff --git a/cloudflare/src/endpoints/workers/mod.rs b/cloudflare/src/endpoints/workers/mod.rs index 81252f54..4246654d 100644 --- a/cloudflare/src/endpoints/workers/mod.rs +++ b/cloudflare/src/endpoints/workers/mod.rs @@ -6,17 +6,21 @@ use serde::Deserialize; mod create_route; mod create_secret; +mod create_script; mod delete_route; mod delete_secret; mod list_routes; mod list_secrets; +mod list_scripts; pub use create_route::{CreateRoute, CreateRouteParams}; pub use create_secret::{CreateSecret, CreateSecretParams}; +pub use create_script::{CreateScript}; pub use delete_route::DeleteRoute; pub use delete_secret::DeleteSecret; pub use list_routes::ListRoutes; pub use list_secrets::ListSecrets; +pub use list_scripts::ListScripts; /// Workers KV Route /// Routes are basic patterns used to enable or disable workers that match requests. @@ -60,3 +64,15 @@ pub struct WorkersSecret { impl ApiResult for WorkersSecret {} impl ApiResult for Vec {} // to parse arrays too + +#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)] +pub struct WorkersScript { + pub id: String, + pub etag: String, + pub script: Option, + pub size: Option, + pub created_on: Option>, + pub modified_on: DateTime, +} +impl ApiResult for WorkersScript {} +impl ApiResult for Vec {}