From ed95955d110da25737c06f18196946b2cfeebd65 Mon Sep 17 00:00:00 2001 From: Sean Murphy Date: Thu, 18 Apr 2019 14:23:46 +0200 Subject: [PATCH] Added copyright notice; added client library --- client/client.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ types.go | 10 +++++--- 2 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 client/client.go diff --git a/client/client.go b/client/client.go new file mode 100644 index 0000000..14895b4 --- /dev/null +++ b/client/client.go @@ -0,0 +1,64 @@ +// +// A basic package that facilitates interactions with the Cyclops +// customer db - these types are used within the customer DB itself +// as well as clients of the customer DB. +// +// (C) Cyclops Labs 2019 +// + +package client + +import ( + "bytes" + "encoding/json" + "gitlab.com/cyclops-enterprise/types" + l "gitlab.com/cyclops-utilities/logging" + "io/ioutil" + "net/http" +) + +// CustomerDBService contains the endpoint where the customer db service lives... +// It is implemented as an interface which includes a PostCommand... +type CustomerDBService struct { + Endpoint string +} + +// Creates a Reseller in the customer DB; note that it is assumed that the +// reseller contains a set of customers and that these can be marshalled +// correctly. +func (s *CustomerDBService) CreateReseller(r types.Reseller) (success bool, err error) { + val, _ := json.Marshal(r) + // fmt.Printf("r= %+v\n", string(val)) + newCustomerEndpoint := s.Endpoint + "/reseller/" + req, err := http.NewRequest("POST", newCustomerEndpoint, bytes.NewBuffer(val)) + req.Header.Set("Content-Type", "application/json") + + client := &http.Client{} + resp, err := client.Do(req) + + if err != nil { + //panic(err) + l.Error.Printf("error = %v\n", err.Error()) + success = false + return + } + + //TODO(murp) - fixme...) + defer resp.Body.Close() + + if err == nil { + if resp.StatusCode != http.StatusCreated { + l.Warning.Printf("Unexpected response Creating new Reseller record - Response Status: %v\n", resp.Status) + body, _ := ioutil.ReadAll(resp.Body) + l.Debug.Printf("Response Body: %v\n", string(body)) + success = false + } else { + // this is the successful case - only dump output if in debug mode as this + // should be the default... + body, _ := ioutil.ReadAll(resp.Body) + l.Debug.Printf("Sending Cyclops Customer DB service - Response Status: %v, Response Body: %v\n", resp.Status, string(body)) + success = true + } + } + return +} diff --git a/types.go b/types.go index 47752f7..7dae0bb 100644 --- a/types.go +++ b/types.go @@ -1,6 +1,10 @@ -// A basic package that facilitates interactions with the cyclops customer db -// This package provides - +// +// A basic package that facilitates interactions with the Cyclops +// customer db - these types are used within the customer DB itself +// as well as clients of the customer DB. +// +// (C) Cyclops Labs 2019 +// package types type Product struct {