cloudflare_but_works/endpoints/workers/
create_route.rs

1use super::WorkersRouteIdOnly;
2
3use crate::framework::endpoint::{EndpointSpec, Method, RequestBody};
4
5use crate::framework::response::ApiSuccess;
6use serde::Serialize;
7
8/// Create a Route
9/// Creates a route mapping the given pattern to the given script
10/// <https://api.cloudflare.com/#worker-routes-create-route>
11#[derive(Debug)]
12pub struct CreateRoute<'a> {
13    pub zone_identifier: &'a str,
14    pub params: CreateRouteParams,
15}
16
17impl EndpointSpec for CreateRoute<'_> {
18    type JsonResponse = WorkersRouteIdOnly;
19    type ResponseType = ApiSuccess<Self::JsonResponse>;
20
21    fn method(&self) -> Method {
22        Method::POST
23    }
24    fn path(&self) -> String {
25        format!("zones/{}/workers/routes", self.zone_identifier)
26    }
27    #[inline]
28    fn body(&self) -> Option<RequestBody> {
29        let body = serde_json::to_string(&self.params).unwrap();
30        Some(RequestBody::Json(body))
31    }
32}
33
34/// pattern: the zone name along with glob-style wildcards
35///         e.g. "example.net/*"
36/// script: Name of the script to apply when the route is matched.
37///         The route is skipped when this is blank/missing.
38#[derive(Serialize, Clone, Debug)]
39pub struct CreateRouteParams {
40    pub pattern: String,
41    pub script: Option<String>,
42}