Skip to content

Commit f123b97

Browse files
committed
添加Github API;缓存添加策略
1 parent 0ff7de8 commit f123b97

File tree

8 files changed

+529
-42
lines changed

8 files changed

+529
-42
lines changed

baidu/oauth.go api_baidu/oauth.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package baidu
1+
package api_baidu
22

33
import "github.com/team-ide/go-tool/util"
44

@@ -11,7 +11,7 @@ type GetAccessTokenResponse struct {
1111

1212
func GetAccessToken(apiKey string, secretKey string) (res *GetAccessTokenResponse, err error) {
1313
apiUrl := GetAccessTokenUrl + "?grant_type=client_credentials&client_id=" + apiKey + "&client_secret=" + secretKey
14-
res, err = util.GetJson(apiUrl, &GetAccessTokenResponse{})
14+
res, _, err = util.GetJson(apiUrl, &GetAccessTokenResponse{})
1515
if err != nil {
1616
return
1717
}

baidu/ocr.go api_baidu/ocr.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package baidu
1+
package api_baidu
22

33
import (
44
"bytes"

api_github/gist.go

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package api_github
2+
3+
import (
4+
"fmt"
5+
"github.com/team-ide/go-tool/util"
6+
"net/http"
7+
"time"
8+
)
9+
10+
var GetGistsUrl = "https://api.github.com/gists"
11+
12+
type GetGistsResponse []*Gist
13+
14+
type Gist struct {
15+
Url string `json:"url"`
16+
ForksUrl string `json:"forks_url"`
17+
CommitsUrl string `json:"commits_url"`
18+
Id string `json:"id"`
19+
NodeId string `json:"node_id"`
20+
GitPullUrl string `json:"git_pull_url"`
21+
GitPushUrl string `json:"git_push_url"`
22+
HtmlUrl string `json:"html_url"`
23+
Files map[string]*GistFile `json:"files"`
24+
Public bool `json:"public"`
25+
CreatedAt time.Time `json:"created_at"`
26+
UpdatedAt time.Time `json:"updated_at"`
27+
Description string `json:"description"`
28+
Comments int `json:"comments"`
29+
CommentsUrl string `json:"comments_url"`
30+
Truncated bool `json:"truncated"`
31+
}
32+
33+
type GistFile struct {
34+
Filename string `json:"filename"`
35+
Type string `json:"type"`
36+
Language string `json:"language"`
37+
RawUrl string `json:"raw_url"`
38+
Size int64 `json:"size"`
39+
}
40+
41+
type GetGistsRequest struct {
42+
Since string `json:"since"` // 仅显示在给定时间后最后更新的结果。这是ISO 8601格式的时间戳:YYYY-MM-DDTHH:MM:SSZ
43+
PerPage int `json:"per_page"` // 每页数量 默认 30
44+
Page int `json:"page"` // 页码 默认 1
45+
}
46+
47+
func GetGists(accessToken string, request *GetGistsRequest) (res *GetGistsResponse, err error) {
48+
apiUrl := GetGistsUrl
49+
if request != nil {
50+
apiUrl = fmt.Sprintf("%s?since=%s&per_page=%d&page=%d", apiUrl, request.Since, request.PerPage, request.Page)
51+
}
52+
header := http.Header{
53+
"Accept": []string{"application/vnd.github+json"},
54+
"Authorization": []string{"Bearer " + accessToken},
55+
"X-GitHub-Api-Version": []string{"2022-11-28"},
56+
}
57+
res, _, err = util.GetJsonHeader(apiUrl, header, &GetGistsResponse{})
58+
if err != nil {
59+
return
60+
}
61+
return
62+
}
63+
64+
var CreateGistUrl = "https://api.github.com/gists"
65+
66+
type CreateGistResponse Gist
67+
68+
type CreateGistRequest struct {
69+
Description string `json:"description"`
70+
Files map[string]*CreateGistFile `json:"files"`
71+
Public bool `json:"public"`
72+
}
73+
74+
type CreateGistFile struct {
75+
Content string `json:"content"`
76+
}
77+
78+
func CreateGist(accessToken string, request *CreateGistRequest) (res *CreateGistResponse, err error) {
79+
apiUrl := CreateGistUrl
80+
header := http.Header{
81+
"Accept": []string{"application/vnd.github+json"},
82+
"Authorization": []string{"Bearer " + accessToken},
83+
"X-GitHub-Api-Version": []string{"2022-11-28"},
84+
}
85+
res, _, err = util.PostJsonHeader(apiUrl, header, request, &CreateGistResponse{})
86+
if err != nil {
87+
return
88+
}
89+
return
90+
}

api_github/oauth.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package api_github
2+
3+
import (
4+
"github.com/team-ide/go-tool/util"
5+
"net/http"
6+
"net/url"
7+
)
8+
9+
var GetAccessTokenUrl = "https://github.com/login/oauth/access_token"
10+
11+
type GetAccessTokenResponse struct {
12+
AccessToken string `json:"access_token"`
13+
ExpiresIn int `json:"expires_in"`
14+
RefreshToken string `json:"refresh_token"`
15+
RefreshTokenExpiresIn int `json:"refresh_token_expires_in"`
16+
Scope string `json:"scope"`
17+
TokenType string `json:"token_type"`
18+
Error string `json:"error"`
19+
ErrorDescription string `json:"error_description"`
20+
}
21+
22+
func GetAccessToken(clientId string, clientSecret string, code string) (res *GetAccessTokenResponse, err error) {
23+
apiUrl := GetAccessTokenUrl
24+
data := url.Values{}
25+
data.Set("client_id", clientId)
26+
data.Set("client_secret", clientSecret)
27+
data.Set("code", code)
28+
header := http.Header{}
29+
header.Set("Accept", "application/json")
30+
res, _, err = util.PostFormHeader(apiUrl, header, data, &GetAccessTokenResponse{})
31+
if err != nil {
32+
return
33+
}
34+
return
35+
}

api_github/user.go

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package api_github
2+
3+
import (
4+
"github.com/team-ide/go-tool/util"
5+
"net/http"
6+
)
7+
8+
var GetUserUrl = "https://api.github.com/user"
9+
10+
// GetUserResponse
11+
/**
12+
{
13+
"login": "toccata",
14+
"id": 1,
15+
"node_id": "MDQ6VXNlcjE=",
16+
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
17+
"gravatar_id": "",
18+
"url": "https://api.github.com/users/octocat",
19+
"html_url": "https://github.com/octocat",
20+
"followers_url": "https://api.github.com/users/octocat/followers",
21+
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
22+
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
23+
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
24+
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
25+
"organizations_url": "https://api.github.com/users/octocat/orgs",
26+
"repos_url": "https://api.github.com/users/octocat/repos",
27+
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
28+
"received_events_url": "https://api.github.com/users/octocat/received_events",
29+
"type": "User",
30+
"site_admin": false,
31+
"name": "monalisa octocat",
32+
"company": "GitHub",
33+
"blog": "https://github.com/blog",
34+
"location": "San Francisco",
35+
"email": "[email protected]",
36+
"hireable": false,
37+
"bio": "There once was...",
38+
"twitter_username": "monatheoctocat",
39+
"public_repos": 2,
40+
"public_gists": 1,
41+
"followers": 20,
42+
"following": 0,
43+
"created_at": "2008-01-14T04:33:35Z",
44+
"updated_at": "2008-01-14T04:33:35Z",
45+
"private_gists": 81,
46+
"total_private_repos": 100,
47+
"owned_private_repos": 100,
48+
"disk_usage": 10000,
49+
"collaborators": 8,
50+
"two_factor_authentication": true,
51+
"plan": {
52+
"name": "Medium",
53+
"space": 400,
54+
"private_repos": 20,
55+
"collaborators": 0
56+
}
57+
}
58+
*/
59+
type GetUserResponse struct {
60+
Login string `json:"login"`
61+
Id int `json:"id"`
62+
Name string `json:"name"`
63+
AvatarUrl string `json:"avatar_url"`
64+
Email string `json:"email"`
65+
HtmlUrl string `json:"html_url"`
66+
Blob string `json:"blob"`
67+
TwitterUsername string `json:"twitter_username"`
68+
Company string `json:"company"`
69+
Location string `json:"location"`
70+
Hireable bool `json:"hireable"`
71+
Bil string `json:"bil"`
72+
}
73+
74+
func GetUser(accessToken string) (res *GetUserResponse, err error) {
75+
apiUrl := GetUserUrl
76+
header := http.Header{
77+
"Accept": []string{"application/vnd.github+json"},
78+
"Authorization": []string{"Bearer " + accessToken},
79+
"X-GitHub-Api-Version": []string{"2022-11-28"},
80+
}
81+
res, _, err = util.GetJsonHeader(apiUrl, header, &GetUserResponse{})
82+
if err != nil {
83+
return
84+
}
85+
return
86+
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"version": "1.2.22"
2+
"version": "1.2.23"
33
}

0 commit comments

Comments
 (0)