1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package gitlab
18
19 import (
20 "encoding/json"
21 "fmt"
22 "io"
23 "net/http"
24
25 retryablehttp "github.com/hashicorp/go-retryablehttp"
26 )
27
28
29
30
31
32 type TopicsService struct {
33 client *Client
34 }
35
36
37
38
39 type Topic struct {
40 ID int `json:"id"`
41 Name string `json:"name"`
42 Title string `json:"title"`
43 Description string `json:"description"`
44 TotalProjectsCount uint64 `json:"total_projects_count"`
45 AvatarURL string `json:"avatar_url"`
46 }
47
48 func (t Topic) String() string {
49 return Stringify(t)
50 }
51
52
53
54
55 type ListTopicsOptions struct {
56 ListOptions
57 Search *string `url:"search,omitempty" json:"search,omitempty"`
58 }
59
60
61
62
63
64 func (s *TopicsService) ListTopics(opt *ListTopicsOptions, options ...RequestOptionFunc) ([]*Topic, *Response, error) {
65 req, err := s.client.NewRequest(http.MethodGet, "topics", opt, options)
66 if err != nil {
67 return nil, nil, err
68 }
69
70 var t []*Topic
71 resp, err := s.client.Do(req, &t)
72 if err != nil {
73 return nil, resp, err
74 }
75
76 return t, resp, nil
77 }
78
79
80
81
82 func (s *TopicsService) GetTopic(topic int, options ...RequestOptionFunc) (*Topic, *Response, error) {
83 u := fmt.Sprintf("topics/%d", topic)
84
85 req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
86 if err != nil {
87 return nil, nil, err
88 }
89
90 t := new(Topic)
91 resp, err := s.client.Do(req, t)
92 if err != nil {
93 return nil, resp, err
94 }
95
96 return t, resp, nil
97 }
98
99
100
101
102
103 type CreateTopicOptions struct {
104 Name *string `url:"name,omitempty" json:"name,omitempty"`
105 Title *string `url:"title,omitempty" json:"title,omitempty"`
106 Description *string `url:"description,omitempty" json:"description,omitempty"`
107 Avatar *TopicAvatar `url:"-" json:"-"`
108 }
109
110
111 type TopicAvatar struct {
112 Filename string
113 Image io.Reader
114 }
115
116
117 func (a *TopicAvatar) MarshalJSON() ([]byte, error) {
118 if a.Filename == "" && a.Image == nil {
119 return []byte(`""`), nil
120 }
121 type alias TopicAvatar
122 return json.Marshal((*alias)(a))
123 }
124
125
126
127
128
129 func (s *TopicsService) CreateTopic(opt *CreateTopicOptions, options ...RequestOptionFunc) (*Topic, *Response, error) {
130 var err error
131 var req *retryablehttp.Request
132
133 if opt.Avatar == nil {
134 req, err = s.client.NewRequest(http.MethodPost, "topics", opt, options)
135 } else {
136 req, err = s.client.UploadRequest(
137 http.MethodPost,
138 "topics",
139 opt.Avatar.Image,
140 opt.Avatar.Filename,
141 UploadAvatar,
142 opt,
143 options,
144 )
145 }
146 if err != nil {
147 return nil, nil, err
148 }
149
150 t := new(Topic)
151 resp, err := s.client.Do(req, t)
152 if err != nil {
153 return nil, resp, err
154 }
155
156 return t, resp, nil
157 }
158
159
160
161
162
163 type UpdateTopicOptions struct {
164 Name *string `url:"name,omitempty" json:"name,omitempty"`
165 Title *string `url:"title,omitempty" json:"title,omitempty"`
166 Description *string `url:"description,omitempty" json:"description,omitempty"`
167 Avatar *TopicAvatar `url:"-" json:"avatar,omitempty"`
168 }
169
170
171
172
173
174
175
176
177 func (s *TopicsService) UpdateTopic(topic int, opt *UpdateTopicOptions, options ...RequestOptionFunc) (*Topic, *Response, error) {
178 u := fmt.Sprintf("topics/%d", topic)
179
180 var err error
181 var req *retryablehttp.Request
182
183 if opt.Avatar == nil || (opt.Avatar.Filename == "" && opt.Avatar.Image == nil) {
184 req, err = s.client.NewRequest(http.MethodPut, u, opt, options)
185 } else {
186 req, err = s.client.UploadRequest(
187 http.MethodPut,
188 u,
189 opt.Avatar.Image,
190 opt.Avatar.Filename,
191 UploadAvatar,
192 opt,
193 options,
194 )
195 }
196 if err != nil {
197 return nil, nil, err
198 }
199
200 t := new(Topic)
201 resp, err := s.client.Do(req, t)
202 if err != nil {
203 return nil, resp, err
204 }
205
206 return t, resp, nil
207 }
208
209
210
211
212
213 func (s *TopicsService) DeleteTopic(topic int, options ...RequestOptionFunc) (*Response, error) {
214 u := fmt.Sprintf("topics/%d", topic)
215
216 req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
217 if err != nil {
218 return nil, err
219 }
220
221 return s.client.Do(req, nil)
222 }
223
View as plain text