fastly_api/apis/
waf_rules_api.rs

1/*
2 * Fastly API
3 *
4 * Via the Fastly API you can perform any of the operations that are possible within the management console,  including creating services, domains, and backends, configuring rules or uploading your own application code, as well as account operations such as user administration and billing reports. The API is organized into collections of endpoints that allow manipulation of objects related to Fastly services and accounts. For the most accurate and up-to-date API reference content, visit our [Developer Hub](https://www.fastly.com/documentation/reference/api/) 
5 *
6 */
7
8
9use reqwest;
10
11use crate::apis::ResponseContent;
12use super::{Error, configuration};
13
14/// struct for passing parameters to the method [`get_waf_rule`]
15#[derive(Clone, Debug, Default)]
16pub struct GetWafRuleParams {
17    /// Alphanumeric string identifying a WAF rule.
18    pub waf_rule_id: String,
19    /// Include relationships. Optional, comma-separated values. Permitted values: `waf_tags` and `waf_rule_revisions`. 
20    pub include: Option<String>
21}
22
23/// struct for passing parameters to the method [`list_waf_rules`]
24#[derive(Clone, Debug, Default)]
25pub struct ListWafRulesParams {
26    /// Limit the returned rules to a specific ModSecurity rule ID.
27    pub filter_modsec_rule_id: Option<String>,
28    /// Limit the returned rules to a set linked to a tag by name.
29    pub filter_waf_tags_name: Option<String>,
30    /// Limit the returned rules to a set linked to a source.
31    pub filter_waf_rule_revisions_source: Option<String>,
32    /// Limit the returned rules to a set not included in the active firewall version for a firewall.
33    pub filter_waf_firewall_id_not_match: Option<String>,
34    /// Current page.
35    pub page_number: Option<i32>,
36    /// Number of records per page.
37    pub page_size: Option<i32>,
38    /// Include relationships. Optional, comma-separated values. Permitted values: `waf_tags` and `waf_rule_revisions`. 
39    pub include: Option<String>
40}
41
42
43/// struct for typed errors of method [`get_waf_rule`]
44#[derive(Debug, Clone, Serialize, Deserialize)]
45#[serde(untagged)]
46pub enum GetWafRuleError {
47    UnknownValue(serde_json::Value),
48}
49
50/// struct for typed errors of method [`list_waf_rules`]
51#[derive(Debug, Clone, Serialize, Deserialize)]
52#[serde(untagged)]
53pub enum ListWafRulesError {
54    UnknownValue(serde_json::Value),
55}
56
57
58/// Get a specific rule. The `id` provided can be the ModSecurity Rule ID or the Fastly generated rule ID.
59pub async fn get_waf_rule(configuration: &mut configuration::Configuration, params: GetWafRuleParams) -> Result<crate::models::WafRuleResponse, Error<GetWafRuleError>> {
60    let local_var_configuration = configuration;
61
62    // unbox the parameters
63    let waf_rule_id = params.waf_rule_id;
64    let include = params.include;
65
66
67    let local_var_client = &local_var_configuration.client;
68
69    let local_var_uri_str = format!("{}/waf/rules/{waf_rule_id}", local_var_configuration.base_path, waf_rule_id=crate::apis::urlencode(waf_rule_id));
70    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
71
72    if let Some(ref local_var_str) = include {
73        local_var_req_builder = local_var_req_builder.query(&[("include", &local_var_str.to_string())]);
74    }
75    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
76        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
77    }
78    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
79        let local_var_key = local_var_apikey.key.clone();
80        let local_var_value = match local_var_apikey.prefix {
81            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
82            None => local_var_key,
83        };
84        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
85    };
86
87    let local_var_req = local_var_req_builder.build()?;
88    let local_var_resp = local_var_client.execute(local_var_req).await?;
89
90    if "GET" != "GET" && "GET" != "HEAD" {
91      let headers = local_var_resp.headers();
92      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
93          Some(v) => v.to_str().unwrap().parse().unwrap(),
94          None => configuration::DEFAULT_RATELIMIT,
95      };
96      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
97          Some(v) => v.to_str().unwrap().parse().unwrap(),
98          None => 0,
99      };
100    }
101
102    let local_var_status = local_var_resp.status();
103    let local_var_content = local_var_resp.text().await?;
104
105    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
106        serde_json::from_str(&local_var_content).map_err(Error::from)
107    } else {
108        let local_var_entity: Option<GetWafRuleError> = serde_json::from_str(&local_var_content).ok();
109        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
110        Err(Error::ResponseError(local_var_error))
111    }
112}
113
114/// List all available WAF rules.
115pub async fn list_waf_rules(configuration: &mut configuration::Configuration, params: ListWafRulesParams) -> Result<crate::models::WafRulesResponse, Error<ListWafRulesError>> {
116    let local_var_configuration = configuration;
117
118    // unbox the parameters
119    let filter_modsec_rule_id = params.filter_modsec_rule_id;
120    let filter_waf_tags_name = params.filter_waf_tags_name;
121    let filter_waf_rule_revisions_source = params.filter_waf_rule_revisions_source;
122    let filter_waf_firewall_id_not_match = params.filter_waf_firewall_id_not_match;
123    let page_number = params.page_number;
124    let page_size = params.page_size;
125    let include = params.include;
126
127
128    let local_var_client = &local_var_configuration.client;
129
130    let local_var_uri_str = format!("{}/waf/rules", local_var_configuration.base_path);
131    let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
132
133    if let Some(ref local_var_str) = filter_modsec_rule_id {
134        local_var_req_builder = local_var_req_builder.query(&[("filter[modsec_rule_id]", &local_var_str.to_string())]);
135    }
136    if let Some(ref local_var_str) = filter_waf_tags_name {
137        local_var_req_builder = local_var_req_builder.query(&[("filter[waf_tags][name]", &local_var_str.to_string())]);
138    }
139    if let Some(ref local_var_str) = filter_waf_rule_revisions_source {
140        local_var_req_builder = local_var_req_builder.query(&[("filter[waf_rule_revisions][source]", &local_var_str.to_string())]);
141    }
142    if let Some(ref local_var_str) = filter_waf_firewall_id_not_match {
143        local_var_req_builder = local_var_req_builder.query(&[("filter[waf_firewall.id][not][match]", &local_var_str.to_string())]);
144    }
145    if let Some(ref local_var_str) = page_number {
146        local_var_req_builder = local_var_req_builder.query(&[("page[number]", &local_var_str.to_string())]);
147    }
148    if let Some(ref local_var_str) = page_size {
149        local_var_req_builder = local_var_req_builder.query(&[("page[size]", &local_var_str.to_string())]);
150    }
151    if let Some(ref local_var_str) = include {
152        local_var_req_builder = local_var_req_builder.query(&[("include", &local_var_str.to_string())]);
153    }
154    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
155        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
156    }
157    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
158        let local_var_key = local_var_apikey.key.clone();
159        let local_var_value = match local_var_apikey.prefix {
160            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
161            None => local_var_key,
162        };
163        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
164    };
165
166    let local_var_req = local_var_req_builder.build()?;
167    let local_var_resp = local_var_client.execute(local_var_req).await?;
168
169    if "GET" != "GET" && "GET" != "HEAD" {
170      let headers = local_var_resp.headers();
171      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
172          Some(v) => v.to_str().unwrap().parse().unwrap(),
173          None => configuration::DEFAULT_RATELIMIT,
174      };
175      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
176          Some(v) => v.to_str().unwrap().parse().unwrap(),
177          None => 0,
178      };
179    }
180
181    let local_var_status = local_var_resp.status();
182    let local_var_content = local_var_resp.text().await?;
183
184    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
185        serde_json::from_str(&local_var_content).map_err(Error::from)
186    } else {
187        let local_var_entity: Option<ListWafRulesError> = serde_json::from_str(&local_var_content).ok();
188        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
189        Err(Error::ResponseError(local_var_error))
190    }
191}
192