1use std::fmt::Display;
2use std::sync::Arc;
3
4use serde::de::DeserializeOwned;
5use serde::Serialize;
6use serde_json::{json, Value};
7
8use crate::error::Error;
9use crate::http::{enc, HttpClient};
10use crate::pagination::Page;
11use crate::params::Params;
12
13pub struct Editorial {
15 http: Arc<HttpClient>,
16}
17
18impl Editorial {
19 pub(crate) fn new(http: Arc<HttpClient>) -> Self {
20 Self { http }
21 }
22
23 pub fn coin_videos<T: DeserializeOwned>(&self, slug: &str, params: Params) -> Result<Page<T>, Error> {
25 Page::fetch(self.http.clone(), &format!("/api/v1/coins/{}/videos", enc(slug)), params)
26 }
27
28 pub fn coin_insights<T: DeserializeOwned>(&self, slug: &str, params: Params) -> Result<T, Error> {
30 self.http.get(&format!("/api/v1/coins/{}/insights", enc(slug)), ¶ms)
31 }
32
33 pub fn articles<T: DeserializeOwned>(&self, params: Params) -> Result<Page<T>, Error> {
36 Page::fetch(self.http.clone(), "/api/v1/articles", params)
37 }
38
39 pub fn article<T: DeserializeOwned>(&self, slug: &str, params: Params) -> Result<T, Error> {
41 self.http.get(&format!("/api/v1/articles/{}", enc(slug)), ¶ms)
42 }
43
44 pub fn submit_article_feedback(&self, slug: &str, helpful: bool) -> Result<Value, Error> {
46 self.http.post_value(&format!("/api/v1/articles/{}/feedback", enc(slug)), Some(json!({ "helpful": helpful })))
47 }
48
49 pub fn video<T: DeserializeOwned>(&self, id: impl Display) -> Result<T, Error> {
51 self.http.get(&format!("/api/v1/videos/{}", enc(&id.to_string())), &Params::new())
52 }
53
54 pub fn insights<T: DeserializeOwned>(&self, params: Params) -> Result<Page<T>, Error> {
56 Page::fetch(self.http.clone(), "/api/v1/insights", params)
57 }
58
59 pub fn insight<T: DeserializeOwned>(&self, id: impl Display, params: Params) -> Result<T, Error> {
61 self.http.get(&format!("/api/v1/insights/{}", enc(&id.to_string())), ¶ms)
62 }
63}
64
65#[derive(Debug, Clone, Serialize)]
67pub struct NewAlarm {
68 pub name: String,
69 pub coin: String,
70 pub metric: String,
71 pub direction: String,
72 pub target: f64,
73 pub notification: String,
74}
75
76pub struct Alarms {
78 http: Arc<HttpClient>,
79}
80
81impl Alarms {
82 pub(crate) fn new(http: Arc<HttpClient>) -> Self {
83 Self { http }
84 }
85
86 pub fn list<T: DeserializeOwned>(&self, params: Params) -> Result<Page<T>, Error> {
88 Page::fetch(self.http.clone(), "/api/v1/alarms", params)
89 }
90
91 pub fn create(&self, alarm: &NewAlarm) -> Result<Value, Error> {
93 let body = serde_json::to_value(alarm).map_err(|e| Error::Decode(e.to_string()))?;
94 self.http.post_value("/api/v1/alarms", Some(body))
95 }
96
97 pub fn delete(&self, id: impl Display) -> Result<Value, Error> {
99 self.http.delete_value(&format!("/api/v1/alarms/{}", enc(&id.to_string())))
100 }
101}
102
103#[derive(Debug, Clone, Serialize)]
105pub struct NewWebhook {
106 pub url: String,
107 pub events: Vec<String>,
108}
109
110pub struct Webhooks {
112 http: Arc<HttpClient>,
113}
114
115impl Webhooks {
116 pub(crate) fn new(http: Arc<HttpClient>) -> Self {
117 Self { http }
118 }
119
120 pub fn list<T: DeserializeOwned>(&self) -> Result<T, Error> {
122 self.http.get("/api/v1/webhooks", &Params::new())
123 }
124
125 pub fn create(&self, webhook: &NewWebhook) -> Result<Value, Error> {
127 let body = serde_json::to_value(webhook).map_err(|e| Error::Decode(e.to_string()))?;
128 self.http.post_value("/api/v1/webhooks", Some(body))
129 }
130
131 pub fn delete(&self, id: impl Display) -> Result<Value, Error> {
133 self.http.delete_value(&format!("/api/v1/webhooks/{}", enc(&id.to_string())))
134 }
135
136 pub fn test(&self, id: impl Display) -> Result<Value, Error> {
138 self.http.post_value(&format!("/api/v1/webhooks/{}/test", enc(&id.to_string())), None)
139 }
140
141 pub fn deliveries<T: DeserializeOwned>(&self, id: impl Display, params: Params) -> Result<Page<T>, Error> {
143 Page::fetch(self.http.clone(), &format!("/api/v1/webhooks/{}/deliveries", enc(&id.to_string())), params)
144 }
145}
146
147pub struct Meta {
149 http: Arc<HttpClient>,
150}
151
152impl Meta {
153 pub(crate) fn new(http: Arc<HttpClient>) -> Self {
154 Self { http }
155 }
156
157 pub fn openapi<T: DeserializeOwned>(&self) -> Result<T, Error> {
159 self.http.get("/api/v1/openapi.json", &Params::new())
160 }
161
162 pub fn ping<T: DeserializeOwned>(&self) -> Result<T, Error> {
164 self.http.get("/api/v1/ping", &Params::new())
165 }
166
167 pub fn usage<T: DeserializeOwned>(&self) -> Result<T, Error> {
169 self.http.get("/api/v1/usage", &Params::new())
170 }
171}