-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Parth Mudgal
committed
Aug 22, 2020
1 parent
1c44342
commit 0b79489
Showing
4 changed files
with
197 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,40 @@ | ||
package daptin_go_client | ||
|
||
type JsonApiObject struct { | ||
} | ||
import "github.com/go-resty/resty/v2" | ||
|
||
type DaptinQueryParameters struct { | ||
} | ||
type JsonApiObject map[string]interface{} | ||
type DaptinQueryParameters map[string]interface{} | ||
|
||
type DaptinActionResponse struct { | ||
ResponseType string | ||
Attributes map[string]interface{} | ||
} | ||
|
||
type DaptinClient interface { | ||
FindOne(tableName string, referenceId string) (JsonApiObject, error) | ||
FindAll(...DaptinQueryParameters) ([]JsonApiObject, error) | ||
Create(object JsonApiObject) (JsonApiObject, error) | ||
Update(object JsonApiObject) error | ||
FindAll(tableName string, parameters DaptinQueryParameters) ([]JsonApiObject, error) | ||
Create(tableName string, attributes JsonApiObject) (JsonApiObject, error) | ||
Update(tableName, referenceId string, object JsonApiObject) (JsonApiObject, error) | ||
Delete(tableName string, referenceId string) error | ||
Execute(actionName string, tableName string, parameters map[string]interface{}) (DaptinActionResponse, error) | ||
Authorize(username string, password string) error | ||
Execute(actionName string, tableName string, attributes JsonApiObject) ([]DaptinActionResponse, error) | ||
SetDebug(bool) | ||
} | ||
|
||
func NewDaptinClient(endpoint string) DaptinClient { | ||
func NewDaptinClient(endpoint string, debug bool) DaptinClient { | ||
|
||
return &daptinClientImpl{ | ||
endpoint: endpoint, | ||
endpoint: endpoint, | ||
restyClient: resty.New(), | ||
debug: debug, | ||
} | ||
} | ||
|
||
func NewDaptinClientWithAuthToken(endpoint string, authToken string) DaptinClient { | ||
func NewDaptinClientWithAuthToken(endpoint string, authToken string, debug bool) DaptinClient { | ||
|
||
return &daptinClientImpl{ | ||
endpoint: endpoint, | ||
authToken: authToken, | ||
restyClient: resty.New(), | ||
endpoint: endpoint, | ||
authToken: authToken, | ||
debug: debug, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,188 @@ | ||
package daptin_go_client | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/go-resty/resty/v2" | ||
) | ||
|
||
type daptinClientImpl struct { | ||
endpoint string | ||
authToken string | ||
endpoint string | ||
authToken string | ||
restyClient *resty.Client | ||
debug bool | ||
} | ||
|
||
func (d daptinClientImpl) SetDebug(b bool) { | ||
d.debug = b | ||
} | ||
|
||
func (d daptinClientImpl) nextRequest() *resty.Request { | ||
request := d.restyClient.NewRequest(). | ||
SetAuthToken(d.authToken). | ||
SetHeader("Accept", "application/json"). | ||
SetHeader("Content-Type", "application/json") | ||
if d.debug { | ||
request.EnableTrace() | ||
} | ||
|
||
return request | ||
} | ||
|
||
func (d daptinClientImpl) FindOne(tableName string, referenceId string) (JsonApiObject, error) { | ||
panic("implement me") | ||
request := d.nextRequest() | ||
|
||
var responseObject JsonApiObject | ||
|
||
response, err := request.Get(d.endpoint + "/api/" + tableName + "/" + referenceId) | ||
|
||
if d.debug { | ||
d.LogTraceInfo(err, response) | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
bodyBytes := response.Body() | ||
|
||
err = json.Unmarshal(bodyBytes, &responseObject) | ||
|
||
return responseObject["data"].(JsonApiObject), err | ||
|
||
} | ||
|
||
func (d daptinClientImpl) FindAll(parameters ...DaptinQueryParameters) ([]JsonApiObject, error) { | ||
panic("implement me") | ||
func (d daptinClientImpl) FindAll(tableName string, parameters DaptinQueryParameters) ([]JsonApiObject, error) { | ||
request := d.nextRequest() | ||
|
||
var responseObject JsonApiObject | ||
|
||
response, err := request.Get(d.endpoint + "/api/" + tableName) | ||
|
||
if d.debug { | ||
d.LogTraceInfo(err, response) | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
bodyBytes := response.Body() | ||
|
||
err = json.Unmarshal(bodyBytes, &responseObject) | ||
|
||
return responseObject["data"].([]JsonApiObject), err | ||
|
||
} | ||
|
||
func (d daptinClientImpl) Create(object JsonApiObject) (JsonApiObject, error) { | ||
panic("implement me") | ||
func (d daptinClientImpl) Create(tableName string, attributes JsonApiObject) (JsonApiObject, error) { | ||
|
||
request := d.nextRequest() | ||
|
||
var responseObject JsonApiObject | ||
|
||
response, err := request.SetBody(attributes).Post(d.endpoint + "/api/" + tableName) | ||
|
||
if d.debug { | ||
d.LogTraceInfo(err, response) | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
bodyBytes := response.Body() | ||
|
||
err = json.Unmarshal(bodyBytes, &responseObject) | ||
|
||
return responseObject["data"].(JsonApiObject), err | ||
|
||
} | ||
|
||
func (d daptinClientImpl) Update(object JsonApiObject) error { | ||
panic("implement me") | ||
func (d daptinClientImpl) Update(tableName, referenceId string, object JsonApiObject) (JsonApiObject, error) { | ||
request := d.nextRequest() | ||
|
||
var responseObject JsonApiObject | ||
|
||
response, err := request.SetBody(object).Patch(d.endpoint + "/api/" + tableName + "/" + referenceId) | ||
|
||
if d.debug { | ||
d.LogTraceInfo(err, response) | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
bodyBytes := response.Body() | ||
|
||
err = json.Unmarshal(bodyBytes, &responseObject) | ||
|
||
return responseObject["data"].(JsonApiObject), err | ||
|
||
} | ||
|
||
func (d daptinClientImpl) Delete(tableName string, referenceId string) error { | ||
panic("implement me") | ||
request := d.nextRequest() | ||
|
||
//var responseObject JsonApiObject | ||
|
||
response, err := request.Delete(d.endpoint + "/api/" + tableName + "/" + referenceId) | ||
|
||
if d.debug { | ||
d.LogTraceInfo(err, response) | ||
} | ||
return err | ||
} | ||
|
||
func (d daptinClientImpl) Execute(actionName string, tableName string, parameters map[string]interface{}) (DaptinActionResponse, error) { | ||
panic("implement me") | ||
func (d daptinClientImpl) Execute(actionName string, tableName string, attributes JsonApiObject) ([]DaptinActionResponse, error) { | ||
request := d.nextRequest() | ||
|
||
var responseObject []DaptinActionResponse | ||
|
||
actionAttributes := map[string]interface{}{ | ||
"Name": actionName, | ||
"OnType": tableName, | ||
"Attributes": attributes, | ||
} | ||
|
||
response, err := request.SetBody(actionAttributes).Post(d.endpoint + "/action/" + tableName + "/" + actionName) | ||
|
||
if d.debug { | ||
d.LogTraceInfo(err, response) | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
bodyBytes := response.Body() | ||
|
||
err = json.Unmarshal(bodyBytes, &responseObject) | ||
|
||
return responseObject, err | ||
|
||
} | ||
|
||
func (d daptinClientImpl) Authorize(username string, password string) error { | ||
panic("implement me") | ||
func (d daptinClientImpl) LogTraceInfo(err error, resp *resty.Response) { | ||
// Explore response object | ||
fmt.Println("Response Info:") | ||
fmt.Println("Error :", err) | ||
fmt.Println("Status Code:", resp.StatusCode()) | ||
fmt.Println("Status :", resp.Status()) | ||
fmt.Println("Proto :", resp.Proto()) | ||
fmt.Println("Time :", resp.Time()) | ||
fmt.Println("Received At:", resp.ReceivedAt()) | ||
fmt.Println("Body :\n", resp) | ||
fmt.Println() | ||
|
||
// Explore trace info | ||
fmt.Println("Request Trace Info:") | ||
ti := resp.Request.TraceInfo() | ||
fmt.Println("DNSLookup :", ti.DNSLookup) | ||
fmt.Println("ConnTime :", ti.ConnTime) | ||
fmt.Println("TCPConnTime :", ti.TCPConnTime) | ||
fmt.Println("TLSHandshake :", ti.TLSHandshake) | ||
fmt.Println("ServerTime :", ti.ServerTime) | ||
fmt.Println("ResponseTime :", ti.ResponseTime) | ||
fmt.Println("TotalTime :", ti.TotalTime) | ||
fmt.Println("IsConnReused :", ti.IsConnReused) | ||
fmt.Println("IsConnWasIdle:", ti.IsConnWasIdle) | ||
fmt.Println("ConnIdleTime :", ti.ConnIdleTime) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module github.com/daptin/daptin-go-client | ||
|
||
go 1.13 | ||
|
||
require github.com/go-resty/resty/v2 v2.3.0 // indirect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
github.com/go-resty/resty/v2 v2.3.0 h1:JOOeAvjSlapTT92p8xiS19Zxev1neGikoHsXJeOq8So= | ||
github.com/go-resty/resty/v2 v2.3.0/go.mod h1:UpN9CgLZNsv4e9XG50UU8xdI0F43UQ4HmxLBDwaroHU= | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/net v0.0.0-20200513185701-a91f0712d120 h1:EZ3cVSzKOlJxAd8e8YAJ7no8nNypTxexh/YE/xW3ZEY= | ||
golang.org/x/net v0.0.0-20200513185701-a91f0712d120/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= |