-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-api.go
More file actions
49 lines (40 loc) · 1.39 KB
/
github-api.go
File metadata and controls
49 lines (40 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
)
// setRequest creates and sends the request.
func setRequest(uri string, token string) []byte {
// Set up the request.
client := &http.Client{}
request, err := http.NewRequest("GET", uri, nil)
check(err)
request.Header.Set("Authorization", token)
// Get the repos json data.
response, _ := client.Do(request)
jsonData, _ := ioutil.ReadAll(response.Body)
return jsonData
}
// getJsonResponse gets the full json object returned from the Request.
func getJsonResponse(uri string, token string, fixer string) []interface{} {
// Set up the request.
jsonData := setRequest(uri, token)
jsonText := string(jsonData)
// Github returns empty stats data for the first uncached request, so try again.
if jsonText == "{}" {
jsonData = setRequest(uri, token)
jsonText = string(jsonData)
}
// Fix the bad json from GitHub.
jsonText = `{ "` + fixer + `": ` + jsonText + ` }`
// Unmarshal the json.
var jsonDataFixed map[string]interface{}
check(json.Unmarshal([]byte(jsonText), &jsonDataFixed))
// Use type assertion to get the repo list into the correct type.
dataList := jsonDataFixed[fixer]
//fmt.Printf("dataList: %v", dataList)
// try catch below for this error caused by stats not being generated yet:
// panic: interface conversion: interface {} is map[string]interface {}, not []interface {}
return dataList.([]interface{})
}