mirror of
https://github.com/prymitive/karma
synced 2026-05-07 03:26:52 +00:00
Move getJSONFromURL to the transport package
Move it and make it public so it can be reused outside of alertmanager package
This commit is contained in:
@@ -28,7 +28,7 @@ func (response *AlertGroupsAPIResponse) Get() error {
|
||||
return err
|
||||
}
|
||||
|
||||
err = getJSONFromURL(url, config.Config.AlertmanagerTimeout, response)
|
||||
err = transport.GetJSONFromURL(url, config.Config.AlertmanagerTimeout, response)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ func (response *SilenceAPIResponse) Get() error {
|
||||
}
|
||||
url = fmt.Sprintf("%s?limit=%d", url, math.MaxUint32)
|
||||
|
||||
err = getJSONFromURL(url, config.Config.AlertmanagerTimeout, response)
|
||||
err = transport.GetJSONFromURL(url, config.Config.AlertmanagerTimeout, response)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package alertmanager
|
||||
package transport
|
||||
|
||||
import (
|
||||
"compress/gzip"
|
||||
@@ -11,10 +11,9 @@ import (
|
||||
log "github.com/Sirupsen/logrus"
|
||||
)
|
||||
|
||||
// getJSONFromURL is a helper function that takesan URL, request timeout
|
||||
// and target structure, it will make a HTTP request and decode JSON response
|
||||
// onto the structure provided
|
||||
func getJSONFromURL(url string, timeout time.Duration, target interface{}) error {
|
||||
// GetJSONFromURL allows to fetch Alertmanager data over HTTP transport and
|
||||
// decode it onto provided data structure.
|
||||
func GetJSONFromURL(url string, timeout time.Duration, target interface{}) error {
|
||||
log.Infof("GET %s", url)
|
||||
|
||||
c := &http.Client{
|
||||
@@ -1,10 +1,11 @@
|
||||
package alertmanager
|
||||
package transport_test
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cloudflare/unsee/transport"
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
httpmock "gopkg.in/jarcoal/httpmock.v1"
|
||||
)
|
||||
@@ -17,7 +18,7 @@ type mockJSONResponse struct {
|
||||
}
|
||||
|
||||
func TestGetJSONFromURL(t *testing.T) {
|
||||
log.SetOutput(ioutil.Discard) // disable logging to console
|
||||
log.SetLevel(log.ErrorLevel)
|
||||
httpmock.Activate()
|
||||
defer httpmock.DeactivateAndReset()
|
||||
mockJSON := `{
|
||||
@@ -29,14 +30,14 @@ func TestGetJSONFromURL(t *testing.T) {
|
||||
httpmock.RegisterResponder("GET", "http://localhost/", httpmock.NewStringResponder(200, mockJSON))
|
||||
|
||||
response := mockJSONResponse{}
|
||||
err := getJSONFromURL("http://localhost/", time.Second, &response)
|
||||
err := transport.GetJSONFromURL("http://localhost/", time.Second, &response)
|
||||
if err != nil {
|
||||
t.Errorf("getJSONFromURL() failed: %s", err.Error())
|
||||
}
|
||||
|
||||
httpmock.RegisterResponder("GET", "http://localhost/404", httpmock.NewStringResponder(404, "Not found"))
|
||||
response = mockJSONResponse{}
|
||||
err = getJSONFromURL("http://localhost/404", time.Second, &response)
|
||||
err = transport.GetJSONFromURL("http://localhost/404", time.Second, &response)
|
||||
if err == nil {
|
||||
t.Errorf("getJSONFromURL() on invalid url didn't return 404, response: %v", response)
|
||||
}
|
||||
Reference in New Issue
Block a user