pub struct Client {
pub client: Client,
pub username: String,
pub logged: bool,
}
Expand description
Client you will do almost everything with
Fields§
§client: Client
The reqwest client
username: String
username
logged: bool
if client has token, this should be true and false otherwise
Implementations§
Source§impl Client
Client functions return:
impl Client
Client functions return:
Err(1)
if request is not Ok, usually when server is unreachableErr(2)
when json cannot be parsed, usually unreachable or due to incorrect parametersErr(3)
if the json contains success=0
Sourcepub fn new(username: String) -> Self
pub fn new(username: String) -> Self
create a new client with given username.
always use this to create a client
for requests you don’t need to be logged in to do, username will not be used
Examples found in repository?
3fn main() {
4 //make a client
5 let client = Client::new(String::from("Gemmady"));
6 let task_list = client.get_task_list(0, 1024, "", None, None).unwrap();
7 let mut hm = std::collections::HashMap::<String, u32>::new();
8 for i in task_list.tasks {
9 let best = client.get_stats(&i.name).unwrap().best;
10 if best.len() > 0 {
11 let t = hm.entry(best[0].username.clone()).or_insert(0);
12 *t += 1;
13 }
14 }
15 let mut v: Vec<(u32, String)> = hm.iter().map(|x| (*x.1, x.0.clone())).collect();
16 v.sort();
17 for i in v.iter().rev() {
18 println!("{} {}", i.1, i.0);
19 }
20}
More examples
3fn main(){
4 let username = String::from("user");
5 let password = "password";
6 let mut client = Client::new(username.clone());
7 client.login(password).unwrap();
8 let user = client.get_user(&username).unwrap();
9 for sc in user.scores.unwrap() {
10 if sc.score == 100.0 {
11 println!("{} has score 100", sc.title);
12 let sub_list = client.get_submission_list(&sc.name).unwrap();
13 let best_sub = sub_list.get_fastest_high(&client).unwrap();
14 let files = &best_sub.files;
15 if files.len() == 1 { // if it is not an output-only
16 let mut submitted: bool = false;
17 print!("Resubmitting {}", sc.name.clone());
18 while !submitted { // because cmsocial has a limit to submission rate
19 print!(".");
20 if client
21 .submit_normal(
22 &sc.name,
23 &client.get_file(&files[0]).unwrap(),
24 files[0].name.split(".").collect::<Vec<&str>>().last().unwrap(),
25 )
26 .is_ok()
27 {
28 submitted = true;
29 }
30 }
31 println!("");
32 }
33 }
34 }
35}
Sourcepub fn login(&mut self, password: &str) -> Result<bool, u8>
pub fn login(&mut self, password: &str) -> Result<bool, u8>
login with self.username and password
Returns Ok(true)
if the client was already logged and Ok(false)
if it was not and succeeds in logging
Examples found in repository?
3fn main(){
4 let username = String::from("user");
5 let password = "password";
6 let mut client = Client::new(username.clone());
7 client.login(password).unwrap();
8 let user = client.get_user(&username).unwrap();
9 for sc in user.scores.unwrap() {
10 if sc.score == 100.0 {
11 println!("{} has score 100", sc.title);
12 let sub_list = client.get_submission_list(&sc.name).unwrap();
13 let best_sub = sub_list.get_fastest_high(&client).unwrap();
14 let files = &best_sub.files;
15 if files.len() == 1 { // if it is not an output-only
16 let mut submitted: bool = false;
17 print!("Resubmitting {}", sc.name.clone());
18 while !submitted { // because cmsocial has a limit to submission rate
19 print!(".");
20 if client
21 .submit_normal(
22 &sc.name,
23 &client.get_file(&files[0]).unwrap(),
24 files[0].name.split(".").collect::<Vec<&str>>().last().unwrap(),
25 )
26 .is_ok()
27 {
28 submitted = true;
29 }
30 }
31 println!("");
32 }
33 }
34 }
35}
Sourcepub fn recover(&self, email: &str, code: &str) -> Result<RecoverResponse, u8>
pub fn recover(&self, email: &str, code: &str) -> Result<RecoverResponse, u8>
recover lost password, use empty code to get the email
Sourcepub fn user_update(
&self,
email: &str,
password: &str,
old_password: &str,
) -> Result<(), u8>
pub fn user_update( &self, email: &str, password: &str, old_password: &str, ) -> Result<(), u8>
update password/email, empty string for fields you dont want to update
Sourcepub fn check_username(&self, username: &str) -> Result<CheckResponse, u8>
pub fn check_username(&self, username: &str) -> Result<CheckResponse, u8>
check if username is valid, note: Ok
does not mean username is valid
Sourcepub fn check_email(&self, email: &str) -> Result<CheckResponse, u8>
pub fn check_email(&self, email: &str) -> Result<CheckResponse, u8>
check if email is valid, note: Ok
does not mean email is valid
Sourcepub fn check_password(&self, password: &str) -> bool
pub fn check_password(&self, password: &str) -> bool
check if password is valid, note: this is done locally
unlike other functions, this returs true if password is acceptable and false otherwise
Sourcepub fn user_exists(&self, username: &str) -> Result<bool, u8>
pub fn user_exists(&self, username: &str) -> Result<bool, u8>
check if there is an user with username = username
Sourcepub fn get_user_list(&self, first: usize, last: usize) -> Result<UserList, u8>
pub fn get_user_list(&self, first: usize, last: usize) -> Result<UserList, u8>
get list of users in reverse order of score in [first,last)
Sourcepub fn get_user(&self, username: &str) -> Result<User, u8>
pub fn get_user(&self, username: &str) -> Result<User, u8>
get the details of a specific user
Examples found in repository?
3fn main(){
4 let username = String::from("user");
5 let password = "password";
6 let mut client = Client::new(username.clone());
7 client.login(password).unwrap();
8 let user = client.get_user(&username).unwrap();
9 for sc in user.scores.unwrap() {
10 if sc.score == 100.0 {
11 println!("{} has score 100", sc.title);
12 let sub_list = client.get_submission_list(&sc.name).unwrap();
13 let best_sub = sub_list.get_fastest_high(&client).unwrap();
14 let files = &best_sub.files;
15 if files.len() == 1 { // if it is not an output-only
16 let mut submitted: bool = false;
17 print!("Resubmitting {}", sc.name.clone());
18 while !submitted { // because cmsocial has a limit to submission rate
19 print!(".");
20 if client
21 .submit_normal(
22 &sc.name,
23 &client.get_file(&files[0]).unwrap(),
24 files[0].name.split(".").collect::<Vec<&str>>().last().unwrap(),
25 )
26 .is_ok()
27 {
28 submitted = true;
29 }
30 }
31 println!("");
32 }
33 }
34 }
35}
Sourcepub fn get_task_list(
&self,
first: usize,
last: usize,
order: &str,
tag: Option<&str>,
search: Option<&str>,
) -> Result<TaskList, u8>
pub fn get_task_list( &self, first: usize, last: usize, order: &str, tag: Option<&str>, search: Option<&str>, ) -> Result<TaskList, u8>
get list of tasks in [first,last) in the given order with the given tag that matches search
possible orders are: newest, easiest, hardest
if an invalid order is given, it is assumed to be newest
Examples found in repository?
3fn main() {
4 //make a client
5 let client = Client::new(String::from("Gemmady"));
6 let task_list = client.get_task_list(0, 1024, "", None, None).unwrap();
7 let mut hm = std::collections::HashMap::<String, u32>::new();
8 for i in task_list.tasks {
9 let best = client.get_stats(&i.name).unwrap().best;
10 if best.len() > 0 {
11 let t = hm.entry(best[0].username.clone()).or_insert(0);
12 *t += 1;
13 }
14 }
15 let mut v: Vec<(u32, String)> = hm.iter().map(|x| (*x.1, x.0.clone())).collect();
16 v.sort();
17 for i in v.iter().rev() {
18 println!("{} {}", i.1, i.0);
19 }
20}
Sourcepub fn get_task(&self, name: &str) -> Result<DetailedTask, u8>
pub fn get_task(&self, name: &str) -> Result<DetailedTask, u8>
get the details of a specific task
Sourcepub fn get_stats(&self, name: &str) -> Result<Stats, u8>
pub fn get_stats(&self, name: &str) -> Result<Stats, u8>
get the statistics for a specific task
Examples found in repository?
3fn main() {
4 //make a client
5 let client = Client::new(String::from("Gemmady"));
6 let task_list = client.get_task_list(0, 1024, "", None, None).unwrap();
7 let mut hm = std::collections::HashMap::<String, u32>::new();
8 for i in task_list.tasks {
9 let best = client.get_stats(&i.name).unwrap().best;
10 if best.len() > 0 {
11 let t = hm.entry(best[0].username.clone()).or_insert(0);
12 *t += 1;
13 }
14 }
15 let mut v: Vec<(u32, String)> = hm.iter().map(|x| (*x.1, x.0.clone())).collect();
16 v.sort();
17 for i in v.iter().rev() {
18 println!("{} {}", i.1, i.0);
19 }
20}
Sourcepub fn get_submission_list(&self, task_name: &str) -> Result<SubmissionList, u8>
pub fn get_submission_list(&self, task_name: &str) -> Result<SubmissionList, u8>
get your submissions for a task
Examples found in repository?
3fn main(){
4 let username = String::from("user");
5 let password = "password";
6 let mut client = Client::new(username.clone());
7 client.login(password).unwrap();
8 let user = client.get_user(&username).unwrap();
9 for sc in user.scores.unwrap() {
10 if sc.score == 100.0 {
11 println!("{} has score 100", sc.title);
12 let sub_list = client.get_submission_list(&sc.name).unwrap();
13 let best_sub = sub_list.get_fastest_high(&client).unwrap();
14 let files = &best_sub.files;
15 if files.len() == 1 { // if it is not an output-only
16 let mut submitted: bool = false;
17 print!("Resubmitting {}", sc.name.clone());
18 while !submitted { // because cmsocial has a limit to submission rate
19 print!(".");
20 if client
21 .submit_normal(
22 &sc.name,
23 &client.get_file(&files[0]).unwrap(),
24 files[0].name.split(".").collect::<Vec<&str>>().last().unwrap(),
25 )
26 .is_ok()
27 {
28 submitted = true;
29 }
30 }
31 println!("");
32 }
33 }
34 }
35}
Sourcepub fn get_submission(&self, id: usize) -> Result<DetailedSubmission, u8>
pub fn get_submission(&self, id: usize) -> Result<DetailedSubmission, u8>
get details for specific submission
Sourcepub fn submit_normal(
&self,
task_name: &str,
text: &str,
lang: &str,
) -> Result<DetailedSubmission, u8>
pub fn submit_normal( &self, task_name: &str, text: &str, lang: &str, ) -> Result<DetailedSubmission, u8>
submit a not output-only task
lang should be the extension of the file (c, cpp, pas)
Examples found in repository?
3fn main(){
4 let username = String::from("user");
5 let password = "password";
6 let mut client = Client::new(username.clone());
7 client.login(password).unwrap();
8 let user = client.get_user(&username).unwrap();
9 for sc in user.scores.unwrap() {
10 if sc.score == 100.0 {
11 println!("{} has score 100", sc.title);
12 let sub_list = client.get_submission_list(&sc.name).unwrap();
13 let best_sub = sub_list.get_fastest_high(&client).unwrap();
14 let files = &best_sub.files;
15 if files.len() == 1 { // if it is not an output-only
16 let mut submitted: bool = false;
17 print!("Resubmitting {}", sc.name.clone());
18 while !submitted { // because cmsocial has a limit to submission rate
19 print!(".");
20 if client
21 .submit_normal(
22 &sc.name,
23 &client.get_file(&files[0]).unwrap(),
24 files[0].name.split(".").collect::<Vec<&str>>().last().unwrap(),
25 )
26 .is_ok()
27 {
28 submitted = true;
29 }
30 }
31 println!("");
32 }
33 }
34 }
35}
Sourcepub fn get_test_list(&self) -> Result<TestList, u8>
pub fn get_test_list(&self) -> Result<TestList, u8>
get the list of available tests
Sourcepub fn get_test(&self, test_name: &str) -> Result<Test, u8>
pub fn get_test(&self, test_name: &str) -> Result<Test, u8>
get the details and text of a specific test
Sourcepub fn get_region_list(&self) -> Result<RegionList, u8>
pub fn get_region_list(&self) -> Result<RegionList, u8>
Misc get a list of the regions
Sourcepub fn get_technique_list(&self) -> Result<TechniqueList, u8>
pub fn get_technique_list(&self) -> Result<TechniqueList, u8>
get list of technique tags
Sourcepub fn get_file(&self, file: &File) -> Result<String, u8>
pub fn get_file(&self, file: &File) -> Result<String, u8>
download file
Examples found in repository?
3fn main(){
4 let username = String::from("user");
5 let password = "password";
6 let mut client = Client::new(username.clone());
7 client.login(password).unwrap();
8 let user = client.get_user(&username).unwrap();
9 for sc in user.scores.unwrap() {
10 if sc.score == 100.0 {
11 println!("{} has score 100", sc.title);
12 let sub_list = client.get_submission_list(&sc.name).unwrap();
13 let best_sub = sub_list.get_fastest_high(&client).unwrap();
14 let files = &best_sub.files;
15 if files.len() == 1 { // if it is not an output-only
16 let mut submitted: bool = false;
17 print!("Resubmitting {}", sc.name.clone());
18 while !submitted { // because cmsocial has a limit to submission rate
19 print!(".");
20 if client
21 .submit_normal(
22 &sc.name,
23 &client.get_file(&files[0]).unwrap(),
24 files[0].name.split(".").collect::<Vec<&str>>().last().unwrap(),
25 )
26 .is_ok()
27 {
28 submitted = true;
29 }
30 }
31 println!("");
32 }
33 }
34 }
35}
Auto Trait Implementations§
impl Freeze for Client
impl !RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl !UnwindSafe for Client
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more