-
Notifications
You must be signed in to change notification settings - Fork 1
feat(client): add session storage for github stars #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(client): add session storage for github stars #19
Conversation
crates/client/Cargo.toml
Outdated
| serde_json = { workspace = true } | ||
|
|
||
| proto = { workspace = true } | ||
| exitfailure = "0.5.1" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can use anyhow to cover this, please checkout out the suggestions for crates/client/src/api.rs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| exitfailure = "0.5.1" |
crates/client/src/api.rs
Outdated
| @@ -0,0 +1,31 @@ | |||
| use exitfailure::ExitFailure; | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| use exitfailure::ExitFailure; | |
| use anyhow::Result; |
crates/client/src/api.rs
Outdated
| } | ||
|
|
||
| impl Stars { | ||
| pub async fn get_amount() -> Result<Self, ExitFailure> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| pub async fn get_amount() -> Result<Self, ExitFailure> { | |
| pub async fn get_amount() -> Result<Self> { |
crates/client/src/api.rs
Outdated
| let url = Url::parse(&*url)?; | ||
| let res = reqwest::get(url) | ||
| .await? | ||
| .json::<Vec<Rustacean>>() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here as generic parameter we should pass the type that deserialzes to the response body.
Which looks like:
[
{
"login": "octocat",
"id": 1,
"node_id": "MDQ6VXNlcjE=",
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "",
"url": "https://api.github.com/users/octocat",
"html_url": "https://github.com/octocat",
"followers_url": "https://api.github.com/users/octocat/followers",
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
"organizations_url": "https://api.github.com/users/octocat/orgs",
"repos_url": "https://api.github.com/users/octocat/repos",
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/octocat/received_events",
"type": "User",
"site_admin": false
}
]As found in: https://docs.github.com/en/rest/activity/starring?apiVersion=2022-11-28
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can deserialize into:
pub struct Stargazer {
pub login: String,
pub id: u64,
pub node_id: String,
pub avatar_url: String,
pub gravatar_id: String,
pub url: String,
pub html_url: String,
pub followers_url: String,
pub following_url: String,
pub gists_url: String,
pub starred_url: String,
pub subscriptions_url: String,
pub organizations_url: String,
pub repos_url: String,
pub events_url: String,
pub received_events_url: String,
pub user_type: String,
pub user_view_type: String,
pub site_admin: bool,
}Do you agree?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGTM! It would be nice to validate if we can have only relevant items in the final struct as well, so we optimize memory usage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I think we could use https://api.github.com/repos/rustacean-sh/rustacean.sh and pick the stargazers_count field from there?
That way we avoid paginating and counting manually.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect!
crates/client/src/api.rs
Outdated
|
|
||
| impl Stars { | ||
| pub async fn get_amount() -> Result<Self> { | ||
| let url = format!("https://api.github.com/repos/rustacean-sh/rustacean.sh"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that we are not interpolating strings we can use a string slice directly.
| let url = format!("https://api.github.com/repos/rustacean-sh/rustacean.sh"); | |
| let url = "https://api.github.com/repos/rustacean-sh/rustacean.sh"; |
crates/client/src/api.rs
Outdated
| let url = format!("https://api.github.com/repos/rustacean-sh/rustacean.sh"); | ||
| let url = Url::parse(&*url)?; | ||
| let res = reqwest::get(url).await?.text().await?; | ||
| let v: Value = serde_json::from_str(res.as_str()).unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest we bubble errors up
| let v: Value = serde_json::from_str(res.as_str()).unwrap(); | |
| let v: Value = serde_json::from_str(res.as_str())?; |
crates/client/src/api.rs
Outdated
| let url = format!("https://api.github.com/repos/rustacean-sh/rustacean.sh"); | ||
| let url = Url::parse(&*url)?; | ||
| let res = reqwest::get(url).await?.text().await?; | ||
| let v: Value = serde_json::from_str(res.as_str()).unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also recommend using the Stars struct directly and defer deserialization to serde here.
| let v: Value = serde_json::from_str(res.as_str()).unwrap(); | |
| let v: Stars = serde_json::from_str(res.as_str()).unwrap(); |
No description provided.