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

Commit af52d51

Browse files
committed
optimize request speed
1 parent bce84e3 commit af52d51

1 file changed

Lines changed: 53 additions & 70 deletions

File tree

api.go

Lines changed: 53 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package main
22

33
import (
4+
"errors"
45
"fmt"
56
"github.com/kataras/iris/v12"
67
)
78

89
func GetUser(username string) (data map[string]interface{}, err error) {
910
err = Get(fmt.Sprintf("users/%s", username), &data)
11+
if err == nil && data["message"] == "Not Found" {
12+
return nil, errors.New("user not found")
13+
}
1014
return data, err
1115
}
1216

@@ -17,6 +21,9 @@ func GetRepos(username string) (data []interface{}, err error) {
1721

1822
func GetRepo(username string, repo string) (data map[string]interface{}, err error) {
1923
err = Get(fmt.Sprintf("repos/%s/%s", username, repo), &data)
24+
if err == nil && data["message"] == "Not Found" {
25+
return nil, errors.New("repo not found")
26+
}
2027
return data, err
2128
}
2229

@@ -32,24 +39,6 @@ func getLicense(license interface{}) string {
3239
return "Empty"
3340
}
3441

35-
func GetUserExist(username string) bool {
36-
res, err := GetUser(username)
37-
if err != nil {
38-
return false
39-
}
40-
val, ok := res["message"]
41-
return !(ok && val == "Not Found")
42-
}
43-
44-
func GetRepoExist(username string, repo string) bool {
45-
res, err := GetRepo(username, repo)
46-
if err != nil {
47-
return false
48-
}
49-
val, ok := res["message"]
50-
return !(ok && val == "Not Found")
51-
}
52-
5342
func iterRepos(username string) (data []interface{}, err error) {
5443
repos, err := GetRepos(username)
5544
if err != nil {
@@ -100,61 +89,55 @@ type AnalysisData struct {
10089
}
10190

10291
func AnalysisUser(username string) AnalysisData {
103-
if GetUserExist(username) {
104-
res, err := GetUser(username)
105-
if err != nil {
106-
return AnalysisData{nil, err.Error(), iris.StatusInternalServerError}
107-
}
108-
repos, err := iterRepos(username)
109-
if err != nil {
110-
return AnalysisData{nil, err.Error(), iris.StatusInternalServerError}
111-
}
112-
languages, err := CollectLanguages(username, repos)
113-
if err != nil {
114-
return AnalysisData{nil, err.Error(), iris.StatusInternalServerError}
115-
}
116-
return AnalysisData{
117-
iris.Map{
118-
"username": username,
119-
"location": res["location"],
120-
"org": res["type"] != "User",
121-
"repos": res["public_repos"],
122-
"follower": ScaleConvert(res["followers"].(float64), true),
123-
"stars": ScaleConvert(Sum(repos, "stargazers_count"), true),
124-
"forks": ScaleConvert(Sum(repos, "forks_count"), true),
125-
"issues": ScaleConvert(Sum(repos, "open_issues_count"), true),
126-
"watchers": ScaleConvert(Sum(repos, "watchers_count"), true),
127-
"languages": CountLanguages(languages),
128-
}, "", iris.StatusOK,
129-
}
92+
res, err := GetUser(username)
93+
if err != nil {
94+
return AnalysisData{nil, err.Error(), iris.StatusNotFound}
95+
}
96+
repos, err := iterRepos(username)
97+
if err != nil {
98+
return AnalysisData{nil, err.Error(), iris.StatusInternalServerError}
99+
}
100+
languages, err := CollectLanguages(username, repos)
101+
if err != nil {
102+
return AnalysisData{nil, err.Error(), iris.StatusInternalServerError}
103+
}
104+
return AnalysisData{
105+
iris.Map{
106+
"username": username,
107+
"location": res["location"],
108+
"org": res["type"] != "User",
109+
"repos": res["public_repos"],
110+
"follower": ScaleConvert(res["followers"].(float64), true),
111+
"stars": ScaleConvert(Sum(repos, "stargazers_count"), true),
112+
"forks": ScaleConvert(Sum(repos, "forks_count"), true),
113+
"issues": ScaleConvert(Sum(repos, "open_issues_count"), true),
114+
"watchers": ScaleConvert(Sum(repos, "watchers_count"), true),
115+
"languages": CountLanguages(languages),
116+
}, "", iris.StatusOK,
130117
}
131-
return AnalysisData{nil, "user not found", iris.StatusNotFound}
132118
}
133119

134120
func AnalysisRepo(username string, repo string) AnalysisData {
135-
if GetRepoExist(username, repo) {
136-
res, err := GetRepo(username, repo)
137-
if err != nil {
138-
return AnalysisData{nil, err.Error(), iris.StatusInternalServerError}
139-
}
140-
languages, err := GetLanguages(username, repo)
141-
if err != nil {
142-
return AnalysisData{nil, err.Error(), iris.StatusInternalServerError}
143-
}
144-
return AnalysisData{
145-
iris.Map{
146-
"username": username,
147-
"repo": repo,
148-
"size": SizeConvert(res["size"].(float64), 1),
149-
"stars": ScaleConvert(res["stargazers_count"].(float64), true),
150-
"forks": ScaleConvert(res["forks_count"].(float64), true),
151-
"watchers": ScaleConvert(res["watchers_count"].(float64), true),
152-
"issues": ScaleConvert(res["open_issues_count"].(float64), false),
153-
"color": GetColor(res["language"]),
154-
"license": getLicense(res["license"]),
155-
"languages": CountLanguages(languages),
156-
}, "", iris.StatusOK,
157-
}
121+
res, err := GetRepo(username, repo)
122+
if err != nil {
123+
return AnalysisData{nil, err.Error(), iris.StatusNotFound}
124+
}
125+
languages, err := GetLanguages(username, repo)
126+
if err != nil {
127+
return AnalysisData{nil, err.Error(), iris.StatusInternalServerError}
128+
}
129+
return AnalysisData{
130+
iris.Map{
131+
"username": username,
132+
"repo": repo,
133+
"size": SizeConvert(res["size"].(float64), 1),
134+
"stars": ScaleConvert(res["stargazers_count"].(float64), true),
135+
"forks": ScaleConvert(res["forks_count"].(float64), true),
136+
"watchers": ScaleConvert(res["watchers_count"].(float64), true),
137+
"issues": ScaleConvert(res["open_issues_count"].(float64), false),
138+
"color": GetColor(res["language"]),
139+
"license": getLicense(res["license"]),
140+
"languages": CountLanguages(languages),
141+
}, "", iris.StatusOK,
158142
}
159-
return AnalysisData{nil, "repo not found", iris.StatusNotFound}
160143
}

0 commit comments

Comments
 (0)