use std::fmt::Display;
use std::sync::Arc;
use serde::de::DeserializeOwned;
use serde::Serialize;
use serde_json::{json, Value};
use crate::error::Error;
use crate::http::{enc, HttpClient};
use crate::pagination::Page;
use crate::params::Params;
pub struct Editorial {
http: Arc<HttpClient>,
}
impl Editorial {
pub(crate) fn new(http: Arc<HttpClient>) -> Self {
Self { http }
}
pub fn coin_videos<T: DeserializeOwned>(&self, slug: &str, params: Params) -> Result<Page<T>, Error> {
Page::fetch(self.http.clone(), &format!("/api/v1/coins/{}/videos", enc(slug)), params)
}
pub fn coin_insights<T: DeserializeOwned>(&self, slug: &str, params: Params) -> Result<T, Error> {
self.http.get(&format!("/api/v1/coins/{}/insights", enc(slug)), ¶ms)
}
pub fn articles<T: DeserializeOwned>(&self, params: Params) -> Result<Page<T>, Error> {
Page::fetch(self.http.clone(), "/api/v1/articles", params)
}
pub fn article<T: DeserializeOwned>(&self, slug: &str, params: Params) -> Result<T, Error> {
self.http.get(&format!("/api/v1/articles/{}", enc(slug)), ¶ms)
}
pub fn submit_article_feedback(&self, slug: &str, helpful: bool) -> Result<Value, Error> {
self.http.post_value(&format!("/api/v1/articles/{}/feedback", enc(slug)), Some(json!({ "helpful": helpful })))
}
pub fn video<T: DeserializeOwned>(&self, id: impl Display) -> Result<T, Error> {
self.http.get(&format!("/api/v1/videos/{}", enc(&id.to_string())), &Params::new())
}
pub fn insights<T: DeserializeOwned>(&self, params: Params) -> Result<Page<T>, Error> {
Page::fetch(self.http.clone(), "/api/v1/insights", params)
}
pub fn insight<T: DeserializeOwned>(&self, id: impl Display, params: Params) -> Result<T, Error> {
self.http.get(&format!("/api/v1/insights/{}", enc(&id.to_string())), ¶ms)
}
}
#[derive(Debug, Clone, Serialize)]
pub struct NewAlarm {
pub name: String,
pub coin: String,
pub metric: String,
pub direction: String,
pub target: f64,
pub notification: String,
}
pub struct Alarms {
http: Arc<HttpClient>,
}
impl Alarms {
pub(crate) fn new(http: Arc<HttpClient>) -> Self {
Self { http }
}
pub fn list<T: DeserializeOwned>(&self, params: Params) -> Result<Page<T>, Error> {
Page::fetch(self.http.clone(), "/api/v1/alarms", params)
}
pub fn create(&self, alarm: &NewAlarm) -> Result<Value, Error> {
let body = serde_json::to_value(alarm).map_err(|e| Error::Decode(e.to_string()))?;
self.http.post_value("/api/v1/alarms", Some(body))
}
pub fn delete(&self, id: impl Display) -> Result<Value, Error> {
self.http.delete_value(&format!("/api/v1/alarms/{}", enc(&id.to_string())))
}
}
#[derive(Debug, Clone, Serialize)]
pub struct NewWebhook {
pub url: String,
pub events: Vec<String>,
}
pub struct Webhooks {
http: Arc<HttpClient>,
}
impl Webhooks {
pub(crate) fn new(http: Arc<HttpClient>) -> Self {
Self { http }
}
pub fn list<T: DeserializeOwned>(&self) -> Result<T, Error> {
self.http.get("/api/v1/webhooks", &Params::new())
}
pub fn create(&self, webhook: &NewWebhook) -> Result<Value, Error> {
let body = serde_json::to_value(webhook).map_err(|e| Error::Decode(e.to_string()))?;
self.http.post_value("/api/v1/webhooks", Some(body))
}
pub fn delete(&self, id: impl Display) -> Result<Value, Error> {
self.http.delete_value(&format!("/api/v1/webhooks/{}", enc(&id.to_string())))
}
pub fn test(&self, id: impl Display) -> Result<Value, Error> {
self.http.post_value(&format!("/api/v1/webhooks/{}/test", enc(&id.to_string())), None)
}
pub fn deliveries<T: DeserializeOwned>(&self, id: impl Display, params: Params) -> Result<Page<T>, Error> {
Page::fetch(self.http.clone(), &format!("/api/v1/webhooks/{}/deliveries", enc(&id.to_string())), params)
}
}
pub struct Meta {
http: Arc<HttpClient>,
}
impl Meta {
pub(crate) fn new(http: Arc<HttpClient>) -> Self {
Self { http }
}
pub fn openapi<T: DeserializeOwned>(&self) -> Result<T, Error> {
self.http.get("/api/v1/openapi.json", &Params::new())
}
pub fn ping<T: DeserializeOwned>(&self) -> Result<T, Error> {
self.http.get("/api/v1/ping", &Params::new())
}
pub fn usage<T: DeserializeOwned>(&self) -> Result<T, Error> {
self.http.get("/api/v1/usage", &Params::new())
}
}