/*
* Asana
*
* This is the interface for interacting with the [Asana Platform](https://developers.asana.com). Our API reference is generated from our [OpenAPI spec] (https://raw.githubusercontent.com/Asana/developer-docs/master/defs/asana_oas.yaml).
*
* The version of the OpenAPI document: 1.0
*
* Generated by: https://openapi-generator.tech
*/
use reqwest;
use super::{configuration, Error};
use crate::apis::ResponseContent;
/// struct for typed errors of method `create_webhook`
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum CreateWebhookError {
Status400(crate::models::ErrorResponse),
Status401(crate::models::ErrorResponse),
Status403(crate::models::ErrorResponse),
Status404(crate::models::ErrorResponse),
Status500(crate::models::ErrorResponse),
UnknownValue(serde_json::Value),
}
/// struct for typed errors of method `delete_webhook`
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum DeleteWebhookError {
Status400(crate::models::ErrorResponse),
Status401(crate::models::ErrorResponse),
Status403(crate::models::ErrorResponse),
Status404(crate::models::ErrorResponse),
Status500(crate::models::ErrorResponse),
UnknownValue(serde_json::Value),
}
/// struct for typed errors of method `get_webhook`
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetWebhookError {
Status400(crate::models::ErrorResponse),
Status401(crate::models::ErrorResponse),
Status403(crate::models::ErrorResponse),
Status404(crate::models::ErrorResponse),
Status500(crate::models::ErrorResponse),
UnknownValue(serde_json::Value),
}
/// struct for typed errors of method `get_webhooks`
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetWebhooksError {
Status400(crate::models::ErrorResponse),
Status401(crate::models::ErrorResponse),
Status403(crate::models::ErrorResponse),
Status404(crate::models::ErrorResponse),
Status500(crate::models::ErrorResponse),
UnknownValue(serde_json::Value),
}
/// Establishing a webhook is a two-part process. First, a simple HTTP POST request initiates the creation similar to creating any other resource. Next, in the middle of this request comes the confirmation handshake. When a webhook is created, we will send a test POST to the target with an `X-Hook-Secret` header. The target must respond with a `200 OK` or `204 No Content` and a matching `X-Hook-Secret` header to confirm that this webhook subscription is indeed expected. We strongly recommend storing this secret to be used to verify future webhook event signatures. The POST request to create the webhook will then return with the status of the request. If you do not acknowledge the webhook’s confirmation handshake it will fail to setup, and you will receive an error in response to your attempt to create it. This means you need to be able to receive and complete the webhook *while* the POST request is in-flight (in other words, have a server that can handle requests asynchronously). ``` # Request curl -H \"Authorization: Bearer <personal_access_token>\" \\ -X POST https://app.asana.com/api/1.0/webhooks \\ -d \"resource=8675309\" \\ -d \"target=https://example.com/receive-webhook/7654\" ``` ``` # Handshake sent to https://example.com/ POST /receive-webhook/7654 X-Hook-Secret: b537207f20cbfa02357cf448134da559e8bd39d61597dcd5631b8012eae53e81 ``` ``` # Handshake response sent by example.com HTTP/1.1 200 X-Hook-Secret: b537207f20cbfa02357cf448134da559e8bd39d61597dcd5631b8012eae53e81 ``` ``` # Response HTTP/1.1 201 { \"data\": { \"gid\": \"43214\", \"resource\": { \"gid\": \"8675309\", \"name\": \"Bugs\" }, \"target\": \"https://example.com/receive-webhook/7654\", \"active\": false, \"last_success_at\": null, \"last_failure_at\": null, \"last_failure_content\": null } } ```
pub async fn create_webhook(
configuration: &configuration::Configuration,
inline_object53: crate::models::InlineObject53,
opt_pretty: Option<bool>,
opt_fields: Option<Vec<String>>,
) -> Result<crate::models::InlineResponse2018, Error<CreateWebhookError>> {
let local_var_client = &configuration.client;
let local_var_uri_str = format!("{}/webhooks", configuration.base_path);
let mut local_var_req_builder =
local_var_client.post(local_var_uri_str.as_str());
if let Some(ref local_var_str) = opt_pretty {
local_var_req_builder = local_var_req_builder
.query(&[("opt_pretty", &local_var_str.to_string())]);
}
if let Some(ref local_var_str) = opt_fields {
local_var_req_builder = local_var_req_builder.query(&[(
"opt_fields",
&local_var_str
.into_iter()
.map(|p| p.to_string())
.collect::<Vec<String>>()
.join(",")
.to_string(),
)]);
}
if let Some(ref local_var_user_agent) = configuration.user_agent {
local_var_req_builder = local_var_req_builder
.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
}
if let Some(ref local_var_token) = configuration.oauth_access_token {
local_var_req_builder =
local_var_req_builder.bearer_auth(local_var_token.to_owned());
};
if let Some(ref local_var_token) = configuration.bearer_access_token {
local_var_req_builder =
local_var_req_builder.bearer_auth(local_var_token.to_owned());
};
local_var_req_builder = local_var_req_builder.json(&inline_object53);
let local_var_req = local_var_req_builder.build()?;
let local_var_resp = local_var_client.execute(local_var_req).await?;
let local_var_status = local_var_resp.status();
let local_var_content = local_var_resp.text().await?;
if !local_var_status.is_client_error()
&& !local_var_status.is_server_error()
{
serde_json::from_str(&local_var_content).map_err(Error::from)
} else {
let local_var_entity: Option<CreateWebhookError> =
serde_json::from_str(&local_var_content).ok();
let local_var_error = ResponseContent {
status: local_var_status,
content: local_var_content,
entity: local_var_entity,
};
Err(Error::ResponseError(local_var_error))
}
}
/// This method *permanently* removes a webhook. Note that it may be possible to receive a request that was already in flight after deleting the webhook, but no further requests will be issued.
pub async fn delete_webhook(
configuration: &configuration::Configuration,
webhook_gid: &str,
opt_pretty: Option<bool>,
opt_fields: Option<Vec<String>>,
) -> Result<crate::models::InlineResponse2001, Error<DeleteWebhookError>> {
let local_var_client = &configuration.client;
let local_var_uri_str = format!(
"{}/webhooks/{webhook_gid}",
configuration.base_path,
webhook_gid = crate::apis::urlencode(webhook_gid)
);
let mut local_var_req_builder =
local_var_client.delete(local_var_uri_str.as_str());
if let Some(ref local_var_str) = opt_pretty {
local_var_req_builder = local_var_req_builder
.query(&[("opt_pretty", &local_var_str.to_string())]);
}
if let Some(ref local_var_str) = opt_fields {
local_var_req_builder = local_var_req_builder.query(&[(
"opt_fields",
&local_var_str
.into_iter()
.map(|p| p.to_string())
.collect::<Vec<String>>()
.join(",")
.to_string(),
)]);
}
if let Some(ref local_var_user_agent) = configuration.user_agent {
local_var_req_builder = local_var_req_builder
.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
}
if let Some(ref local_var_token) = configuration.oauth_access_token {
local_var_req_builder =
local_var_req_builder.bearer_auth(local_var_token.to_owned());
};
if let Some(ref local_var_token) = configuration.bearer_access_token {
local_var_req_builder =
local_var_req_builder.bearer_auth(local_var_token.to_owned());
};
let local_var_req = local_var_req_builder.build()?;
let local_var_resp = local_var_client.execute(local_var_req).await?;
let local_var_status = local_var_resp.status();
let local_var_content = local_var_resp.text().await?;
if !local_var_status.is_client_error()
&& !local_var_status.is_server_error()
{
serde_json::from_str(&local_var_content).map_err(Error::from)
} else {
let local_var_entity: Option<DeleteWebhookError> =
serde_json::from_str(&local_var_content).ok();
let local_var_error = ResponseContent {
status: local_var_status,
content: local_var_content,
entity: local_var_entity,
};
Err(Error::ResponseError(local_var_error))
}
}
/// Returns the full record for the given webhook.
pub async fn get_webhook(
configuration: &configuration::Configuration,
webhook_gid: &str,
opt_pretty: Option<bool>,
opt_fields: Option<Vec<String>>,
) -> Result<crate::models::InlineResponse2018, Error<GetWebhookError>> {
let local_var_client = &configuration.client;
let local_var_uri_str = format!(
"{}/webhooks/{webhook_gid}",
configuration.base_path,
webhook_gid = crate::apis::urlencode(webhook_gid)
);
let mut local_var_req_builder =
local_var_client.get(local_var_uri_str.as_str());
if let Some(ref local_var_str) = opt_pretty {
local_var_req_builder = local_var_req_builder
.query(&[("opt_pretty", &local_var_str.to_string())]);
}
if let Some(ref local_var_str) = opt_fields {
local_var_req_builder = local_var_req_builder.query(&[(
"opt_fields",
&local_var_str
.into_iter()
.map(|p| p.to_string())
.collect::<Vec<String>>()
.join(",")
.to_string(),
)]);
}
if let Some(ref local_var_user_agent) = configuration.user_agent {
local_var_req_builder = local_var_req_builder
.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
}
if let Some(ref local_var_token) = configuration.oauth_access_token {
local_var_req_builder =
local_var_req_builder.bearer_auth(local_var_token.to_owned());
};
if let Some(ref local_var_token) = configuration.bearer_access_token {
local_var_req_builder =
local_var_req_builder.bearer_auth(local_var_token.to_owned());
};
let local_var_req = local_var_req_builder.build()?;
let local_var_resp = local_var_client.execute(local_var_req).await?;
let local_var_status = local_var_resp.status();
let local_var_content = local_var_resp.text().await?;
if !local_var_status.is_client_error()
&& !local_var_status.is_server_error()
{
serde_json::from_str(&local_var_content).map_err(Error::from)
} else {
let local_var_entity: Option<GetWebhookError> =
serde_json::from_str(&local_var_content).ok();
let local_var_error = ResponseContent {
status: local_var_status,
content: local_var_content,
entity: local_var_entity,
};
Err(Error::ResponseError(local_var_error))
}
}
/// Get the compact representation of all webhooks your app has registered for the authenticated user in the given workspace.
pub async fn get_webhooks(
configuration: &configuration::Configuration,
workspace: &str,
opt_pretty: Option<bool>,
opt_fields: Option<Vec<String>>,
limit: Option<i32>,
offset: Option<&str>,
resource: Option<&str>,
) -> Result<crate::models::InlineResponse20033, Error<GetWebhooksError>> {
let local_var_client = &configuration.client;
let local_var_uri_str = format!("{}/webhooks", configuration.base_path);
let mut local_var_req_builder =
local_var_client.get(local_var_uri_str.as_str());
if let Some(ref local_var_str) = opt_pretty {
local_var_req_builder = local_var_req_builder
.query(&[("opt_pretty", &local_var_str.to_string())]);
}
if let Some(ref local_var_str) = opt_fields {
local_var_req_builder = local_var_req_builder.query(&[(
"opt_fields",
&local_var_str
.into_iter()
.map(|p| p.to_string())
.collect::<Vec<String>>()
.join(",")
.to_string(),
)]);
}
if let Some(ref local_var_str) = limit {
local_var_req_builder = local_var_req_builder
.query(&[("limit", &local_var_str.to_string())]);
}
if let Some(ref local_var_str) = offset {
local_var_req_builder = local_var_req_builder
.query(&[("offset", &local_var_str.to_string())]);
}
local_var_req_builder =
local_var_req_builder.query(&[("workspace", &workspace.to_string())]);
if let Some(ref local_var_str) = resource {
local_var_req_builder = local_var_req_builder
.query(&[("resource", &local_var_str.to_string())]);
}
if let Some(ref local_var_user_agent) = configuration.user_agent {
local_var_req_builder = local_var_req_builder
.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
}
if let Some(ref local_var_token) = configuration.oauth_access_token {
local_var_req_builder =
local_var_req_builder.bearer_auth(local_var_token.to_owned());
};
if let Some(ref local_var_token) = configuration.bearer_access_token {
local_var_req_builder =
local_var_req_builder.bearer_auth(local_var_token.to_owned());
};
let local_var_req = local_var_req_builder.build()?;
let local_var_resp = local_var_client.execute(local_var_req).await?;
let local_var_status = local_var_resp.status();
let local_var_content = local_var_resp.text().await?;
if !local_var_status.is_client_error()
&& !local_var_status.is_server_error()
{
serde_json::from_str(&local_var_content).map_err(Error::from)
} else {
let local_var_entity: Option<GetWebhooksError> =
serde_json::from_str(&local_var_content).ok();
let local_var_error = ResponseContent {
status: local_var_status,
content: local_var_content,
entity: local_var_entity,
};
Err(Error::ResponseError(local_var_error))
}
}