From 56927666ef92ffa21ca6f8ce2f3b52ef9d47648f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paulo=20Sima=CC=83o?= Date: Fri, 5 Nov 2021 10:11:04 -0300 Subject: [PATCH] fixed prop name --- api/dataapi/lib.go | 44 +++++++++++++++++++++ api/dataapi/lib_test.go | 87 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 api/dataapi/lib.go create mode 100644 api/dataapi/lib_test.go diff --git a/api/dataapi/lib.go b/api/dataapi/lib.go new file mode 100644 index 0000000..415319f --- /dev/null +++ b/api/dataapi/lib.go @@ -0,0 +1,44 @@ +package dataapi + +import ( + "go.digitalcircle.com.br/open/httpcli" +) + +type DataApiOp string + +const ( + RETRIEVE DataApiOp = "R" + CREATE DataApiOp = "C" + DELETE DataApiOp = "D" + UPDATE DataApiOp = "U" +) + +type DataAPIRequest struct { + Col string `json:"col"` + Q string `json:"q"` + Op DataApiOp `json:"op"` + Id string `json:"id"` + Data map[string]interface{} `json:"data"` +} + +type DataAPIResponse struct { + Msg string `json:"msg"` + Id string `json:"id"` + Data []interface{} `json:"data"` +} + +type Cli struct { + Httpcli *httpcli.Cli +} + +func (c *Cli) Do(request *DataAPIRequest) (response *DataAPIResponse, err error) { + response = &DataAPIResponse{} + err = c.Httpcli.JsonPost("/", request, response) + return +} + +func NewCli() *Cli { + ret := &Cli{Httpcli: httpcli.NewCli()} + ret.Httpcli.SetBasePath("https://dataapi.digitalcircle.com.br") + return ret +} diff --git a/api/dataapi/lib_test.go b/api/dataapi/lib_test.go new file mode 100644 index 0000000..0276226 --- /dev/null +++ b/api/dataapi/lib_test.go @@ -0,0 +1,87 @@ +package dataapi_test + +import ( + "go.digitalcircle.com.br/open/replaycli-go/api/dataapi" + "log" + "testing" +) + +func TestAPIRetrieve(t *testing.T) { + c := dataapi.NewCli() + c.Httpcli.AddHeader("X-API-KEY", "RnJpIDA1IE5vdiAyMDIxIDExOjE1OjIyIEFNIFVUQwo") + res, err := c.Do(&dataapi.DataAPIRequest{ + Col: "C1", + Op: dataapi.RETRIEVE, + }) + if err != nil { + t.Fatal(err.Error()) + } + + log.Printf("%#v", res) +} + +func TestAPIRetrieveWQuery(t *testing.T) { + c := dataapi.NewCli() + c.Httpcli.AddHeader("X-API-KEY", "RnJpIDA1IE5vdiAyMDIxIDExOjE1OjIyIEFNIFVUQwo") + res, err := c.Do(&dataapi.DataAPIRequest{ + Col: "C1", + Op: dataapi.RETRIEVE, + Q: "@[?ID==`2`]", + }) + if err != nil { + t.Fatal(err.Error()) + } + + log.Printf("%#v", res) +} + +func TestAPIUpdate(t *testing.T) { + c := dataapi.NewCli() + c.Httpcli.AddHeader("X-API-KEY", "RnJpIDA1IE5vdiAyMDIxIDExOjE1OjIyIEFNIFVUQwo") + res, err := c.Do(&dataapi.DataAPIRequest{ + Col: "C1", + Op: dataapi.UPDATE, + Id: "2", + Data: map[string]interface{}{ + "F2": "Novo Campo", + }, + }) + if err != nil { + t.Fatal(err.Error()) + } + + log.Printf("%#v", res) +} + +func TestAPIDelete(t *testing.T) { + c := dataapi.NewCli() + c.Httpcli.AddHeader("X-API-KEY", "RnJpIDA1IE5vdiAyMDIxIDExOjE1OjIyIEFNIFVUQwo") + res, err := c.Do(&dataapi.DataAPIRequest{ + Col: "C1", + Op: dataapi.DELETE, + Id: "2", + }) + if err != nil { + t.Fatal(err.Error()) + } + + log.Printf("%#v", res) +} + +func TestAPICreate(t *testing.T) { + c := dataapi.NewCli() + c.Httpcli.AddHeader("X-API-KEY", "RnJpIDA1IE5vdiAyMDIxIDExOjE1OjIyIEFNIFVUQwo") + + res, err := c.Do(&dataapi.DataAPIRequest{ + Col: "C1", + Op: dataapi.CREATE, + Data: map[string]interface{}{ + "MSG": "OI LEO", + }, + }) + if err != nil { + t.Fatal(err.Error()) + } + + log.Printf("%#v", res) +}