-
Notifications
You must be signed in to change notification settings - Fork 3
WIP - Pulling a JSON object from a given Starred repo URL #42
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -68,6 +68,7 @@ impl ClientBuilder { | |||||
| struct Star { | ||||||
| starred_at: DateTime<Utc>, | ||||||
| repo: Repository, | ||||||
| //lang: Languages, | ||||||
| } | ||||||
|
|
||||||
| impl fmt::Display for Star { | ||||||
|
|
@@ -83,6 +84,7 @@ struct Repository { | |||||
| full_name: String, | ||||||
| description: Option<String>, | ||||||
| stargazers_count: i32, | ||||||
| languages_url: Option<String>, | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will we need to hand on to this URL for the duration of the program and/or output it at some point? Or is it only a temporary variable so we can make a request to the GitHub API? (see more comments below)
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure this needs to be an |
||||||
| } | ||||||
|
|
||||||
| impl fmt::Display for Repository { | ||||||
|
|
@@ -93,11 +95,33 @@ impl fmt::Display for Repository { | |||||
| write!(f, " - {}", description)?; | ||||||
| } | ||||||
|
|
||||||
| if let Some(ref languages_url) = self.languages_url { | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||||||
| write!(f, " - {}", languages_url)?; | ||||||
| } | ||||||
|
|
||||||
| Ok(()) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| pub fn collect_stars(config: Config) -> Result<(), Box<dyn error::Error>> { | ||||||
| #[derive(Debug, Deserialize)] | ||||||
| struct Languages { | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get what you're going for here, but unfortunately this isn't how serde works. With this struct definition, serde expects the json response to contain one or more objects of the following structure: {
"key": "string data",
"value": "string data"
}whereas the data we actually get back from the languages API endpoint is: {
"Python": 597727,
"JavaScript": 38865,
"CSS": 1229,
"Batchfile": 838,
"Shell": 713,
"HTML": 478
}Now, because we don't know what languages a given repo will contain, and it would be a lot of work and not very flexible to write a struct that has keys for every language GitHub understands, I would really recommend looking into how serde works with untyped json values; the |
||||||
| key: Option<String>, | ||||||
| value: Option<String>, | ||||||
| } | ||||||
|
|
||||||
| impl fmt::Display for Languages { | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You probably don't need to implement your own println!("{:?}", lang);The only reason |
||||||
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||||||
| if let Some(ref key) = self.key { | ||||||
| write!(f, " - {}", key)?; | ||||||
| } | ||||||
| if let Some(ref value) = self.value { | ||||||
| write!(f, " - {}", value)?; | ||||||
| } | ||||||
| Ok(()) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| pub fn collect_stars(config: Config) -> Result<(), Box<dyn error::Error>> { //Where the json extraction happens | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
nit: extraneous comment |
||||||
| let mut builder = ClientBuilder::new(); | ||||||
|
|
||||||
| if let Some(ref token) = config.token { | ||||||
|
|
@@ -120,6 +144,20 @@ pub fn collect_stars(config: Config) -> Result<(), Box<dyn error::Error>> { | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| let mut langs: Vec<Languages> = Vec::new(); | ||||||
|
|
||||||
| for star in stars.iter(){ | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you've overcomplicated this part. To get the languages for all the starred repositories, we want to:
The stars vector is already mutable, so the |
||||||
| let ref mut next = star.repo.languages_url; | ||||||
| if let Some(ref link) = next{ | ||||||
| let mut res = client.get(link).send()?; | ||||||
| next = &mut extract_link_next(res.headers()); | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it makes sense to try and extract The which the languages API endpoint does not have. |
||||||
|
|
||||||
| let mut s: Vec<Languages> = res.json()?; | ||||||
| langs.append(&mut s); | ||||||
| } | ||||||
| println!("{:?}", langs) | ||||||
| } | ||||||
|
|
||||||
| for star in stars.iter() { | ||||||
| println!("{}", star); | ||||||
| } | ||||||
|
|
||||||
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.
If we need this field on
struct Star, but we don't want serde to look at the JSON response for a key of "lang"; we can tell serde to ignore a field using the skip directive so it won't be seen during (de)serialization.I think this field should actually go on the
Repositorytype since it's data about the repository and not the star itself.Note that repositories may also have a(n optionally
null)"language"key, which denotes the primary language for the repository.