|
| 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 | +} |
0 commit comments