-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmod.rs
220 lines (193 loc) · 7.41 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use std::{fmt::Debug, time::Duration};
use anyhow::anyhow;
use graphql_client::{GraphQLQuery, Response};
use reqwest::header::{self, HeaderValue};
use serde::{de::DeserializeOwned, Serialize};
use synd_o11y::{health_check::Health, opentelemetry::extension::*};
use thiserror::Error;
use tracing::{error, Span};
use url::Url;
use crate::{auth::Credential, client::payload::ExportSubscriptionPayload, config, types};
use self::query::subscription::SubscriptionOutput;
mod scalar;
pub use scalar::*;
pub mod mutation;
pub mod payload;
pub mod query;
#[derive(Error, Debug)]
pub enum SubscribeFeedError {
#[error("invalid feed url: `{feed_url}` ({message})`")]
InvalidFeedUrl { feed_url: String, message: String },
#[error("internal error: {0}")]
Internal(anyhow::Error),
}
/// synd-api client
#[derive(Clone)]
pub struct Client {
client: reqwest::Client,
credential: Option<HeaderValue>,
endpoint: Url,
}
impl Client {
const GRAPHQL: &'static str = "/graphql";
const HEALTH_CHECK: &'static str = "/health";
pub fn new(endpoint: Url, timeout: Duration) -> anyhow::Result<Self> {
let client = reqwest::ClientBuilder::new()
.user_agent(config::client::USER_AGENT)
.timeout(timeout)
.connect_timeout(Duration::from_secs(10))
// this client specifically targets the syndicationd api, so accepts self signed certificates
.danger_accept_invalid_certs(true)
.build()?;
Ok(Self {
client,
endpoint,
credential: None,
})
}
pub fn set_credential(&mut self, auth: Credential) {
let mut token = HeaderValue::try_from(match auth {
Credential::Github { access_token } => format!("github {access_token}"),
Credential::Google { id_token, .. } => format!("google {id_token}"),
})
.unwrap();
token.set_sensitive(true);
self.credential = Some(token);
}
#[tracing::instrument(skip(self))]
pub async fn fetch_subscription(
&self,
after: Option<String>,
first: Option<i64>,
) -> anyhow::Result<SubscriptionOutput> {
let var = query::subscription::Variables { after, first };
let request = query::Subscription::build_query(var);
let response: query::subscription::ResponseData = self.request(&request).await?;
Ok(response.output)
}
#[tracing::instrument(skip(self))]
pub async fn subscribe_feed(&self, url: String) -> Result<types::Feed, SubscribeFeedError> {
use crate::client::mutation::subscribe_feed::ResponseCode;
let var = mutation::subscribe_feed::Variables {
input: mutation::subscribe_feed::SubscribeFeedInput { url: url.clone() },
};
let request = mutation::SubscribeFeed::build_query(var);
let response: mutation::subscribe_feed::ResponseData = self
.request(&request)
.await
.map_err(SubscribeFeedError::Internal)?;
match response.subscribe_feed {
mutation::subscribe_feed::SubscribeFeedSubscribeFeed::SubscribeFeedSuccess(success) => {
Ok(types::Feed::from(success.feed))
}
mutation::subscribe_feed::SubscribeFeedSubscribeFeed::SubscribeFeedError(err) => {
match err.status.code {
ResponseCode::OK => unreachable!(),
ResponseCode::INVALID_FEED_URL => Err(SubscribeFeedError::InvalidFeedUrl {
feed_url: url,
message: err.message,
}),
err_code => Err(SubscribeFeedError::Internal(anyhow::anyhow!(
"{err_code:?}"
))),
}
}
}
}
#[tracing::instrument(skip(self))]
pub async fn unsubscribe_feed(&self, url: String) -> anyhow::Result<()> {
let var = mutation::unsubscribe_feed::Variables {
input: mutation::unsubscribe_feed::UnsubscribeFeedInput { url },
};
let request = mutation::UnsubscribeFeed::build_query(var);
let response: mutation::unsubscribe_feed::ResponseData = self.request(&request).await?;
match response.unsubscribe_feed {
mutation::unsubscribe_feed::UnsubscribeFeedUnsubscribeFeed::UnsubscribeFeedSuccess(
_,
) => Ok(()),
mutation::unsubscribe_feed::UnsubscribeFeedUnsubscribeFeed::UnsubscribeFeedError(
err,
) => Err(anyhow!("Failed to mutate unsubscribe_feed {err:?}")),
}
}
#[tracing::instrument(skip(self))]
pub async fn fetch_entries(
&self,
after: Option<String>,
first: i64,
) -> anyhow::Result<payload::FetchEntriesPayload> {
tracing::debug!("Fetch entries...");
let var = query::entries::Variables { after, first };
let request = query::Entries::build_query(var);
let response: query::entries::ResponseData = self.request(&request).await?;
tracing::debug!("Got response");
Ok(response.output.into())
}
#[tracing::instrument(skip(self))]
pub async fn export_subscription(
&self,
after: Option<String>,
first: i64,
) -> anyhow::Result<ExportSubscriptionPayload> {
let var = query::export_subscription::Variables { after, first };
let request = query::ExportSubscription::build_query(var);
let response: query::export_subscription::ResponseData = self.request(&request).await?;
Ok(response.output.into())
}
#[tracing::instrument(skip_all, err(Display))]
async fn request<Body, ResponseData>(&self, body: &Body) -> anyhow::Result<ResponseData>
where
Body: Serialize + Debug + ?Sized,
ResponseData: DeserializeOwned + Debug,
{
let mut request = self
.client
.post(self.endpoint.join(Self::GRAPHQL).unwrap())
.header(
header::AUTHORIZATION,
self.credential
.as_ref()
.expect("Credential not configured. this is a BUG")
.clone(),
)
.json(body)
.build()?;
synd_o11y::opentelemetry::http::inject_with_baggage(
&Span::current().context(),
request.headers_mut(),
std::iter::once(synd_o11y::request_id_key_value()),
);
tracing::debug!(url = request.url().as_str(), "Send request");
let response: Response<ResponseData> = self
.client
.execute(request)
.await?
.error_for_status()?
.json()
.await?;
match (response.data, response.errors) {
(_, Some(errs)) if !errs.is_empty() => {
for err in &errs {
error!("{err:?}");
}
Err(anyhow::anyhow!(
"failed to request synd api: {}",
errs.first().unwrap()
))
}
(Some(data), _) => Ok(data),
_ => Err(anyhow::anyhow!("unexpected response",)),
}
}
// call health check api
pub async fn health(&self) -> anyhow::Result<Health> {
self.client
.get(self.endpoint.join(Self::HEALTH_CHECK).unwrap())
.send()
.await?
.error_for_status()?
.json()
.await
.map_err(anyhow::Error::from)
}
}