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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//! Types and functions to interact with the [Exercism website](https://exercism.org) v1 API.
pub mod ping;
pub mod solution;
pub mod track;
use futures::future::Either;
use futures::stream;
use crate::http::StatusCode;
use crate::stream::{Bytes, Stream, StreamExt, TryStreamExt};
use crate::{Error, Result};
/// Default base URL for the [Exercism website](https://exercism.org) v1 API.
pub const DEFAULT_V1_API_BASE_URL: &str = "https://api.exercism.io/v1";
define_api_client! {
/// Client for the [Exercism website](https://exercism.org) v1 API.
///
/// This API is undocumented and is mostly used by the [Exercism CLI](https://exercism.org/docs/using/solving-exercises/working-locally)
/// to download solution files.
pub struct Client(DEFAULT_V1_API_BASE_URL);
}
impl Client {
/// Returns information about a specific solution submitted by the user.
///
/// The `solution_uuid` can be obtained from the mentoring interface, or
/// returned by another API, like [`api::v2::Client::get_exercises`]
/// (see [`Solution::uuid`]).
///
/// # Notes
///
/// Performing this request requires [`credentials`], otherwise a
/// `401 Unauthorized` error will be returned.
///
/// # Errors
///
/// - [`ApiError`]: Error while fetching solution information from API
///
/// # Examples
///
/// ```no_run
/// use mini_exercism::api;
/// use mini_exercism::core::Credentials;
///
/// async fn get_solution_url(api_token: &str, solution_uuid: &str) -> anyhow::Result<String> {
/// let credentials = Credentials::from_api_token(api_token);
/// let client = api::v1::Client::builder()
/// .credentials(credentials)
/// .build()?;
///
/// Ok(client.get_solution(solution_uuid).await?.solution.url)
/// }
/// ```
///
/// [`api::v2::Client::get_exercises`]: crate::api::v2::Client::get_exercises
/// [`Solution::uuid`]: crate::api::v2::solution::Solution::uuid
/// [`credentials`]: ClientBuilder::credentials
/// [`ApiError`]: Error::ApiError
#[cfg_attr(not(coverage), tracing::instrument(skip(self), ret, err))]
pub async fn get_solution(&self, uuid: &str) -> Result<solution::Response> {
self.api_client
.get(format!("/solutions/{uuid}"))
.execute()
.await
}
/// Returns information about the latest solution submitted by the user for
/// a given exercise.
///
/// # Notes
///
/// Performing this request requires [`credentials`](ClientBuilder::credentials),
/// otherwise a `401 Unauthorized` error will be returned.
///
/// # Errors
///
/// - [`ApiError`]: Error while fetching solution information from API
///
/// # Examples
///
/// ```no_run
/// use mini_exercism::api;
/// use mini_exercism::core::Credentials;
///
/// async fn get_latest_solution_url(
/// api_token: &str,
/// track: &str,
/// exercise: &str,
/// ) -> anyhow::Result<String> {
/// let credentials = Credentials::from_api_token(api_token);
/// let client = api::v1::Client::builder()
/// .credentials(credentials)
/// .build()?;
///
/// Ok(client
/// .get_latest_solution(track, exercise)
/// .await?
/// .solution
/// .url)
/// }
/// ```
///
/// [`ApiError`]: Error::ApiError
#[cfg_attr(not(coverage), tracing::instrument(skip(self), ret, err))]
pub async fn get_latest_solution(
&self,
track: &str,
exercise: &str,
) -> Result<solution::Response> {
self.api_client
.get("/solutions/latest")
.query(("track_id", Some(track)))
.query(("exercise_id", Some(exercise)))
.execute()
.await
}
/// Returns the contents of a specific file that is part of a solution.
///
/// # Arguments
///
/// - `solution_uuid` - [UUID](solution::Solution::uuid) of the solution containing the file.
/// - `file_path` - Path to the file, as returned in [`solution::Solution::files`].
///
/// # Notes
///
/// - Performing this request requires [`credentials`](ClientBuilder::credentials),
/// otherwise a `401 Unauthorized` error will be returned.
/// - If the API call to fetch file content fails, this method will return a [`Stream`]
/// containing a single [`ApiError`].
///
/// # Examples
///
/// ```no_run
/// use std::io::Write;
///
/// use mini_exercism::api;
/// use mini_exercism::core::Credentials;
/// use mini_exercism::stream::StreamExt;
///
/// async fn get_file_content(
/// api_token: &str,
/// track: &str,
/// exercise: &str,
/// file: &str,
/// ) -> anyhow::Result<String> {
/// let credentials = Credentials::from_api_token(api_token);
/// let client = api::v1::Client::builder()
/// .credentials(credentials)
/// .build()?;
///
/// let solution = client.get_latest_solution(track, exercise).await?.solution;
/// let mut file_response = client.get_file(&solution.uuid, file).await;
/// let mut file_content: Vec<u8> = Vec::new();
/// while let Some(bytes) = file_response.next().await {
/// file_content.write_all(&bytes?)?;
/// }
///
/// Ok(String::from_utf8(file_content).expect("File should be valid UTF-8"))
/// }
/// ```
///
/// [`ApiError`]: Error::ApiError
#[cfg_attr(not(coverage), tracing::instrument(skip(self)))]
pub async fn get_file(
&self,
solution_uuid: &str,
file_path: &str,
) -> impl Stream<Item = Result<Bytes>> + use<> {
let result = self
.api_client
.get(format!("/solutions/{solution_uuid}/files/{file_path}"))
.send()
.await;
// The result of `stream::once` is not `Unpin`, so calling `boxed()` will make sure it's
// possible for callers to use `next()` on the returned `Stream` without pinning it first.
match result {
Ok(response) => Either::Left(response.bytes_stream().map_err(|err| err.into())),
Err(error) => Either::Right(stream::once(async { Err(error) }).boxed()),
}
}
/// Returns information about a language track.
///
/// # Notes
///
/// Perhaps strangely, performing this request requires [`credentials`](ClientBuilder::credentials),
/// otherwise a `401 Unauthorized` error will be returned.
///
/// # Errors
///
/// - [`ApiError`]: Error while fetching track information from API
///
/// # Examples
///
/// ```no_run
/// use mini_exercism::api;
/// use mini_exercism::api::v1::track::Track;
/// use mini_exercism::core::Credentials;
///
/// async fn get_language_track_details(api_token: &str, track: &str) -> anyhow::Result<Track> {
/// let credentials = Credentials::from_api_token(api_token);
/// let client = api::v1::Client::builder()
/// .credentials(credentials)
/// .build()?;
///
/// Ok(client.get_track(track).await?.track)
/// }
/// ```
///
/// [`ApiError`]: Error::ApiError
#[cfg_attr(not(coverage), tracing::instrument(skip(self), ret, err))]
pub async fn get_track(&self, track: &str) -> Result<track::Response> {
self.api_client
.get(format!("/tracks/{track}"))
.execute()
.await
}
/// Validates the token used to perform API requests.
///
/// If the API token is invalid or if the query is performed without [`credentials`],
/// the API will return `401 Unauthorized` and this method will return `false`.
/// If another HTTP, error is returned by the API, this method will return an [`ApiError`].
///
/// # Errors
///
/// - [`ApiError`]: Error while validating API token (other than `401 Unauthorized`)
///
/// # Examples
///
/// ```no_run
/// use mini_exercism::api;
/// use mini_exercism::core::Credentials;
///
/// async fn is_api_token_valid(api_token: &str) -> bool {
/// let credentials = Credentials::from_api_token(api_token);
/// match api::v1::Client::builder().credentials(credentials).build() {
/// Ok(client) => client.validate_token().await.unwrap_or(false),
/// Err(_) => false,
/// }
/// }
/// ```
///
/// [`credentials`]: ClientBuilder::credentials
/// [`ApiError`]: Error::ApiError
#[cfg_attr(not(coverage), tracing::instrument(skip(self), ret, err))]
pub async fn validate_token(&self) -> Result<bool> {
// This API call returns a payload, but it doesn't really contain useful information:
// if the token is invalid, 401 will be returned.
let response = self.api_client.get("/validate_token").send().await;
match response {
Ok(_) => Ok(true),
Err(Error::ApiError(error)) if error.status() == Some(StatusCode::UNAUTHORIZED) => {
Ok(false)
},
Err(error) => Err(error),
}
}
/// Sends a "ping" to the server to determine if service is up and available.
///
/// The call returns information about the website and database.
///
/// # Notes
///
/// - This call does not require [`credentials`], but works anyway if they are provided.
/// - As of this writing, the [current implementation] of this endpoint always return `true`
/// as status for all components. It makes sense if you think about it: if the database
/// or the Rails server misbehave, then the API would be inaccessible anyway 😅 It also
/// means that if the service is actually down, this method will simply return an [`ApiError`].
///
/// # Errors
///
/// - [`ApiError`]: Error while pinging API
///
/// # Examples
///
/// ```no_run
/// use mini_exercism::api;
/// use mini_exercism::core::Credentials;
///
/// async fn report_service_status() -> anyhow::Result<()> {
/// let client = api::v1::Client::new()?;
///
/// let service_status = client.ping().await?.status;
/// println!(
/// "Status: website: {}, database: {}",
/// service_status.website, service_status.database,
/// );
///
/// Ok(())
/// }
/// ```
///
/// [`credentials`]: ClientBuilder::credentials
/// [current implementation]: https://github.com/exercism/website/blob/2580b8fa2b13cad7aa7e8a877551bbd8552bee8b/app/controllers/api/v1/ping_controller.rb
/// [`ApiError`]: Error::ApiError
#[cfg_attr(not(coverage), tracing::instrument(skip(self), ret, err))]
pub async fn ping(&self) -> Result<ping::Response> {
self.api_client.get("/ping").execute().await
}
}