/*
 * Clerk Backend API
 *
 * The Clerk REST Backend API, meant to be accessed by backend servers. Please see https://clerk.com/docs for more information.
 *
 * The version of the OpenAPI document: v1
 * Contact: support@clerk.com
 * Generated by: https://openapi-generator.tech
 */
use super::Error;
use crate::{apis::ResponseContent, clerk::Clerk};
use reqwest;
use serde::{self, Deserialize, Serialize};

/// struct for typed errors of method [`get_jwks`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetJwksError {
	UnknownValue(serde_json::Value),
}

#[derive(Debug, Serialize, Deserialize)]
pub struct JwksKey {
	#[serde(rename = "use")]
	pub use_key: String,
	pub kty: String,
	pub kid: String,
	pub alg: String,
	pub n: String,
	pub e: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct JwksModel {
	pub keys: Vec<JwksKey>,
}

pub struct Jwks;

impl Jwks {
	/// Retrieve the JSON Web Key Set of the instance
	pub async fn get_jwks(clerk_client: &Clerk) -> Result<JwksModel, Error<GetJwksError>> {
		let local_var_configuration = &clerk_client.config;

		let local_var_client = &local_var_configuration.client;

		let local_var_uri_str = format!("{}/jwks", local_var_configuration.base_path);
		let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());

		if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
			local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
		}

		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();

		if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
			let res = local_var_resp.json::<JwksModel>().await?;
			Ok(res)
		} else {
			let local_var_content = local_var_resp.text().await?;
			let local_var_entity: Option<GetJwksError> = 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))
		}
	}
}
