Skip to content

Conversation

@luchosr
Copy link
Contributor

@luchosr luchosr commented Dec 17, 2024

No description provided.

serde_json = { workspace = true }

proto = { workspace = true }
exitfailure = "0.5.1"
Copy link
Contributor

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
exitfailure = "0.5.1"

@@ -0,0 +1,31 @@
use exitfailure::ExitFailure;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
use exitfailure::ExitFailure;
use anyhow::Result;

}

impl Stars {
pub async fn get_amount() -> Result<Self, ExitFailure> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub async fn get_amount() -> Result<Self, ExitFailure> {
pub async fn get_amount() -> Result<Self> {

let url = Url::parse(&*url)?;
let res = reqwest::get(url)
.await?
.json::<Vec<Rustacean>>()
Copy link
Contributor

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

Copy link
Contributor Author

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?

Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect!


impl Stars {
pub async fn get_amount() -> Result<Self> {
let url = format!("https://api.github.com/repos/rustacean-sh/rustacean.sh");
Copy link
Contributor

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.

Suggested change
let url = format!("https://api.github.com/repos/rustacean-sh/rustacean.sh");
let url = "https://api.github.com/repos/rustacean-sh/rustacean.sh";

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();
Copy link
Contributor

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

Suggested change
let v: Value = serde_json::from_str(res.as_str()).unwrap();
let v: Value = serde_json::from_str(res.as_str())?;

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();
Copy link
Contributor

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.

Suggested change
let v: Value = serde_json::from_str(res.as_str()).unwrap();
let v: Stars = serde_json::from_str(res.as_str()).unwrap();

@LeoBorai LeoBorai changed the title Fix/retrieve actual number of stars fix: retrieve actual number of stars Dec 28, 2024
@LeoBorai LeoBorai changed the title fix: retrieve actual number of stars feat(client): add session storage for github stars Dec 28, 2024
@LeoBorai LeoBorai merged commit dd7c429 into rustacean-sh:main Dec 28, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants