//!
//! AUTO-GENERATED CODE - DO NOT EDIT!
//!
//! Swagger Petstore
//! Source: crates/oas3-gen/fixtures/petstore.json
//! Version: 1.0.0
//! Generated by `oas3-gen v0.26.0`
//!

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

use super::types::*;
pub const BASE_URL: &str = "http://petstore.swagger.io/v1";
#[derive(Debug, Clone)]
pub struct SwaggerPetstoreClient {
  pub client: Client,
  pub base_url: Url,
}
impl Default for SwaggerPetstoreClient {
  fn default() -> Self {
    Self::new()
  }
}
impl SwaggerPetstoreClient {
  /// 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 })
  }
  /// List all pets
  ///
  /// * Path: `GET /{api_version}/pets`
  pub async fn list_pets(&self, request: ListPetsRequest) -> anyhow::Result<ListPetsResponse> {
    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(&request.path.api_version.clone())
      .push("pets");
    let response = self
      .client
      .get(url)
      .query(&request.query)
      .headers(http::HeaderMap::try_from(&request.header).context("building request headers")?)
      .send()
      .await?;
    ListPetsRequest::parse_response(response).await
  }
  /// Create a pet
  ///
  /// * Path: `POST /{api_version}/pets`
  pub async fn create_pets(&self, request: CreatePetsRequest) -> anyhow::Result<CreatePetsResponse> {
    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(&request.path.api_version.clone())
      .push("pets");
    let response = self.client.post(url).send().await?;
    CreatePetsRequest::parse_response(response).await
  }
  /// List all cats
  ///
  /// * Path: `GET /cats`
  pub async fn list_cats(&self, request: ListCatsRequest) -> anyhow::Result<ListCatsResponse> {
    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("cats");
    let response = self
      .client
      .get(url)
      .query(&request.query)
      .headers(http::HeaderMap::try_from(&request.header).context("building request headers")?)
      .send()
      .await?;
    ListCatsRequest::parse_response(response).await
  }
  /// Info for a specific pet
  ///
  /// * Path: `GET /pets/{petId}`
  pub async fn show_pet_by_id(&self, request: ShowPetByIdRequest) -> anyhow::Result<ShowPetByIdResponse> {
    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("pets")
      .push(&request.path.pet_id.clone());
    let response = self
      .client
      .get(url)
      .headers(http::HeaderMap::try_from(&request.header).context("building request headers")?)
      .send()
      .await?;
    ShowPetByIdRequest::parse_response(response).await
  }
  /// Upload pet image and update name
  ///
  /// * Path: `POST /pets/{petId}/upload`
  pub async fn upload_pet_image(&self, request: UploadPetImageRequest) -> anyhow::Result<ShowPetByIdResponse> {
    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("pets")
      .push(&request.path.pet_id.clone())
      .push("upload");
    let mut req_builder = self.client.post(url);
    let body = &request.body;
    let mut form = reqwest::multipart::Form::new();
    form = form.part("name", reqwest::multipart::Part::text(body.name.clone()));
    form = form.part(
      "image",
      reqwest::multipart::Part::bytes(std::borrow::Cow::from(body.image.clone())),
    );
    req_builder = req_builder.multipart(form);
    let response = req_builder.send().await?;
    UploadPetImageRequest::parse_response(response).await
  }
}
