fastly_api/apis/
publish_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 [`publish`]
15#[derive(Clone, Debug, Default)]
16pub struct PublishParams {
17    /// Alphanumeric string identifying the service.
18    pub service_id: String,
19    pub publish_request: Option<crate::models::PublishRequest>
20}
21
22
23/// struct for typed errors of method [`publish`]
24#[derive(Debug, Clone, Serialize, Deserialize)]
25#[serde(untagged)]
26pub enum PublishError {
27    UnknownValue(serde_json::Value),
28}
29
30
31/// Send one or more messages to [Fanout](https://www.fastly.com/documentation/learning/concepts/real-time-messaging/fanout) subscribers. Each message specifies a channel, and Fanout will deliver the message to all subscribers of its channel. > **IMPORTANT:** For compatibility with GRIP, this endpoint requires a trailing slash, and the API token may be provided in the `Authorization` header (instead of the `Fastly-Key` header) using the `Bearer` scheme. 
32pub async fn publish(configuration: &mut configuration::Configuration, params: PublishParams) -> Result<String, Error<PublishError>> {
33    let local_var_configuration = configuration;
34
35    // unbox the parameters
36    let service_id = params.service_id;
37    let publish_request = params.publish_request;
38
39
40    let local_var_client = &local_var_configuration.client;
41
42    let local_var_uri_str = format!("{}/service/{service_id}/publish/", local_var_configuration.base_path, service_id=crate::apis::urlencode(service_id));
43    let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
44
45    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
46        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
47    }
48    if let Some(ref local_var_apikey) = local_var_configuration.api_key {
49        let local_var_key = local_var_apikey.key.clone();
50        let local_var_value = match local_var_apikey.prefix {
51            Some(ref local_var_prefix) => format!("{} {}", local_var_prefix, local_var_key),
52            None => local_var_key,
53        };
54        local_var_req_builder = local_var_req_builder.header("Fastly-Key", local_var_value);
55    };
56    local_var_req_builder = local_var_req_builder.json(&publish_request);
57
58    let local_var_req = local_var_req_builder.build()?;
59    let local_var_resp = local_var_client.execute(local_var_req).await?;
60
61    if "POST" != "GET" && "POST" != "HEAD" {
62      let headers = local_var_resp.headers();
63      local_var_configuration.rate_limit_remaining = match headers.get("Fastly-RateLimit-Remaining") {
64          Some(v) => v.to_str().unwrap().parse().unwrap(),
65          None => configuration::DEFAULT_RATELIMIT,
66      };
67      local_var_configuration.rate_limit_reset = match headers.get("Fastly-RateLimit-Reset") {
68          Some(v) => v.to_str().unwrap().parse().unwrap(),
69          None => 0,
70      };
71    }
72
73    let local_var_status = local_var_resp.status();
74    let local_var_content = local_var_resp.text().await?;
75
76    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
77        serde_json::from_str(&local_var_content).map_err(Error::from)
78    } else {
79        let local_var_entity: Option<PublishError> = serde_json::from_str(&local_var_content).ok();
80        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
81        Err(Error::ResponseError(local_var_error))
82    }
83}
84