leetcode_core/graphql/query/
submit_code.rs

1use super::{GQLLeetcodeRequest, RunOrSubmitCodeCheckResult};
2use crate::types::run_submit_response::RunSubmitResult;
3pub use crate::types::submit::{SubmitCodeIntermediateResponse, SubmitCodeRequest};
4
5impl GQLLeetcodeRequest for SubmitCodeRequest {
6    type T = SubmitCodeIntermediateResponse;
7
8    fn get_endpoint(&self) -> String {
9        let slug = self.slug.as_str();
10        format!("https://leetcode.com/problems/{slug}/submit/")
11    }
12}
13
14/// Polling is done to retrieve the run status from the server. Hence it may take indefinite time to run the solution on leetcode.
15impl GQLLeetcodeRequest for SubmitCodeIntermediateResponse {
16    type T = RunSubmitResult;
17    fn is_post(&self) -> bool {
18        false
19    }
20
21    fn get_endpoint(&self) -> String {
22        let submission_id = self.submission_id;
23        format!("https://leetcode.com/submissions/detail/{submission_id}/check/")
24    }
25}
26
27impl RunOrSubmitCodeCheckResult<SubmitCodeIntermediateResponse> for SubmitCodeRequest {}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn test() {
35        // Request JSON data as a string
36        let request_json_data = r#"
37            {
38                "lang": "python3",
39                "question_id": "1",
40                "typed_code": "class Solution:\n    def twoSum(self, nums: List[int], target: int) -> List[int]:    return [1]"
41            }
42        "#;
43
44        // Parse Request JSON data into the RequestBody struct
45        let request_body: SubmitCodeRequest = serde_json::from_str(request_json_data).unwrap();
46        println!("Request Body: {:?}", request_body);
47
48        // Define expected request body
49        let expected_request_body = SubmitCodeRequest {
50            lang: crate::types::language::Language::Python3,
51            question_id: "1".to_string(),
52            typed_code: "class Solution:\n    def twoSum(self, nums: List[int], target: int) -> List[int]:    return [1]".to_string(),
53            slug: "".to_string()
54        };
55
56        // Test if the parsed request body matches the expected request body
57        assert_eq!(request_body, expected_request_body);
58
59        // Response JSON data as a string
60        let response_json_data = r#"
61            {
62                "submission_id": 1001727658
63            }
64        "#;
65
66        // Parse Response JSON data into the ResponseBody struct
67        let response_body: SubmitCodeIntermediateResponse =
68            serde_json::from_str(response_json_data).unwrap();
69        println!("Response Body: {:?}", response_body);
70
71        // Define expected response body
72        let expected_response_body = SubmitCodeIntermediateResponse {
73            submission_id: 1001727658,
74        };
75
76        // Test if the parsed response body matches the expected response body
77        assert_eq!(response_body, expected_response_body);
78    }
79}