From bcfcfd893cc3ebcf4b5a8e6a15ec3bfd91633ec4 Mon Sep 17 00:00:00 2001 From: Daniel Martinez Date: Wed, 2 Jun 2021 13:48:45 +0300 Subject: [PATCH] added an optional feedback-url parameter back to send-feedback --- tmc-client/src/tmc_client.rs | 14 ++++++++++ tmc-client/src/tmc_client/api_v8.rs | 41 ++++++++++++++++++++++------- tmc-langs-cli/src/app.rs | 7 +++-- tmc-langs-cli/src/lib.rs | 15 ++++++++--- 4 files changed, 63 insertions(+), 14 deletions(-) diff --git a/tmc-client/src/tmc_client.rs b/tmc-client/src/tmc_client.rs index 5c90ec6f888..8808d41f0bb 100644 --- a/tmc-client/src/tmc_client.rs +++ b/tmc-client/src/tmc_client.rs @@ -302,6 +302,20 @@ impl TmcClient { api_v8::core::post_submission_feedback(self, submission_id, feedback) } + /// Posts feedback to the given URL. Requires authentication. + /// + /// # Errors + /// If not authenticated, there's some problem reaching the API, or if the API returns an error. + pub fn send_feedback_to_url( + &self, + feedback_url: Url, + feedback: Vec, + ) -> Result { + self.require_authentication()?; + let form = api_v8::prepare_feedback_form(feedback); + api_v8::post_form(self, feedback_url, &form) + } + /// Sends the submission to the server. Requires authentication. /// /// # Errors diff --git a/tmc-client/src/tmc_client/api_v8.rs b/tmc-client/src/tmc_client/api_v8.rs index b2796b6d62b..3fd48365875 100644 --- a/tmc-client/src/tmc_client/api_v8.rs +++ b/tmc-client/src/tmc_client/api_v8.rs @@ -80,6 +80,19 @@ fn assert_success(response: Response, url: &Url) -> Result) -> HashMap { + let mut form = HashMap::new(); + for (i, answer) in feedback.into_iter().enumerate() { + form.insert( + format!("answers[{}][question_id]", i), + answer.question_id.to_string(), + ); + form.insert(format!("answers[{}][answer]", i), answer.answer); + } + form +} + /// Fetches data from the URL and writes it into the target. pub fn download(client: &TmcClient, url: Url, mut target: impl Write) -> Result<(), ClientError> { let res = prepare_tmc_request(client, Method::GET, url.clone()) @@ -111,6 +124,24 @@ pub fn get_json( Ok(json) } +/// Posts the given form data to the given URL and deserializes the response to T. +pub fn post_form( + client: &TmcClient, + url: Url, + form: &HashMap, +) -> Result { + let res = prepare_tmc_request(client, Method::POST, url.clone()) + .form(form) + .send() + .map_err(|e| ClientError::ConnectionError(Method::GET, url.clone(), e))?; + + let res = assert_success(res, &url)?; + let json = res + .json() + .map_err(|e| ClientError::HttpJsonResponse(url, e))?; + Ok(json) +} + /// get /api/v8/application/{client_name}/credentials /// Fetches oauth2 credentials info. pub fn get_credentials(client: &TmcClient, client_name: &str) -> Result { @@ -948,15 +979,7 @@ pub mod core { format!("/api/v8/core/submissions/{}/feedback", submission_id), )?; - let mut form = HashMap::new(); - for (i, answer) in feedback.into_iter().enumerate() { - form.insert( - format!("answers[{}][question_id]", i), - answer.question_id.to_string(), - ); - form.insert(format!("answers[{}][answer]", i), answer.answer); - } - + let form = prepare_feedback_form(feedback); let res = prepare_tmc_request(client, Method::POST, url.clone()) .form(&form) .send() diff --git a/tmc-langs-cli/src/app.rs b/tmc-langs-cli/src/app.rs index 06344301762..004365c6553 100644 --- a/tmc-langs-cli/src/app.rs +++ b/tmc-langs-cli/src/app.rs @@ -444,8 +444,11 @@ pub enum Core { #[structopt(long_about = schema_leaked::())] SendFeedback { /// The ID of the submission. - #[structopt(long)] - submission_id: u32, + #[structopt(long, required_unless = "feedback-url")] + submission_id: Option, + /// The feedback answer URL. + #[structopt(long, required_unless = "submission-id")] + feedback_url: Option, /// A feedback answer. Takes two values, a feedback answer id and the answer. Multiple feedback arguments can be given. #[structopt(long, required = true, number_of_values = 2, value_names = &["feedback-answer-id, answer"])] feedback: Vec, diff --git a/tmc-langs-cli/src/lib.rs b/tmc-langs-cli/src/lib.rs index 99856547f8c..e1d3b1c3ab7 100644 --- a/tmc-langs-cli/src/lib.rs +++ b/tmc-langs-cli/src/lib.rs @@ -864,6 +864,7 @@ fn run_core_inner( Core::SendFeedback { submission_id, + feedback_url, feedback, } => { let mut feedback_answers = feedback.into_iter(); @@ -879,9 +880,17 @@ fn run_core_inner( answer, }); } - let response = client - .send_feedback(submission_id, feedback) - .context("Failed to send feedback")?; + + let response = if let Some(submission_id) = submission_id { + client + .send_feedback(submission_id, feedback) + .context("Failed to send feedback")? + } else if let Some(feedback_url) = feedback_url { + let feedback_url = feedback_url.parse()?; + client.send_feedback_to_url(feedback_url, feedback)? + } else { + panic!("validation error") + }; Output::finished_with_data("sent feedback", Data::SubmissionFeedbackResponse(response)) }