#![allow(
clippy::all
)]
use serde::Deserialize;
use roctokit::adapters::{AdapterError, Client, GitHubRequest, GitHubResponseExt};
use crate::models::*;
use super::PerPage;
use std::collections::HashMap;
use serde_json::value::Value;
pub struct Emojis<'api, C: Client> where AdapterError: From<<C as Client>::Err> {
client: &'api C
}
pub fn new<C: Client>(client: &C) -> Emojis<C> where AdapterError: From<<C as Client>::Err> {
Emojis { client }
}
#[derive(Debug, thiserror::Error)]
pub enum EmojisGetError {
#[error("Not modified")]
Status304,
#[error("Status code: {}", code)]
Generic { code: u16 },
}
impl From<EmojisGetError> for AdapterError {
fn from(err: EmojisGetError) -> Self {
let (description, status_code) = match err {
EmojisGetError::Status304 => (String::from("Not modified"), 304),
EmojisGetError::Generic { code } => (String::from("Generic"), code)
};
Self::Endpoint {
description,
status_code,
source: Some(Box::new(err))
}
}
}
impl<'api, C: Client> Emojis<'api, C> where AdapterError: From<<C as Client>::Err> {
pub async fn get_async(&self) -> Result<HashMap<String, String>, AdapterError> {
let request_uri = format!("{}/emojis", super::GITHUB_BASE_API_URL);
let req = GitHubRequest {
uri: request_uri,
body: None::<C::Body>,
method: "GET",
headers: vec![]
};
let request = self.client.build(req)?;
let github_response = self.client.fetch_async(request).await?;
if github_response.is_success() {
Ok(github_response.to_json_async().await?)
} else {
match github_response.status_code() {
304 => Err(EmojisGetError::Status304.into()),
code => Err(EmojisGetError::Generic { code }.into()),
}
}
}
#[cfg(not(target_arch = "wasm32"))]
pub fn get(&self) -> Result<HashMap<String, String>, AdapterError> {
let request_uri = format!("{}/emojis", super::GITHUB_BASE_API_URL);
let req = GitHubRequest {
uri: request_uri,
body: None,
method: "GET",
headers: vec![]
};
let request = self.client.build(req)?;
let github_response = self.client.fetch(request)?;
if github_response.is_success() {
Ok(github_response.to_json()?)
} else {
match github_response.status_code() {
304 => Err(EmojisGetError::Status304.into()),
code => Err(EmojisGetError::Generic { code }.into()),
}
}
}
}