Skip to content

Commit

Permalink
fix(client): Allow client models to be JSON serialized.
Browse files Browse the repository at this point in the history
  • Loading branch information
spbsoluble committed Mar 15, 2024
1 parent d860f3d commit cf1613a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
11 changes: 9 additions & 2 deletions v2/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,18 @@ func (c *Client) sendRequest(request *request) (*http.Response, error) {
log.Printf("[ERROR] Call to %s returned status %d. %s", keyfactorPath, resp.StatusCode, stringMessage)
return nil, errors.New(stringMessage)
} else if resp.StatusCode == http.StatusUnauthorized {
_, derr := httputil.DumpResponse(resp, true)
dmp, derr := httputil.DumpResponse(resp, true)
if derr != nil {
return nil, derr
}
err = errors.New("401 - Unauthorized: Access is denied due to invalid credentials")
//convert response to string
if dmp != nil {
respStr := string(dmp)
errMsg := fmt.Sprintf("http %d: %s", resp.StatusCode, respStr)
return nil, errors.New(errMsg)
}
errMsg := fmt.Sprintf("http %d: %s", resp.StatusCode, "Unauthorized: Access is denied due to invalid credentials")
err = errors.New(errMsg)
return nil, err
} else {
var errorMessage map[string]interface{} // Decode JSON body to handle issue
Expand Down
18 changes: 9 additions & 9 deletions v2/api/client_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@ package api
// StringTuple is a struct holding two string elements used by the Keyfactor
// Go Client library for data types requiring a tuple of strings
type StringTuple struct {
Elem1 string
Elem2 string
Elem1 string `json:"elem1,omitempty"`
Elem2 string `json:"elem2,omitempty"`
}

// apiHeaders is a struct that holds an array of StringTuples used
// to modularize the passing of custom API headers.
type apiHeaders struct {
Headers []StringTuple
Headers []StringTuple `json:"headers,omitempty"`
}

// apiQuery is a struct that holds an array of StringTuples used
// to modularize the passing of custom URL query parameters.
type apiQuery struct {
Query []StringTuple
Query []StringTuple `json:"query,omitempty"`
}

// request is a structure that holds required information for communicating with
// the Keyfactor API. Included inside this struct is a pointer to an APIHeaders struct, a payload as an
// interface, and other configuration information for the API call.
type request struct {
Method string
Endpoint string
Headers *apiHeaders
Query *apiQuery
Payload interface{}
Method string `json:"method"`
Endpoint string `json:"endpoint"`
Headers *apiHeaders `json:"headers,omitempty"`
Query *apiQuery `json:"query,omitempty"`
Payload interface{} `json:"payload,omitempty"`
}

0 comments on commit cf1613a

Please sign in to comment.