mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Issue #600 points out Jenkins module panics occasionally I noticed that a number of places panic because of JSON parsing Centralize JSON parsing, have it return an error, and have widgets report the error
57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
package rollbar
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"github.com/wtfutil/wtf/utils"
|
|
)
|
|
|
|
func CurrentActiveItems(accessToken, assignedToName string, activeOnly bool) (*ActiveItems, error) {
|
|
items := &ActiveItems{}
|
|
|
|
rollbarAPIURL.Host = "api.rollbar.com"
|
|
rollbarAPIURL.Path = "/api/1/items"
|
|
resp, err := rollbarItemRequest(accessToken, assignedToName, activeOnly)
|
|
if err != nil {
|
|
return items, err
|
|
}
|
|
|
|
err = utils.ParseJson(&items, resp.Body)
|
|
|
|
return items, nil
|
|
}
|
|
|
|
/* -------------------- Unexported Functions -------------------- */
|
|
|
|
var (
|
|
rollbarAPIURL = &url.URL{Scheme: "https"}
|
|
)
|
|
|
|
func rollbarItemRequest(accessToken, assignedToName string, activeOnly bool) (*http.Response, error) {
|
|
params := url.Values{}
|
|
params.Add("access_token", accessToken)
|
|
params.Add("assigned_user", assignedToName)
|
|
if activeOnly {
|
|
params.Add("status", "active")
|
|
}
|
|
|
|
requestURL := rollbarAPIURL.ResolveReference(&url.URL{RawQuery: params.Encode()})
|
|
req, err := http.NewRequest("GET", requestURL.String(), nil)
|
|
req.Header.Add("Accept", "application/json")
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
httpClient := &http.Client{}
|
|
resp, err := httpClient.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
|
return nil, fmt.Errorf(resp.Status)
|
|
}
|
|
|
|
return resp, nil
|
|
}
|