Created
January 20, 2020 21:06
-
-
Save topherPedersen/162b25b4549d16f2bb39c5cf2c25801d to your computer and use it in GitHub Desktop.
Working with APIs and Parsing JSON Data in Golang (with RapidAPI & Hacker News)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// REFERENCE (Parsing JSON in Golang): https://www.sohamkamani.com/blog/2017/10/18/parsing-json-in-golang/ | |
// REFERENCE (Cast Int as String in Golang): https://yourbasic.org/golang/convert-int-to-string/ | |
package main | |
import ( | |
"fmt" | |
"net/http" | |
"io/ioutil" | |
"encoding/json" | |
"strconv" | |
) | |
type TopStories struct { | |
Story []int | |
} | |
type Story struct { | |
By string | |
Descendants int | |
Id int | |
Kids []int | |
Score int | |
Title string | |
Type string | |
URL string | |
} | |
func main() { | |
// --------------------------------------------------------------------- | |
// Hit the top stories endpoint for the Hacker News API | |
// via RapidAPI to get the story ids for the top stories | |
// today trending on Hacker News | |
// --------------------------------------------------------------------- | |
url := "https://community-hacker-news-v1.p.rapidapi.com/topstories.json?print=pretty" | |
// Create Request | |
request, _ := http.NewRequest("GET", url, nil) | |
request.Header.Add("x-rapidapi-host", "community-hacker-news-v1.p.rapidapi.com") | |
request.Header.Add("x-rapidapi-key", "YouR-SuPER-SWeeT-RaPiDaPi-KeY-GoeS-HeRe") | |
// Get Result, Result Body, and Result Body String | |
result, _ := http.DefaultClient.Do(request) | |
defer result.Body.Close() | |
resultBody, _ := ioutil.ReadAll(result.Body) | |
resultBodyStr := string(resultBody) | |
// Create an array to store our topStories, | |
// then append the story ids to our array | |
// using json.Unmarshall | |
var topStories []int | |
json.Unmarshal([]byte(resultBodyStr), &topStories) | |
// --------------------------------------------------------------------- | |
// --------------------------------------------------------------------- | |
// Next, Let's fetch some information about the top trending story on | |
// Hacker News. TODO: Fetch data for ALL the trending stories on HN | |
// --------------------------------------------------------------------- | |
var topStoryID string = strconv.Itoa(topStories[0]) | |
url = "https://community-hacker-news-v1.p.rapidapi.com/item/" + topStoryID + ".json?print=pretty" | |
request, _ = http.NewRequest("GET", url, nil) | |
request.Header.Add("x-rapidapi-host", "community-hacker-news-v1.p.rapidapi.com") | |
request.Header.Add("x-rapidapi-key", "YouR-SuPER-SWeeT-RaPiDaPi-KeY-GoeS-HeRe") | |
result, _ = http.DefaultClient.Do(request) | |
defer result.Body.Close() | |
resultBody, _ = ioutil.ReadAll(result.Body) | |
resultBodyStr = string(resultBody) | |
// --------------------------------------------------------------------- | |
// --------------------------------------------------------------------- | |
// Now, let's parse the top story... | |
// --------------------------------------------------------------------- | |
var topStory Story | |
json.Unmarshal([]byte(resultBodyStr), &topStory) | |
fmt.Println("Top Story...") | |
fmt.Printf("Title: %s\n", topStory.Title) | |
fmt.Printf("URL: %s\n", topStory.URL) | |
// --------------------------------------------------------------------- | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment