-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
135 lines (117 loc) · 3.14 KB
/
types.go
File metadata and controls
135 lines (117 loc) · 3.14 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package gitcode
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
type Timestamp struct {
time.Time
}
func (t *Timestamp) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
tt, err := time.Parse(time.RFC3339, s)
if err != nil {
return err
}
t.Time = tt
return nil
}
type Error struct {
Message string `json:"message"`
Errors []struct {
Resource string `json:"resource"`
Field string `json:"field"`
Code string `json:"code"`
} `json:"errors"`
}
func (e *Error) Error() string {
return e.Message
}
type SearchOptions struct {
ListOptions
Query string `json:"q"`
Order string `json:"order,omitempty"`
}
type SearchResult struct {
TotalCount int `json:"total_count"`
Items []*Repository `json:"items"`
}
type Notification struct {
ID string `json:"id"`
Reason string `json:"reason"`
Unread bool `json:"unread"`
Subject struct {
Title string `json:"title"`
URL string `json:"url"`
Type string `json:"type"`
} `json:"subject"`
CreatedAt time.Time `json:"created_at"`
}
type Star struct {
StarredAt time.Time `json:"starred_at"`
}
type Member struct {
ID int64 `json:"id"`
Login string `json:"login"`
Role string `json:"role"`
}
type Organization struct {
ID int64 `json:"id"`
Login string `json:"login"`
Name string `json:"name"`
Description string `json:"description"`
AvatarURL string `json:"avatar_url"`
}
func (c *Client) ListOrganizations(ctx context.Context) ([]*Organization, error) {
var orgs []*Organization
err := c.doRequest(ctx, http.MethodGet, "/user/orgs", nil, &orgs)
if err != nil {
return nil, err
}
return orgs, nil
}
func (c *Client) GetOrganization(ctx context.Context, org string) (*Organization, error) {
var o Organization
err := c.doRequest(ctx, http.MethodGet, fmt.Sprintf("/orgs/%s", org), nil, &o)
if err != nil {
return nil, err
}
return &o, nil
}
func (c *Client) ListOrganizationMembers(ctx context.Context, org string) ([]*Member, error) {
var members []*Member
err := c.doRequest(ctx, http.MethodGet, fmt.Sprintf("/orgs/%s/members", org), nil, &members)
if err != nil {
return nil, err
}
return members, nil
}
func (c *Client) ListNotifications(ctx context.Context) ([]*Notification, error) {
var notifications []*Notification
err := c.doRequest(ctx, http.MethodGet, "/notifications", nil, ¬ifications)
if err != nil {
return nil, err
}
return notifications, nil
}
func (c *Client) StarRepository(ctx context.Context, owner, repo string) error {
return c.doRequest(ctx, http.MethodPut, fmt.Sprintf("/user/starred/%s/%s", owner, repo), nil, nil)
}
func (c *Client) UnstarRepository(ctx context.Context, owner, repo string) error {
return c.doRequest(ctx, http.MethodDelete, fmt.Sprintf("/user/starred/%s/%s", owner, repo), nil, nil)
}
func (c *Client) IsRepositoryStarred(ctx context.Context, owner, repo string) (bool, error) {
_, err := c.doRawRequest(ctx, http.MethodGet, fmt.Sprintf("/user/starred/%s/%s", owner, repo))
if err != nil {
if err.Error() == "404 Not Found" {
return false, nil
}
return false, err
}
return true, nil
}