tagger/internal/client/client.go
Matthew Rich 14fda44c41
Some checks failed
Lint / golangci-lint (push) Failing after 10m25s
Declarative Tests / test (push) Failing after 1m59s
add artifacts
2024-04-04 13:37:54 -07:00

52 lines
999 B
Go

// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
//
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
}