tagger/internal/client/client.go

52 lines
999 B
Go
Raw Normal View History

2024-04-04 20:37:54 +00:00
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
//
2024-04-04 20:30:41 +00:00
package client
import (
_ "io"
"fmt"
"strings"
_ "encoding/json"
"net/http"
)
type TaggerApi struct {
client *http.Client
endpoint string
}
func New(endpointUrl string) *TaggerApi {
return &TaggerApi{
client: &http.Client{},
endpoint: endpointUrl,
}
}
func (t *TaggerApi) assertReceiver() {
if t == nil {
panic("Method can't be called on nil receiver")
}
}
func (t *TaggerApi) Ping() error {
t.assertReceiver()
resp, e := t.client.Get(fmt.Sprintf("%s/ping", t.endpoint))
defer resp.Body.Close()
return e
}
func (t *TaggerApi) AddTagResource(tagName string, resourceUrl string) error {
t.assertReceiver()
jsonReader := strings.NewReader(fmt.Sprintf(`{ "resource": "%s" }`, resourceUrl))
resp, e := t.client.Post(fmt.Sprintf("%s/tags/%s", t.endpoint, tagName), "application/json", jsonReader)
if e == nil {
defer resp.Body.Close()
}
return e
}