//!
//! AUTO-GENERATED CODE - DO NOT EDIT!
//!
//! Union Serialization Test API
//! Source: crates/oas3-gen/fixtures/union_serde.json
//! Version: 1.0.0
//! Generated by `oas3-gen v0.26.0`
//!
//! Comprehensive test fixture for union serialization/deserialization

use anyhow::Context;
use reqwest::{Client, Url};
use validator::Validate;

use super::types::*;
pub const BASE_URL: &str = "https://example.com/";
#[derive(Debug, Clone)]
pub struct UnionSerializationTestApiClient {
  pub client: Client,
  pub base_url: Url,
}
impl Default for UnionSerializationTestApiClient {
  fn default() -> Self {
    Self::new()
  }
}
impl UnionSerializationTestApiClient {
  /// Create a client using the OpenAPI `servers[0]` URL.
  #[must_use]
  #[track_caller]
  pub fn new() -> Self {
    Self {
      client: Client::builder().build().expect("client"),
      base_url: Url::parse(BASE_URL).expect("valid base url"),
    }
  }
  /// Create a client with a custom base URL.
  pub fn with_base_url(base_url: impl AsRef<str>) -> anyhow::Result<Self> {
    Ok(Self {
      client: Client::builder().build().context("building reqwest client")?,
      base_url: Url::parse(base_url.as_ref()).context("parsing base url")?,
    })
  }
  /// Create a client from an existing `reqwest::Client`.
  pub fn with_client(base_url: impl AsRef<str>, client: Client) -> anyhow::Result<Self> {
    let url = Url::parse(base_url.as_ref()).context("parsing base url")?;
    Ok(Self { client, base_url: url })
  }
  /// Send content blocks
  ///
  /// * Path: `POST /content`
  pub async fn send_content(&self, request: SendContentRequest) -> anyhow::Result<SendContentResponse> {
    request.validate().context("parameter validation")?;
    let mut url = self.base_url.clone();
    url
      .path_segments_mut()
      .map_err(|()| anyhow::anyhow!("URL cannot be a base"))?
      .push("content");
    let response = self.client.post(url).json(&request.body).send().await?;
    SendContentRequest::parse_response(response).await
  }
  /// Get events with discriminated union
  ///
  /// * Path: `GET /events`
  pub async fn get_events(&self, request: GetEventsRequest) -> anyhow::Result<GetEventsResponse> {
    request.validate().context("parameter validation")?;
    let mut url = self.base_url.clone();
    url
      .path_segments_mut()
      .map_err(|()| anyhow::anyhow!("URL cannot be a base"))?
      .push("events");
    let response = self.client.get(url).send().await?;
    GetEventsRequest::parse_response(response).await
  }
}
