forked from mattn/go-redmine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
news.go
48 lines (43 loc) · 1000 Bytes
/
news.go
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
package redmine
import (
"encoding/json"
"errors"
"strconv"
"strings"
)
type newsResult struct {
News []News `json:"news"`
}
type News struct {
Id int `json:"id"`
Project IdName `json:"project"`
Title string `json:"title"`
Summary string `json:"summary"`
Description string `json:"description"`
CreatedOn string `json:"created_on"`
}
func (c *Client) News(projectId int) ([]News, error) {
res, err := c.Get(c.endpoint + "/projects/" + strconv.Itoa(projectId) + "/news.json?key=" + c.apikey + c.getPaginationClause())
if err != nil {
return nil, err
}
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
var r newsResult
if res.StatusCode == 404 {
return nil, errors.New("Not Found")
}
if res.StatusCode != 200 {
var er errorsResult
err = decoder.Decode(&er)
if err == nil {
err = errors.New(strings.Join(er.Errors, "\n"))
}
} else {
err = decoder.Decode(&r)
}
if err != nil {
return nil, err
}
return r.News, nil
}