Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e25aad1

Browse files
committed
feat: Add profile integration
1 parent 66ccc99 commit e25aad1

File tree

6 files changed

+220
-37
lines changed

6 files changed

+220
-37
lines changed

‎README.md‎

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ leetcoderustapi = "0.1.6"
1717
### Authentication
1818
To use the LeetCode API, you need to obtain an authentication token. Follow the instructions provided by LeetCode to obtain your token.
1919

20-
### Example
20+
### Example: Actions with problems
2121
```rust
2222
use leetcoderustapi::UserApi;
2323

@@ -52,8 +52,10 @@ async fn main() {
5252

5353
// Fetch the full data for a specific problem
5454
let problem_info = api.set_problem("two sum").await.unwrap();
55+
5556
// Retrieve previous submissions to this problem
5657
let my_submissions = problem_info.my_submissions().await.unwrap();
58+
5759
// Retrieve code snippets
5860
let code_snippets = problem_info.code_snippets().unwrap();
5961

@@ -64,22 +66,22 @@ async fn main() {
6466
let related_topics = problem_info.related_topics();
6567

6668
// Retrieve similar questions
67-
let similar_questions = problem_info.similar_questions();
69+
let similar_questions = problem_info.similar_questions().unwrap();
6870

6971
// Retrieve stats
70-
let stats = problem_info.stats();
72+
let stats = problem_info.stats().unwrap();
7173

7274
// Retrieve hints
7375
let hints = problem_info.hints();
7476

7577
// Retrieve description
76-
let description = problem_info.description();
78+
let description = problem_info.description().unwrap();
7779

7880
// Retrieve difficulty
7981
let difficulty = problem_info.difficulty();
8082

8183
// Retrieve likes and dislikes
82-
let likes = problem_info.rating();
84+
let likes = problem_info.rating().unwrap();
8385

8486
// Retrieve category
8587
let category = problem_info.category();
@@ -96,9 +98,55 @@ async fn main() {
9698
.unwrap();
9799
}
98100
```
99-
### That is what is it looks like:
100101

