1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
5 // Somewhat inspired by https://github.com/mattn/go-redmine (MIT licensed)
23 type errorsResult struct {
24 Errors []string `json:"errors"`
29 Name string `json:"name"`
36 func NewClient(endpoint, apikey string) *Client {
37 return &Client{endpoint, apikey, http.DefaultClient}
40 func (c *Client) Get(url string) (*http.Response, error) {
41 req, err := http.NewRequest("GET", c.endpoint+url, nil)
45 req.Header.Add("X-Redmine-API-Key", c.apikey)
53 func (c *Client) Post(url string, payload string) (*http.Response, error) {
54 req, err := http.NewRequest("POST", c.endpoint+url, strings.NewReader(payload))
58 req.Header.Set("Content-Type", "application/json")
59 req.Header.Add("X-Redmine-API-Key", c.apikey)
67 func (c *Client) Put(url string, payload string) (*http.Response, error) {
68 req, err := http.NewRequest("PUT", c.endpoint+url, strings.NewReader(payload))
72 req.Header.Set("Content-Type", "application/json")
73 req.Header.Add("X-Redmine-API-Key", c.apikey)
81 func responseHelper(res *http.Response, r interface{}, okCode int) error {
83 decoder := json.NewDecoder(res.Body)
84 if res.StatusCode != okCode {
85 var result errorsResult
86 err = decoder.Decode(&result)
88 err = errors.New(strings.Join(result.Errors, "\n"))
89 } else if err.Error() == "EOF" {
90 // The body is empty, just return the status code as an error. This is
91 // an error because res.StatusCode != okCode.
92 err = fmt.Errorf("[error] %s", res.Status)
95 // When r is nil, the API call is not expected to return a result (empty res.Body)
96 err = decoder.Decode(&r)
98 //fmt.Printf("***\nRAW RESPONSE: %+v\n***\n", r)