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

+128 -48
View File
@@ -6,7 +6,6 @@ import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"time"
@@ -28,16 +27,68 @@ type Group struct {
MemberIds []string `json:"member_ids,omitempty"`
Members []*Member `json:"members,omitempty"`
lastResponseBody *bytes.Buffer
lastResponseMap map[string]interface{}
responseBody *bytes.Buffer
responseMap map[string]interface{}
}
func (n Group) LastResponseBody() *bytes.Buffer {
return n.lastResponseBody
func (n Group) ResponseBody() *bytes.Buffer {
return n.responseBody
}
func (n Group) LastResponseMap() map[string]interface{} {
return n.lastResponseMap
func (n Group) ResponseMap() map[string]interface{} {
return n.responseMap
}
type GroupReadResult struct {
Item *Group
responseBody *bytes.Buffer
responseMap map[string]interface{}
}
func (n GroupReadResult) GetItem() interface{} {
return n.Item
}
func (n GroupReadResult) GetResponseBody() *bytes.Buffer {
return n.responseBody
}
func (n GroupReadResult) GetResponseMap() map[string]interface{} {
return n.responseMap
}
type GroupCreateResult = GroupReadResult
type GroupUpdateResult = GroupReadResult
type GroupDeleteResult struct {
responseBody *bytes.Buffer
responseMap map[string]interface{}
}
func (n GroupDeleteResult) GetResponseBody() *bytes.Buffer {
return n.responseBody
}
func (n GroupDeleteResult) GetResponseMap() map[string]interface{} {
return n.responseMap
}
type GroupListResult struct {
Items []*Group
responseBody *bytes.Buffer
responseMap map[string]interface{}
}
func (n GroupListResult) GetItems() interface{} {
return n.Items
}
func (n GroupListResult) GetResponseBody() *bytes.Buffer {
return n.responseBody
}
func (n GroupListResult) GetResponseMap() map[string]interface{} {
return n.responseMap
}
// Client is a client for this collection
@@ -58,7 +109,7 @@ func (c *Client) ApiClient() *api.Client {
return c.client
}
func (c *Client) Create(ctx context.Context, scopeId string, opt ...Option) (*Group, *api.Error, error) {
func (c *Client) Create(ctx context.Context, scopeId string, opt ...Option) (*GroupCreateResult, *api.Error, error) {
if scopeId == "" {
return nil, nil, fmt.Errorf("empty scopeId value passed into Create request")
}
@@ -89,18 +140,21 @@ func (c *Client) Create(ctx context.Context, scopeId string, opt ...Option) (*Gr
return nil, nil, fmt.Errorf("error performing client request during Create call: %w", err)
}
target := new(Group)
apiErr, err := resp.Decode(target)
target := new(GroupCreateResult)
target.Item = new(Group)
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, groupId string, opt ...Option) (*Group, *api.Error, error) {
func (c *Client) Read(ctx context.Context, groupId string, opt ...Option) (*GroupReadResult, *api.Error, error) {
if groupId == "" {
return nil, nil, fmt.Errorf("empty groupId value passed into Read request")
}
@@ -128,18 +182,21 @@ func (c *Client) Read(ctx context.Context, groupId string, opt ...Option) (*Grou
return nil, nil, fmt.Errorf("error performing client request during Read call: %w", err)
}
target := new(Group)
apiErr, err := resp.Decode(target)
target := new(GroupReadResult)
target.Item = new(Group)
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, groupId string, version uint32, opt ...Option) (*Group, *api.Error, error) {
func (c *Client) Update(ctx context.Context, groupId string, version uint32, opt ...Option) (*GroupUpdateResult, *api.Error, error) {
if groupId == "" {
return nil, nil, fmt.Errorf("empty groupId value passed into Update request")
}
@@ -161,9 +218,12 @@ func (c *Client) Update(ctx context.Context, groupId 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
@@ -186,30 +246,33 @@ func (c *Client) Update(ctx context.Context, groupId string, version uint32, opt
return nil, nil, fmt.Errorf("error performing client request during Update call: %w", err)
}
target := new(Group)
apiErr, err := resp.Decode(target)
target := new(GroupUpdateResult)
target.Item = new(Group)
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, groupId string, opt ...Option) (bool, *api.Error, error) {
func (c *Client) Delete(ctx context.Context, groupId string, opt ...Option) (*GroupDeleteResult, *api.Error, error) {
if groupId == "" {
return false, nil, fmt.Errorf("empty groupId value passed into Delete request")
return nil, nil, fmt.Errorf("empty groupId 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("groups/%s", groupId), 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 {
@@ -222,25 +285,25 @@ func (c *Client) Delete(ctx context.Context, groupId string, opt ...Option) (boo
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 := &GroupDeleteResult{
responseBody: resp.Body,
responseMap: resp.Map,
}
return target, nil, nil
}
func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) ([]*Group, *api.Error, error) {
func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) (*GroupListResult, *api.Error, error) {
if scopeId == "" {
return nil, nil, fmt.Errorf("empty scopeId value passed into List request")
}
@@ -269,10 +332,7 @@ func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) ([]*Gr
return nil, nil, fmt.Errorf("error performing client request during List call: %w", err)
}
type listResponse struct {
Items []*Group
}
target := &listResponse{}
target := new(GroupListResult)
apiErr, err := resp.Decode(target)
if err != nil {
return nil, nil, fmt.Errorf("error decoding List response: %w", err)
@@ -280,10 +340,12 @@ func (c *Client) List(ctx context.Context, scopeId string, opt ...Option) ([]*Gr
if apiErr != nil {
return nil, apiErr, nil
}
return target.Items, apiErr, nil
target.responseBody = resp.Body
target.responseMap = resp.Map
return target, apiErr, nil
}
func (c *Client) AddMembers(ctx context.Context, groupId string, version uint32, memberIds []string, opt ...Option) (*Group, *api.Error, error) {
func (c *Client) AddMembers(ctx context.Context, groupId string, version uint32, memberIds []string, opt ...Option) (*GroupUpdateResult, *api.Error, error) {
if groupId == "" {
return nil, nil, fmt.Errorf("empty groupId value passed into AddMembers request")
}
@@ -308,9 +370,12 @@ func (c *Client) AddMembers(ctx context.Context, groupId string, version uint32,
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
@@ -334,18 +399,21 @@ func (c *Client) AddMembers(ctx context.Context, groupId string, version uint32,
return nil, nil, fmt.Errorf("error performing client request during AddMembers call: %w", err)
}
target := new(Group)
apiErr, err := resp.Decode(target)
target := new(GroupUpdateResult)
target.Item = new(Group)
apiErr, err := resp.Decode(target.Item)
if err != nil {
return nil, nil, fmt.Errorf("error decoding AddMembers 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) SetMembers(ctx context.Context, groupId string, version uint32, memberIds []string, opt ...Option) (*Group, *api.Error, error) {
func (c *Client) SetMembers(ctx context.Context, groupId string, version uint32, memberIds []string, opt ...Option) (*GroupUpdateResult, *api.Error, error) {
if groupId == "" {
return nil, nil, fmt.Errorf("empty groupId value passed into SetMembers request")
}
@@ -368,9 +436,12 @@ func (c *Client) SetMembers(ctx context.Context, groupId string, version uint32,
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
@@ -394,18 +465,21 @@ func (c *Client) SetMembers(ctx context.Context, groupId string, version uint32,
return nil, nil, fmt.Errorf("error performing client request during SetMembers call: %w", err)
}
target := new(Group)
apiErr, err := resp.Decode(target)
target := new(GroupUpdateResult)
target.Item = new(Group)
apiErr, err := resp.Decode(target.Item)
if err != nil {
return nil, nil, fmt.Errorf("error decoding SetMembers 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) RemoveMembers(ctx context.Context, groupId string, version uint32, memberIds []string, opt ...Option) (*Group, *api.Error, error) {
func (c *Client) RemoveMembers(ctx context.Context, groupId string, version uint32, memberIds []string, opt ...Option) (*GroupUpdateResult, *api.Error, error) {
if groupId == "" {
return nil, nil, fmt.Errorf("empty groupId value passed into RemoveMembers request")
}
@@ -430,9 +504,12 @@ func (c *Client) RemoveMembers(ctx context.Context, groupId string, version uint
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
@@ -456,13 +533,16 @@ func (c *Client) RemoveMembers(ctx context.Context, groupId string, version uint
return nil, nil, fmt.Errorf("error performing client request during RemoveMembers call: %w", err)
}
target := new(Group)
apiErr, err := resp.Decode(target)
target := new(GroupUpdateResult)
target.Item = new(Group)
apiErr, err := resp.Decode(target.Item)
if err != nil {
return nil, nil, fmt.Errorf("error decoding RemoveMembers response: %w", err)
}
if apiErr != nil {
return nil, apiErr, nil
}
target.responseBody = resp.Body
target.responseMap = resp.Map
return target, apiErr, nil
}