101-
![work](https://github.com/1101-1/LeetcodeRustAPI/assets/70093559/70806622-526f-4307-b3b6-c25335ed4421)
102+
### Example: Actions with User profile
103+
```rust
104+
#[tokio::main]
105+
async fn main() {
106+
// Set cookie from leetcode
107+
let token = std::env::var("COOKIE").expect("cookie doesn't set");
108+
109+
// Create a new LeetCode client
110+
let api = UserApi::new(&token).await.unwrap();
111+
112+
// Create interaction with profile
113+
let user_profile = api.my_profile().await.unwrap();
114+
115+
// Create empty list of the problems with provided name
116+
user_profile
117+
.create_fav_list("my_new_favorite_list")
118+
.await
119+
.unwrap();
120+
121+
// Rename lists
122+
user_profile
123+
.rename_fav_list("my_new_favorite_list", "hard_problems")
124+
.await
125+
.unwrap();
126+
127+
// Set list puplic
128+
user_profile.set_public("hard_problems").await.unwrap();
129+
130+
// Set list private
131+
user_profile.set_private("hard_problems").await.unwrap();
132+
133+
// Get link to the list if it is public
134+
user_profile.get_share_url("hard_problems").await.unwrap();
135+
136+
// Show existing lists
137+
user_profile.show_lists();
138+
139+
// Delete list with provided name
140+
user_profile
141+
.delete_list("hard_problems")
142+
.await
143+
.unwrap();
144+
145+
// Show users last 10 notification
146+
user_profile.get_notifications().await.unwrap();
147+
}
148+
```
149+
102150

103151

104152
#### Important

‎src/problem_actions.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use crate::{
1717

1818
#[derive(Debug)]
1919
pub struct Problem {
20-
pub client: reqwest::Client,
21-
pub task_search_name: String,
20+
pub(crate) client: reqwest::Client,
21+
pub(crate) task_search_name: String,
2222
pub full_data: ProblemFullData,
2323
}
2424

‎src/problem_build.rs‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@ use crate::{error::Errors, resources::descr::ProblemData, Category, Difficulty,
55

66
#[derive(Debug)]
77
pub struct ProblemBuilder {
8-
pub client: reqwest::Client,
9-
pub key_word: String,
10-
pub limit: u32,
11-
pub category: String,
12-
pub filters: Filters,
8+
pub(crate) client: reqwest::Client,
9+
pub(crate) key_word: String,
10+
pub(crate) limit: u32,
11+
pub(crate) category: String,
12+
pub(crate) filters: Filters,
1313
}
1414

1515
#[allow(non_snake_case)]
1616
#[derive(Debug, Deserialize)]
1717
pub struct Filters {
18-
difficulty: String,
19-
orderBy: String,
20-
sortOrder: String,
21-
status: String,
22-
tags: Vec<String>,
18+
pubdifficulty: String,
19+
puborderBy: String,
20+
pubsortOrder: String,
21+
pubstatus: String,
22+
pubtags: Vec<String>,
2323
}
2424

2525
impl Default for Filters {

‎src/profile.rs‎

Lines changed: 98 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
use serde_json::json;
22

3-
use crate::{error::Errors, resources::fav_list::FavoriteList};
3+
use crate::{
4+
error::Errors,
5+
resources::{fav_list::FavoriteList, notification::NotificationsData},
6+
};
47

8+
#[derive(Debug)]
59
pub struct MyProfile {
6-
pub client: reqwest::Client,
7-
pub fav_lists: FavoriteList,
10+
pub(crate) client: reqwest::Client,
11+
pub(crate) fav_lists: FavoriteList,
812
}
913

1014
impl MyProfile {
11-
pub async fn create_fav_list(self, list_name: &str) -> Result<MyProfile, Errors> {
15+
pub async fn create_fav_list(&self, list_name: &str) -> Result<&MyProfile, Errors> {
1216
let query = json!({ "name": list_name });
1317

1418
let query = serde_json::to_string(&query)?;
@@ -17,26 +21,26 @@ impl MyProfile {
1721
.get("https://leetcode.com/list/api/")
1822
.body(query)
1923
.send()
20-
.await?;// let info = Self::s
24+
.await?;
2125
Ok(self)
2226
}
23-
pub fn show_fav_lists(self) -> FavoriteList {
24-
self.fav_lists
27+
pub fn show_lists(&self) -> FavoriteList {
28+
self.fav_lists.clone()
2529
}
2630

2731
pub async fn rename_fav_list(
28-
self,
32+
&self,
2933
prev_list_name: &str,
3034
new_list_name: &str,
31-
) -> Result<MyProfile, Errors> {
35+
) -> Result<&MyProfile, Errors> {
3236
let id_hash = if let Some(id) = Self::get_id_hash(&self, prev_list_name).await {
3337
id
3438
} else {
3539
return Err(Errors::ApiError("Provided name doesn't found".into()));
3640
};
3741
let query = json!({
38-
"favorite_id_hash": id_hash,
39-
"is_public_favorite": false,
42+
"favorite_id_hash": id_hash.0,
43+
"is_public_favorite": id_hash.1,
4044
"name": new_list_name
4145
});
4246

@@ -49,7 +53,32 @@ impl MyProfile {
4953
.await?;
5054
Ok(self)
5155
}
52-
pub async fn set_accessibility(self, list_name: &str) -> Result<MyProfile, Errors> {
56+
57+
pub async fn set_public(&self, list_name: &str) -> Result<&MyProfile, Errors> {
58+
let id_hash = if let Some(id) = Self::get_id_hash(&self, list_name).await {
59+
id
60+
} else {
61+
return Err(Errors::ApiError(
62+
"Provided name doesn't found in lists".into(),
63+
));
64+
};
65+
let query = json!({
66+
"favorite_id_hash": id_hash.0,
67+
"is_public_favorite": true,
68+
"name": list_name
69+
});
70+
71+
let query = serde_json::to_string(&query)?;
72+
73+
self.client
74+
.put("https://leetcode.com/list/api/")
75+
.body(query)
76+
.send()
77+
.await?;
78+
Ok(self)
79+
}
80+
81+
pub async fn set_private(&self, list_name: &str) -> Result<&MyProfile, Errors> {
5382
let id_hash = if let Some(id) = Self::get_id_hash(&self, list_name).await {
5483
id
5584
} else {
@@ -58,7 +87,7 @@ impl MyProfile {
5887
));
5988
};
6089
let query = json!({
61-
"favorite_id_hash": id_hash,
90+
"favorite_id_hash": id_hash.0,
6291
"is_public_favorite": true,
6392
"name": list_name
6493
});
@@ -82,10 +111,10 @@ impl MyProfile {
82111
));
83112
};
84113

85-
Ok(format!("https://leetcode.com/list/{}", id_hash))
114+
Ok(format!("https://leetcode.com/list/{}", id_hash.0))
86115
}
87116

88-
pub async fn delete_list(self, list_name: &str) -> Result<MyProfile, Errors> {
117+
pub async fn delete_list(&self, list_name: &str) -> Result<&MyProfile, Errors> {
89118
let id_hash = if let Some(id) = Self::get_id_hash(&self, list_name).await {
90119
id
91120
} else {
@@ -95,21 +124,72 @@ impl MyProfile {
95124
};
96125

97126
self.client
98-
.delete(format!("https://leetcode.com/list/api/{}", id_hash))
127+
.delete(format!("https://leetcode.com/list/api/{}", id_hash.0))
99128
.send()
100129
.await?;
101130

102131
Ok(self)
103132
}
104133

105-
async fn get_id_hash(&self, list_name: &str) -> Option<String> {
134+
async fn get_id_hash(&self, list_name: &str) -> Option<(String,bool)> {
106135
for favourite in &self.fav_lists.data.favoritesLists.allFavorites {
107136
if favourite.name == list_name.to_string() {
108-
return Some(favourite.idHash.clone());
137+
return Some((favourite.idHash.clone(), favourite.isPublicFavorite.clone()));
109138
}
110139
}
111140
None
112141
}
142+
143+
pub async fn get_notifications(&self) -> Result<NotificationsData, Errors> {
144+
let operation_name = "fetchNotifications";
145+
let variables = json!({ "first": 10 });
146+
let query = r#"query fetchNotifications($first: Int!, $after: String) {
147+
notifications(first: $first, after: $after) {
148+
edges {
149+
node {
150+
id
151+
notificationId
152+
modifiedDate
153+
actioned
154+
notificationData {
155+
id
156+
content
157+
type
158+
metadata
159+
__typename
160+
}
161+
__typename
162+
}
163+
__typename
164+
}
165+
pageInfo {
166+
endCursor
167+
hasNextPage
168+
__typename
169+
}
170+
__typename
171+
}
172+
}"#;
173+
174+
let json_data = json!({
175+
"operationName": operation_name,
176+
"variables": variables,
177+
"query": query,
178+
});
179+
180+
let query = serde_json::to_string(&json_data)?;
181+
182+
let problem_info = self
183+
.client
184+
.post("https://leetcode.com/graphql/")
185+
.body(query)
186+
.send()
187+
.await?
188+
.text()
189+
.await?;
190+
191+
Ok(serde_json::from_str::<NotificationsData>(&problem_info)?)
192+
}
113193
}
114194

115195
pub struct UserProfile {

‎src/resources/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pub mod cookie;
22
pub mod descr;
33
pub mod fav_list;
4+
pub mod notification;
45
pub mod problemfulldata;
56
pub mod subm_send;
67
pub mod subm_show;

‎src/resources/notification.rs‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use serde::Deserialize;
2+
3+
#[derive(Debug, Deserialize)]
4+
pub struct NotificationsData {
5+
pub data: Notifications,
6+
}
7+
8+
#[derive(Debug, Deserialize)]
9+
pub struct Notifications {
10+
pub notifications: NotificationConnection,
11+
}
12+
13+
#[allow(non_snake_case)]
14+
#[derive(Debug, Deserialize)]
15+
pub struct NotificationConnection {
16+
pub edges: Vec<NotificationEdge>,
17+
pub pageInfo: PageInfo,
18+
pub __typename: String,
19+
}
20+
21+
#[derive(Debug, Deserialize)]
22+
pub struct NotificationEdge {
23+
pub node: NotificationNode,
24+
pub __typename: String,
25+
}
26+
27+
#[allow(non_snake_case)]
28+
#[derive(Debug, Deserialize)]
29+
pub struct NotificationNode {
30+
pub id: String,
31+
pub notificationId: i32,
32+
pub modifiedDate: i64,
33+
pub actioned: bool,
34+
pub notificationData: NotificationDataNode,
35+
pub __typename: String,
36+
}
37+
38+
#[derive(Debug, Deserialize)]
39+
pub struct NotificationDataNode {
40+
pub id: String,
41+
pub content: String,
42+
#[serde(rename = "type")]
43+
pub type_: String,
44+
pub metadata: String,
45+
pub __typename: String,
46+
}
47+
48+
#[allow(non_snake_case)]
49+
#[derive(Debug, Deserialize)]
50+
pub struct PageInfo {
51+
pub endCursor: String,
52+
pub hasNextPage: bool,
53+
pub __typename: String,
54+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /