mirror of
https://github.com/prymitive/karma
synced 2026-05-05 03:16:51 +00:00
Move joinURL to the transport package
Move it and make it public so it can be reused outside of alertmanager package
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/cloudflare/unsee/config"
|
||||
"github.com/cloudflare/unsee/models"
|
||||
"github.com/cloudflare/unsee/transport"
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
)
|
||||
@@ -22,7 +23,7 @@ type AlertGroupsAPIResponse struct {
|
||||
func (response *AlertGroupsAPIResponse) Get() error {
|
||||
start := time.Now()
|
||||
|
||||
url, err := joinURL(config.Config.AlertmanagerURI, "api/v1/alerts/groups")
|
||||
url, err := transport.JoinURL(config.Config.AlertmanagerURI, "api/v1/alerts/groups")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -6,24 +6,11 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
"time"
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
)
|
||||
|
||||
// joinURL can be used to join a base url (http(s)://domain.com) and a path (/my/path)
|
||||
// it will return a joined string or an error (if you supply invalid url)
|
||||
func joinURL(base string, sub string) (string, error) {
|
||||
u, err := url.Parse(base)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
u.Path = path.Join(u.Path, sub)
|
||||
return u.String(), nil
|
||||
}
|
||||
|
||||
// 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
|
||||
|
||||
@@ -9,42 +9,6 @@ import (
|
||||
httpmock "gopkg.in/jarcoal/httpmock.v1"
|
||||
)
|
||||
|
||||
type joinURLTest struct {
|
||||
base string
|
||||
sub string
|
||||
url string
|
||||
}
|
||||
|
||||
var joinURLTests = []joinURLTest{
|
||||
joinURLTest{
|
||||
base: "http://localhost",
|
||||
sub: "/sub",
|
||||
url: "http://localhost/sub",
|
||||
},
|
||||
joinURLTest{
|
||||
base: "http://localhost",
|
||||
sub: "/sub/",
|
||||
url: "http://localhost/sub",
|
||||
},
|
||||
joinURLTest{
|
||||
base: "http://am.example.com",
|
||||
sub: "/api/v1/alerts",
|
||||
url: "http://am.example.com/api/v1/alerts",
|
||||
},
|
||||
}
|
||||
|
||||
func TestJoinURL(t *testing.T) {
|
||||
for _, testCase := range joinURLTests {
|
||||
url, err := joinURL(testCase.base, testCase.sub)
|
||||
if err != nil {
|
||||
t.Errorf("joinURL(%v, %v) failed: %s", testCase.base, testCase.sub, err.Error())
|
||||
}
|
||||
if url != testCase.url {
|
||||
t.Errorf("Invalid joined url from '%s' + '%s', expected '%s', got '%s'", testCase.base, testCase.sub, testCase.url, url)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type mockJSONResponse struct {
|
||||
status string
|
||||
integer int
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/cloudflare/unsee/config"
|
||||
"github.com/cloudflare/unsee/models"
|
||||
"github.com/cloudflare/unsee/transport"
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
)
|
||||
@@ -24,7 +25,7 @@ type SilenceAPIResponse struct {
|
||||
func (response *SilenceAPIResponse) Get() error {
|
||||
start := time.Now()
|
||||
|
||||
url, err := joinURL(config.Config.AlertmanagerURI, "api/v1/silences")
|
||||
url, err := transport.JoinURL(config.Config.AlertmanagerURI, "api/v1/silences")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
17
transport/urls.go
Normal file
17
transport/urls.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package transport
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"path"
|
||||
)
|
||||
|
||||
// JoinURL can be used to join a base url (http(s)://domain.com) and a path (/my/path)
|
||||
// it will return a joined string or an error (if you supply invalid url)
|
||||
func JoinURL(base string, sub string) (string, error) {
|
||||
u, err := url.Parse(base)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
u.Path = path.Join(u.Path, sub)
|
||||
return u.String(), nil
|
||||
}
|
||||
43
transport/urls_test.go
Normal file
43
transport/urls_test.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package transport_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/cloudflare/unsee/transport"
|
||||
)
|
||||
|
||||
type joinURLTest struct {
|
||||
base string
|
||||
sub string
|
||||
url string
|
||||
}
|
||||
|
||||
var joinURLTests = []joinURLTest{
|
||||
joinURLTest{
|
||||
base: "http://localhost",
|
||||
sub: "/sub",
|
||||
url: "http://localhost/sub",
|
||||
},
|
||||
joinURLTest{
|
||||
base: "http://localhost",
|
||||
sub: "/sub/",
|
||||
url: "http://localhost/sub",
|
||||
},
|
||||
joinURLTest{
|
||||
base: "http://am.example.com",
|
||||
sub: "/api/v1/alerts",
|
||||
url: "http://am.example.com/api/v1/alerts",
|
||||
},
|
||||
}
|
||||
|
||||
func TestJoinURL(t *testing.T) {
|
||||
for _, testCase := range joinURLTests {
|
||||
url, err := transport.JoinURL(testCase.base, testCase.sub)
|
||||
if err != nil {
|
||||
t.Errorf("joinURL(%v, %v) failed: %s", testCase.base, testCase.sub, err.Error())
|
||||
}
|
||||
if url != testCase.url {
|
||||
t.Errorf("Invalid joined url from '%s' + '%s', expected '%s', got '%s'", testCase.base, testCase.sub, testCase.url, url)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user