Skip to content
This repository was archived by the owner on May 11, 2025. It is now read-only.

Commit 463ba81

Browse files
committed
add api
1 parent 2887ed8 commit 463ba81

3 files changed

Lines changed: 51 additions & 32 deletions

File tree

api.go

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,19 @@
11
package main
22

3-
import (
4-
"encoding/json"
5-
"github.com/sirupsen/logrus"
6-
"io"
7-
"net/http"
8-
)
3+
import "fmt"
94

10-
func Get(uri string) (res map[string]interface{}, err error) {
11-
req, err := http.NewRequest("GET", "https://api.github.com/"+uri, nil)
12-
if err != nil {
13-
return nil, err
14-
}
15-
16-
req.Header.Set("Accept", "application/json")
17-
req.Header.Set("Authorization", "Bearer "+GetToken())
5+
func GetUser(username string) (res map[string]interface{}, err error) {
6+
return Get(fmt.Sprintf("users/%s", username))
7+
}
188

19-
client := &http.Client{}
20-
resp, err := client.Do(req)
21-
if err != nil {
22-
return nil, err
23-
}
9+
func GetRepos(username string) (res map[string]interface{}, err error) {
10+
return Get(fmt.Sprintf("users/%s/repos", username))
11+
}
2412

25-
defer func(Body io.ReadCloser) {
26-
err := Body.Close()
27-
if err != nil {
28-
logrus.Infoln()
29-
}
30-
}(resp.Body)
13+
func GetRepo(username string, repo string) (res map[string]interface{}, err error) {
14+
return Get(fmt.Sprintf("repos/%s/%s", username, repo))
15+
}
3116

32-
var data map[string]interface{}
33-
err = json.NewDecoder(resp.Body).Decode(&data)
34-
if err != nil {
35-
return nil, err
36-
}
37-
return data, nil
17+
func GetLanguages(username string, repo string) (res map[string]interface{}, err error) {
18+
return Get(fmt.Sprintf("repos/%s/%s/languages", username, repo))
3819
}

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"fmt"
45
"github.com/sirupsen/logrus"
56
)
67

@@ -10,4 +11,5 @@ func main() {
1011

1112
tokenList = GetTokenFromEnv()
1213
ValidateToken()
14+
fmt.Println(GetRepos("zmh-program"))
1315
}

utils.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package main
22

3-
import "fmt"
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"github.com/sirupsen/logrus"
7+
"io"
8+
"net/http"
9+
)
410

511
func Sum(array []int) int {
612
total := 0
@@ -40,3 +46,33 @@ func ScaleConvert(n float64, useSmallScale bool) string {
4046
}
4147
return fmt.Sprintf("%.1f%s", n, scaleUnits[idx])
4248
}
49+
50+
func Get(uri string) (res map[string]interface{}, err error) {
51+
req, err := http.NewRequest("GET", "https://api.github.com/"+uri, nil)
52+
if err != nil {
53+
return nil, err
54+
}
55+
56+
req.Header.Set("Accept", "application/json")
57+
req.Header.Set("Authorization", "Bearer "+GetToken())
58+
59+
client := &http.Client{}
60+
resp, err := client.Do(req)
61+
if err != nil {
62+
return nil, err
63+
}
64+
65+
defer func(Body io.ReadCloser) {
66+
err := Body.Close()
67+
if err != nil {
68+
logrus.Infoln()
69+
}
70+
}(resp.Body)
71+
72+
var data map[string]interface{}
73+
err = json.NewDecoder(resp.Body).Decode(&data)
74+
if err != nil {
75+
return nil, err
76+
}
77+
return data, nil
78+
}

0 commit comments

Comments
 (0)