magellan/pkg/client/default.go

53 lines
1.5 KiB
Go

package client
import (
"fmt"
"net/http"
)
type DefaultClient struct {
*http.Client
}
func (c *DefaultClient) Name() string {
return "default"
}
// Add() is the default function that is called with a client with no implementation.
// This function will simply make a HTTP request including all the data passed as
// the first argument with no data processing or manipulation. The function sends
// the data to a set callback URL (which may be changed to use a configurable value
// instead).
func (c *DefaultClient) Add(data HTTPBody, headers HTTPHeader) error {
if data == nil {
return fmt.Errorf("no data found")
}
path := "/inventory/add"
res, body, err := MakeRequest(c.Client, path, http.MethodPost, data, headers)
if res != nil {
statusOk := res.StatusCode >= 200 && res.StatusCode < 300
if !statusOk {
return fmt.Errorf("returned status code %d when POST'ing to endpoint", res.StatusCode)
}
fmt.Printf("%v (%v)\n%s\n", path, res.Status, string(body))
}
return err
}
func (c *DefaultClient) Update(data HTTPBody, headers HTTPHeader) error {
if data == nil {
return fmt.Errorf("no data found")
}
path := "/inventory/update"
res, body, err := MakeRequest(c.Client, path, http.MethodPut, data, headers)
if res != nil {
statusOk := res.StatusCode >= 200 && res.StatusCode < 300
if !statusOk {
return fmt.Errorf("returned status code %d when PUT'ing to endpoint", res.StatusCode)
}
fmt.Printf("%v (%v)\n%s\n", path, res.Status, string(body))
}
return err
}