//!
//! AUTO-GENERATED CODE - DO NOT EDIT!
//!
//! Event Stream API
//! Source: crates/oas3-gen/fixtures/event_stream.json
//! Version: 1.0.0
//! Generated by `oas3-gen v0.26.0`
//!
//! API with Server-Sent Events (SSE) endpoint

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

use super::types::*;
pub const BASE_URL: &str = "https://api.example.com";
#[derive(Debug, Clone)]
pub struct EventStreamApiClient {
  pub client: Client,
  pub base_url: Url,
}
impl Default for EventStreamApiClient {
  fn default() -> Self {
    Self::new()
  }
}
impl EventStreamApiClient {
  /// 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 })
  }
  /// Stream events
  ///
  /// Returns a stream of server-sent events
  ///
  /// * Path: `GET /events`
  pub async fn events(&self, request: EventsRequest) -> anyhow::Result<EventsResponse> {
    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?;
    EventsRequest::parse_response(response).await
  }
  /// Stream typed events
  ///
  /// Returns a stream of typed server-sent events with query parameters
  ///
  /// * Path: `GET /events/typed`
  pub async fn typed_events(&self, request: TypedEventsRequest) -> anyhow::Result<TypedEventsResponse> {
    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")
      .push("typed");
    let response = self.client.get(url).query(&request.query).send().await?;
    TypedEventsRequest::parse_response(response).await
  }
}
