Skip to content

Commit 5bcc12a

Browse files
committed
improved paste/review API
1 parent 84332ff commit 5bcc12a

File tree

3 files changed

+41
-26
lines changed

3 files changed

+41
-26
lines changed

tmc-client/src/tmc_client.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Contains the TmcClient struct for communicating with the TMC server.
22
pub mod api_v8;
33

4+
use self::api_v8::{PasteData, ReviewData};
45
use crate::error::ClientError;
56
use crate::request::*;
67
use crate::response::*;
@@ -203,15 +204,6 @@ impl TmcClient {
203204
api_v8::core::get_exercise_details(self, exercise_ids)
204205
}
205206

206-
/// Fetches the course's information. Requires authentication.
207-
///
208-
/// # Errors
209-
/// If not authenticated, there's some problem reaching the API, or if the API returns an error.
210-
pub fn get_course_submissions(&self, course_id: u32) -> Result<Vec<Submission>, ClientError> {
211-
self.require_authentication()?;
212-
api_v8::submission::get_course_submissions_by_id(self, course_id)
213-
}
214-
215207
/// Fetches all courses under the given organization. Requires authentication.
216208
///
217209
/// # Errors
@@ -275,11 +267,17 @@ impl TmcClient {
275267
None,
276268
);
277269

270+
let paste = if let Some(message) = paste_message {
271+
PasteData::WithMessage(message)
272+
} else {
273+
PasteData::WithoutMessage
274+
};
275+
278276
let result = api_v8::core::submit_exercise(
279277
self,
280278
exercise_id,
281279
Cursor::new(compressed),
282-
paste_message,
280+
Some(paste),
283281
None,
284282
locale,
285283
)?;
@@ -531,20 +529,26 @@ impl TmcClient {
531529
&self,
532530
exercise_id: u32,
533531
submission_path: &Path,
534-
message_for_reviewer: String,
532+
message_for_reviewer: Option<String>,
535533
locale: Option<Language>,
536534
) -> Result<NewSubmission, ClientError> {
537535
self.require_authentication()?;
538536

539537
// compress
540538
let compressed = tmc_langs_plugins::compress_project_to_zip(submission_path)?;
541539

540+
let review = if let Some(message) = message_for_reviewer {
541+
ReviewData::WithMessage(message)
542+
} else {
543+
ReviewData::WithoutMessage
544+
};
545+
542546
api_v8::core::submit_exercise(
543547
self,
544548
exercise_id,
545549
Cursor::new(compressed),
546550
None,
547-
Some(message_for_reviewer),
551+
Some(review),
548552
locale,
549553
)
550554
}

tmc-client/src/tmc_client/api_v8.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ use std::{collections::HashMap, time::SystemTime};
1313
use tmc_langs_plugins::Language;
1414
use url::Url;
1515

16+
pub enum PasteData {
17+
WithoutMessage,
18+
WithMessage(String),
19+
}
20+
21+
pub enum ReviewData {
22+
WithoutMessage,
23+
WithMessage(String),
24+
}
25+
1626
// joins the URL "tail" with the API url root from the client
1727
fn make_url(client: &TmcClient, tail: impl AsRef<str>) -> Result<Url, ClientError> {
1828
client
@@ -843,8 +853,8 @@ pub mod core {
843853
client: &TmcClient,
844854
exercise_id: u32,
845855
submission_zip: impl Read + Send + Sync + 'static,
846-
paste_message: Option<String>,
847-
message_for_reviewer: Option<String>,
856+
submit_paste: Option<PasteData>,
857+
submit_for_review: Option<ReviewData>,
848858
locale: Option<Language>,
849859
) -> Result<NewSubmission, ClientError> {
850860
let url = make_url(
@@ -867,16 +877,17 @@ pub mod core {
867877
Part::reader(submission_zip).file_name("submission.zip"),
868878
);
869879

870-
if let Some(paste_message) = paste_message {
871-
form = form
872-
.text("paste", "1")
873-
.text("message_for_paste", paste_message);
880+
if let Some(submit_paste) = submit_paste {
881+
form = form.text("paste", "1");
882+
if let PasteData::WithMessage(message) = submit_paste {
883+
form = form.text("message_for_paste", message);
884+
}
874885
}
875-
876-
if let Some(message_for_reviewer) = message_for_reviewer {
877-
form = form
878-
.text("request_review", "1")
879-
.text("message_for_reviewer", message_for_reviewer);
886+
if let Some(submit_for_review) = submit_for_review {
887+
form = form.text("request_review", "1");
888+
if let ReviewData::WithMessage(message) = submit_for_review {
889+
form = form.text("message_for_reviewer", message);
890+
}
880891
}
881892

882893
if let Some(locale) = locale {
@@ -2398,8 +2409,8 @@ mod test {
23982409
client,
23992410
0,
24002411
Cursor::new(vec![]),
2401-
Some("paste".to_string()),
2402-
Some("message".to_string()),
2412+
Some(PasteData::WithMessage("paste".to_string())),
2413+
Some(ReviewData::WithMessage("message".to_string())),
24032414
Some(Language::from_639_1("fi").unwrap()),
24042415
)
24052416
.unwrap();

tmc-langs-cli/src/app.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ pub enum Core {
420420
locale: Locale,
421421
/// Message for the review.
422422
#[structopt(long)]
423-
message_for_reviewer: String,
423+
message_for_reviewer: Option<String>,
424424
/// Path to the directory where the submission resides.
425425
#[structopt(long)]
426426
submission_path: PathBuf,

0 commit comments

Comments
 (0)