late 0.0.655

API reference for Zernio. Authenticate with a Bearer API key. Base URL: https://zernio.com/api
Documentation
/*
 * Zernio API
 *
 * API reference for Zernio. Authenticate with a Bearer API key. Base URL: https://zernio.com/api
 *
 * The version of the OpenAPI document: 1.0.4
 * Contact: support@zernio.com
 * Generated by: https://openapi-generator.tech
 */

use super::{configuration, ContentType, Error};
use crate::{apis::ResponseContent, models};
use reqwest;
use serde::{de::Error as _, Deserialize, Serialize};

/// struct for typed errors of method [`cancel_rf_reservation`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum CancelRfReservationError {
    Status400(),
    Status401(models::InlineObject),
    Status501(),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`create_rf_prediction`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum CreateRfPredictionError {
    Status400(),
    Status401(models::InlineObject),
    Status422(),
    Status501(),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`get_rf_prediction`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetRfPredictionError {
    Status400(),
    Status401(models::InlineObject),
    Status501(),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`reserve_rf_prediction`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ReserveRfPredictionError {
    Status400(),
    Status401(models::InlineObject),
    Status501(),
    UnknownValue(serde_json::Value),
}

/// Releases a RESERVATION's locked price and inventory. Unreserved predictions expire on their own.
pub async fn cancel_rf_reservation(
    configuration: &configuration::Configuration,
    prediction_id: &str,
    account_id: &str,
    ad_account_id: &str,
) -> Result<(), Error<CancelRfReservationError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_path_prediction_id = prediction_id;
    let p_query_account_id = account_id;
    let p_query_ad_account_id = ad_account_id;

    let uri_str = format!(
        "{}/v1/ads/rf-predictions/{predictionId}",
        configuration.base_path,
        predictionId = crate::apis::urlencode(p_path_prediction_id)
    );
    let mut req_builder = configuration
        .client
        .request(reqwest::Method::DELETE, &uri_str);

    req_builder = req_builder.query(&[("accountId", &p_query_account_id.to_string())]);
    req_builder = req_builder.query(&[("adAccountId", &p_query_ad_account_id.to_string())]);
    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }
    if let Some(ref token) = configuration.bearer_access_token {
        req_builder = req_builder.bearer_auth(token.to_owned());
    };

    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();

    if !status.is_client_error() && !status.is_server_error() {
        Ok(())
    } else {
        let content = resp.text().await?;
        let entity: Option<CancelRfReservationError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent {
            status,
            content,
            entity,
        }))
    }
}

/// Creates an R&F prediction — a QUOTE, nothing is bought and no ad entities are created. Provide a date range plus exactly one of `budgetAmount` (Meta predicts reach) or `reach` (Meta predicts the budget). The response carries the estimate and its allowed bounds (min/max budget and reach). Predictions expire on their own; to buy, reserve one via POST /v1/ads/rf-predictions/{predictionId}/reserve and pass the RESERVED id to POST /v1/ads/create with `buyingType: \"RESERVED\"`.  Reservation campaigns reject automatic placements. Top-level `placements` wins; when it is omitted, `targeting.placements` is used; when neither is set, placements default to Facebook feed (+ Instagram stream when a linked IG professional account resolves). Instagram placements require that IG account.
pub async fn create_rf_prediction(
    configuration: &configuration::Configuration,
    create_rf_prediction_request: models::CreateRfPredictionRequest,
) -> Result<models::CreateRfPrediction201Response, Error<CreateRfPredictionError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_body_create_rf_prediction_request = create_rf_prediction_request;

    let uri_str = format!("{}/v1/ads/rf-predictions", configuration.base_path);
    let mut req_builder = configuration
        .client
        .request(reqwest::Method::POST, &uri_str);

    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }
    if let Some(ref token) = configuration.bearer_access_token {
        req_builder = req_builder.bearer_auth(token.to_owned());
    };
    req_builder = req_builder.json(&p_body_create_rf_prediction_request);

    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();
    let content_type = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("application/octet-stream");
    let content_type = super::ContentType::from(content_type);

    if !status.is_client_error() && !status.is_server_error() {
        let content = resp.text().await?;
        match content_type {
            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::CreateRfPrediction201Response`"))),
            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::CreateRfPrediction201Response`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<CreateRfPredictionError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent {
            status,
            content,
            entity,
        }))
    }
}

pub async fn get_rf_prediction(
    configuration: &configuration::Configuration,
    prediction_id: &str,
    account_id: &str,
    ad_account_id: &str,
) -> Result<models::CreateRfPrediction201Response, Error<GetRfPredictionError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_path_prediction_id = prediction_id;
    let p_query_account_id = account_id;
    let p_query_ad_account_id = ad_account_id;

    let uri_str = format!(
        "{}/v1/ads/rf-predictions/{predictionId}",
        configuration.base_path,
        predictionId = crate::apis::urlencode(p_path_prediction_id)
    );
    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);

    req_builder = req_builder.query(&[("accountId", &p_query_account_id.to_string())]);
    req_builder = req_builder.query(&[("adAccountId", &p_query_ad_account_id.to_string())]);
    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }
    if let Some(ref token) = configuration.bearer_access_token {
        req_builder = req_builder.bearer_auth(token.to_owned());
    };

    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();
    let content_type = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("application/octet-stream");
    let content_type = super::ContentType::from(content_type);

    if !status.is_client_error() && !status.is_server_error() {
        let content = resp.text().await?;
        match content_type {
            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::CreateRfPrediction201Response`"))),
            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::CreateRfPrediction201Response`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<GetRfPredictionError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent {
            status,
            content,
            entity,
        }))
    }
}

/// Locks the quoted price + inventory until the returned `expiresAt` and mints a NEW prediction id — pass that RESERVED id (not the original) as `rfPredictionId` on POST /v1/ads/create. Release an unused reservation via DELETE.
pub async fn reserve_rf_prediction(
    configuration: &configuration::Configuration,
    prediction_id: &str,
    reserve_rf_prediction_request: models::ReserveRfPredictionRequest,
) -> Result<models::ReserveRfPrediction201Response, Error<ReserveRfPredictionError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_path_prediction_id = prediction_id;
    let p_body_reserve_rf_prediction_request = reserve_rf_prediction_request;

    let uri_str = format!(
        "{}/v1/ads/rf-predictions/{predictionId}/reserve",
        configuration.base_path,
        predictionId = crate::apis::urlencode(p_path_prediction_id)
    );
    let mut req_builder = configuration
        .client
        .request(reqwest::Method::POST, &uri_str);

    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }
    if let Some(ref token) = configuration.bearer_access_token {
        req_builder = req_builder.bearer_auth(token.to_owned());
    };
    req_builder = req_builder.json(&p_body_reserve_rf_prediction_request);

    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();
    let content_type = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("application/octet-stream");
    let content_type = super::ContentType::from(content_type);

    if !status.is_client_error() && !status.is_server_error() {
        let content = resp.text().await?;
        match content_type {
            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ReserveRfPrediction201Response`"))),
            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ReserveRfPrediction201Response`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<ReserveRfPredictionError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent {
            status,
            content,
            entity,
        }))
    }
}