Skip to content

Commit

Permalink
fetch languages data from remote
Browse files Browse the repository at this point in the history
  • Loading branch information
TonnyL committed Nov 17, 2019
1 parent cea4c11 commit 3c81f0d
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli"
"io/ioutil"
"net/http"
"os"
"strconv"
Expand Down Expand Up @@ -85,7 +84,7 @@ func main() {
Aliases: []string{"l", "languages", "language"},
Usage: "List all the available language options",
Action: func(c *cli.Context) error {
langs, err := ReadLanguages()
langs, err := FetchLanguages()
if err != nil {
return err
}
Expand Down Expand Up @@ -138,27 +137,27 @@ func FetchTrendingDevelopers(language, since string) ([]Developer, error) {
return developers, nil
}

// Read popular languages and all languages.
func ReadLanguages() ([]Language, error) {
jsonFile, err := os.Open("./resources/languages.json")
if err != nil {
return nil, err
}

defer jsonFile.Close()
// cache to avoid too many requests.
var cachedLanguages = make([]Language, 0)

bytes, err := ioutil.ReadAll(jsonFile)
// Fetch all languages.
func FetchLanguages() ([]Language, error) {
if len(cachedLanguages) != 0 {
return cachedLanguages, nil
}
resp, err := http.Get("https://raw.githubusercontent.com/TonnyL/Wukong/master/resources/languages.json")
if err != nil {
return nil, err
}

languages := make([]Language, 0)
jsonErr := json.Unmarshal(bytes, &languages)
defer resp.Body.Close()

jsonErr := json.NewDecoder(resp.Body).Decode(&cachedLanguages)
if jsonErr != nil {
return nil, jsonErr
}

return languages, nil
return cachedLanguages, nil
}

func ShowTableOfRepositories(repos []Repository) {
Expand Down Expand Up @@ -202,7 +201,7 @@ func CheckParams(l, p string) error {
return errors.New("Unknown period value: " + p)
}

langs, err := ReadLanguages()
langs, err := FetchLanguages()
if err != nil {
return err
}
Expand Down

0 comments on commit 3c81f0d

Please sign in to comment.