mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 03:27:03 -07:00
Refactored how clients work to reduce hard-coded dependencies
This commit is contained in:
parent
8e59885f55
commit
c5a348562b
3 changed files with 100 additions and 57 deletions
55
pkg/client/default.go
Normal file
55
pkg/client/default.go
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
package client
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/OpenCHAMI/magellan/internal/util"
|
||||
)
|
||||
|
||||
type MagellanClient struct {
|
||||
*http.Client
|
||||
}
|
||||
|
||||
func (c *MagellanClient) 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 *MagellanClient) Add(data util.HTTPBody, headers util.HTTPHeader) error {
|
||||
if data == nil {
|
||||
return fmt.Errorf("no data found")
|
||||
}
|
||||
|
||||
path := "/inventory/add"
|
||||
res, body, err := util.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 *MagellanClient) Update(data util.HTTPBody, headers util.HTTPHeader) error {
|
||||
if data == nil {
|
||||
return fmt.Errorf("no data found")
|
||||
}
|
||||
|
||||
path := "/inventory/update"
|
||||
res, body, err := util.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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue