Add Result types to Go SDK and properly populate body/map fields (#358)

This commit is contained in:
Jeff Mitchell authored and GitHub committed 2020-09-15 01:08:21 -04:00
1 parent d8dc1ed0b2
commit a00ee7a948
61 files changed
+1888 -1099

No files matched your search

+98 -36
View File
@@ -6,7 +6,6 @@ import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"time"
@@ -26,16 +25,68 @@ type User struct {
UpdatedTime time.Time `json:"updated_time,omitempty"`
Version uint32 `json:"version,omitempty"`
lastResponseBody *bytes.Buffer
lastResponseMap map[string]interface{}
responseBody *bytes.Buffer
responseMap map[string]interface{}
}
func (n User) LastResponseBody() *bytes.Buffer {
return n.lastResponseBody
func (n User) ResponseBody() *bytes.Buffer {
return n.responseBody
}
func (n User) LastResponseMap() map[string]interface{} {
return n.lastResponseMap
func (n User) ResponseMap() map[string]interface{} {
return n.responseMap
}
type UserReadResult struct {
Item *User
responseBody *bytes.Buffer
responseMap map[string]interface{}
}
func (n UserReadResult) GetItem() interface{} {
return n.Item
}
func (n UserReadResult) GetResponseBody() *bytes.Buffer {
return n.responseBody
}
func (n UserReadResult) GetResponseMap() map[string]interface{} {
return n.responseMap
}
type UserCreateResult = UserReadResult
type UserUpdateResult = UserReadResult
type UserDeleteResult struct {
responseBody *bytes.Buffer
responseMap map[string]interface{}
}
func (n UserDeleteResult) GetResponseBody() *bytes.Buffer {
return n.responseBody
}
func (n UserDeleteResult) GetResponseMap() map[string]interface{} {
return n.responseMap
}
type UserListResult struct {
Items []*User
responseBody *bytes.Buffer
responseMap map[string]interface{}
}
func (n UserListResult) GetItems() interface{} {
return n.Items
}
func (n UserListResult) GetResponseBody() *bytes.Buffer {
return n.responseBody
}
func (n UserListResult) GetResponseMap() map[string]interface{} {
return n.responseMap
}
// Client is a client for this collection
@@ -56,7 +107,7 @@ func (c *Client) ApiClient() *api.Client {
return c.client
}
func (c *Client) Create(ctx context.Context, scopeId string, opt ...Option) (*User, *api.Error, error) {
func (c *Client) Create(ctx context.Context, scopeId string, opt ...Option) (*UserCreateResult, *api.Error, error) {
if scopeId == "" {
return nil, nil, fmt.Errorf("empty scopeId value passed into Create request")
}
@@ -87,18 +138,21 @@ func (c *Client) Create(ctx context.Context, scopeId string, opt ...Option) (*Us
return nil, nil, fmt.Errorf("error performing client request during Create call: %w", err)
}
target := new(User)
apiErr, err := resp.Decode(target)
target := new(UserCreateResult)
target.Item = new(User)
apiErr, err := resp.Decode(target.Item)
if err != nil {
return nil, nil, fmt.Errorf("error decoding Create response: %w", err)
}
if apiErr != nil {
return nil, apiErr, nil
}
target.responseBody = resp.Body
target.responseMap = resp.Map
return target, apiErr, nil
}
func (c *Client) Read(ctx context.Context, userId string, opt ...Option) (*User, *api.Error, error) {
func (c *Client) Read(ctx context.Context, userId string, opt ...Option) (*UserReadResult, *api.Error, error) {
if userId == "" {
return nil, nil, fmt.Errorf("empty userId value passed into Read request")
}
@@ -126,18 +180,21 @@ func (c *Client) Read(ctx context.Context, userId string, opt ...Option) (*User,
return nil, nil, fmt.Errorf("error performing client request during Read call: %w", err)
}
target := new(User)
apiErr, err := resp.Decode(target)
target := new(UserReadResult)
target.Item = new(User)
apiErr, err := resp.Decode(target.Item)
if err != nil {
return nil, nil, fmt.Errorf("error decoding Read response: %w", err)
}
if apiErr != nil {
return nil, apiErr, nil
}
target.responseBody = resp.Body
target.responseMap = resp.Map
return target, apiErr, nil
}
func (c *Client) Update(ctx context.Context, userId string, version uint32, opt ...Option) (*User, *api.Error, error) {
func (c *Client) Update(ctx context.Context, userId string, version uint32, opt ...Option) (*UserUpdateResult, *api.Error, error) {
if userId == "" {
return nil, nil, fmt.Errorf("empty userId value passed into Update request")
}
@@ -159,9 +216,12 @@ func (c *Client) Update(ctx context.Context, userId string, version uint32, opt
return nil, nil, fmt.Errorf("error from controller when performing initial check-and-set read: %s", pretty.Sprint(existingApiErr))
}
if existingTarget == nil {
return nil, nil, errors.New("nil resource response found when performing initial check-and-set read")
}
if existingTarget.Item == nil {
return nil, nil, errors.New("nil resource found when performing initial check-and-set read")
}
version = existingTarget.Version
version = existingTarget.Item.Version
}
opts.postMap["version"] = version
@@ -184,30 +244,33 @@ func (c *Client) Update(ctx context.Context, userId string, version uint32, opt
return nil, nil, fmt.Errorf("error performing client request during Update call: %w", err)
}
target := new(User)
apiErr, err := resp.Decode(target)
target := new(UserUpdateResult)
target.Item = new(User)
apiErr, err := resp.Decode(target.Item)
if err != nil {
return nil, nil, fmt.Errorf("error decoding Update response: %w", err)
}
if apiErr != nil {
return nil, apiErr, nil
}
target.responseBody = resp.Body
target.responseMap = resp.Map
return target, apiErr, nil
}
func (c *Client) Delete(ctx context.Context, userId string, opt ...Option) (bool, *api.Error, error) {
func (c *Client) Delete(ctx context.Context, userId string, opt ...Option) (*UserDeleteResult, *api.Error, error) {
if userId == "" {
return false, nil, fmt.Errorf("empty userId value passed into Delete request")
return nil, nil, fmt.Errorf("empty userId value passed into Delete request")
}
if c.client == nil {
return false, nil, fmt.Errorf("nil client")
return nil, nil, fmt.Errorf("nil client")
}
opts, apiOpts := getOpts(opt...)
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("users/%s", userId), nil, apiOpts...)
if err != nil {
return false, nil, fmt.Errorf("error creating Delete request: %w", err)
return nil, nil, fmt.Errorf("error creating Delete request: %w", err)
}
if len(opts.queryMap) > 0 {
@@ -220,25 +283,25 @@ func (c *Client) Delete(ctx context.Context, userId string, opt ...Option) (bool
resp, err := c.client.Do(req)
if err != nil {
return false, nil, fmt.Errorf("error performing client request during Delete call: %w", err)
return nil, nil, fmt.Errorf("error performing client request during Delete call: %w", err)
}
apiErr, err := resp.Decode(nil)
if err != nil {
return false, nil, fmt.Errorf("error decoding Delete response: %w", err)
return nil, nil, fmt.Errorf("error decoding Delete response: %w", err)
}
if apiErr != nil {
// We don't treat a 404 in this case as failure, in order for deletes to
// be idempotent
if apiErr.Status == http.StatusNotFound {
return false, nil, nil
}
return false, apiErr, nil
return nil, apiErr, nil
}
return true, nil, nil
target := &UserDeleteResult{
responseBody: resp.Body,
responseMap: resp.Map,
}
return target, nil, nil
}
func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) ([]*User, *api.Error, error) {
func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) (*UserListResult, *api.Error, error) {
if scopeId == "" {
return nil, nil, fmt.Errorf("empty scopeId value passed into List request")
}
@@ -267,10 +330,7 @@ func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) ([]*Us
return nil, nil, fmt.Errorf("error performing client request during List call: %w", err)
}
type listResponse struct {
Items []*User
}
target := &listResponse{}
target := new(UserListResult)
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding List response: %w", err)
@@ -278,5 +338,7 @@ func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) ([]*Us
if apiErr != nil {
return nil, apiErr, nil
}
return target.Items, apiErr, nil
target.responseBody = resp.Body
target.responseMap = resp.Map
return target, apiErr, nil
}