Add Result types to Go SDK and properly populate body/map fields (#358)
This commit is contained in:
1 parent
d8dc1ed0b2
commit
a00ee7a948
61 files changed
+1888
-1099
No files matched your search
+128
-48
@@ -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
|
||||
}
|
||||
+25
-24
@@ -44,20 +44,21 @@ func TestList(t *testing.T) {
|
||||
pl, apiErr, err := grps.List(tc.Context(), tt.scopeId)
|
||||
assert.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
assert.Empty(pl)
|
||||
assert.Empty(pl.Items)
|
||||
|
||||
expected := make([]*groups.Group, 10)
|
||||
for i := 0; i < 10; i++ {
|
||||
expected[i], apiErr, err = grps.Create(tc.Context(), tt.scopeId, groups.WithName(fmt.Sprint(i)))
|
||||
createResult, apiErr, err := grps.Create(tc.Context(), tt.scopeId, groups.WithName(fmt.Sprint(i)))
|
||||
require.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
expected[i] = createResult.Item
|
||||
}
|
||||
pl, apiErr, err = grps.List(tc.Context(), tt.scopeId)
|
||||
require.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
require.NotNil(pl)
|
||||
require.Len(pl, 10)
|
||||
assert.ElementsMatch(comparableSlice(expected), comparableSlice(pl))
|
||||
require.Len(pl.Items, 10)
|
||||
assert.ElementsMatch(comparableSlice(expected), comparableSlice(pl.Items))
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -131,34 +132,34 @@ func TestCrud(t *testing.T) {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
groupsClient := groups.NewClient(client)
|
||||
g, apiErr, err := groupsClient.Create(tc.Context(), tt.scopeId, groups.WithName("foo"))
|
||||
checkGroup("create", g, apiErr, err, "foo", 1, nil)
|
||||
checkGroup("create", g.Item, apiErr, err, "foo", 1, nil)
|
||||
|
||||
g, apiErr, err = groupsClient.Read(tc.Context(), g.Id)
|
||||
checkGroup("read", g, apiErr, err, "foo", 1, nil)
|
||||
g, apiErr, err = groupsClient.Read(tc.Context(), g.Item.Id)
|
||||
checkGroup("read", g.Item, apiErr, err, "foo", 1, nil)
|
||||
|
||||
g, apiErr, err = groupsClient.Update(tc.Context(), g.Id, g.Version, groups.WithName("bar"))
|
||||
checkGroup("update", g, apiErr, err, "bar", 2, nil)
|
||||
g, apiErr, err = groupsClient.Update(tc.Context(), g.Item.Id, g.Item.Version, groups.WithName("bar"))
|
||||
checkGroup("update", g.Item, apiErr, err, "bar", 2, nil)
|
||||
|
||||
g, apiErr, err = groupsClient.Update(tc.Context(), g.Id, g.Version, groups.DefaultName())
|
||||
checkGroup("update", g, apiErr, err, "", 3, nil)
|
||||
g, apiErr, err = groupsClient.Update(tc.Context(), g.Item.Id, g.Item.Version, groups.DefaultName())
|
||||
checkGroup("update", g.Item, apiErr, err, "", 3, nil)
|
||||
|
||||
g, apiErr, err = groupsClient.AddMembers(tc.Context(), g.Id, g.Version, []string{user1.Id})
|
||||
checkGroup("update", g, apiErr, err, "", 4, []string{user1.Id})
|
||||
g, apiErr, err = groupsClient.AddMembers(tc.Context(), g.Item.Id, g.Item.Version, []string{user1.Item.Id})
|
||||
checkGroup("update", g.Item, apiErr, err, "", 4, []string{user1.Item.Id})
|
||||
|
||||
g, apiErr, err = groupsClient.SetMembers(tc.Context(), g.Id, g.Version, []string{user2.Id})
|
||||
checkGroup("update", g, apiErr, err, "", 5, []string{user2.Id})
|
||||
g, apiErr, err = groupsClient.SetMembers(tc.Context(), g.Item.Id, g.Item.Version, []string{user2.Item.Id})
|
||||
checkGroup("update", g.Item, apiErr, err, "", 5, []string{user2.Item.Id})
|
||||
|
||||
g, apiErr, err = groupsClient.RemoveMembers(tc.Context(), g.Id, g.Version, []string{user2.Id})
|
||||
checkGroup("update", g, apiErr, err, "", 6, nil)
|
||||
g, apiErr, err = groupsClient.RemoveMembers(tc.Context(), g.Item.Id, g.Item.Version, []string{user2.Item.Id})
|
||||
checkGroup("update", g.Item, apiErr, err, "", 6, nil)
|
||||
|
||||
existed, apiErr, err := groupsClient.Delete(tc.Context(), g.Id)
|
||||
assert.NoError(err)
|
||||
assert.True(existed, "Expected existing user when deleted, but it wasn't.")
|
||||
|
||||
existed, apiErr, err = groupsClient.Delete(tc.Context(), g.Id)
|
||||
_, apiErr, err = groupsClient.Delete(tc.Context(), g.Item.Id)
|
||||
require.NoError(err)
|
||||
assert.Nil(apiErr)
|
||||
assert.False(existed)
|
||||
|
||||
_, apiErr, err = groupsClient.Delete(tc.Context(), g.Item.Id)
|
||||
require.NoError(err)
|
||||
require.NotNil(apiErr)
|
||||
assert.EqualValues(http.StatusNotFound, apiErr.Status)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -211,7 +212,7 @@ func TestErrors(t *testing.T) {
|
||||
assert.NotNil(apiErr)
|
||||
assert.EqualValues(http.StatusBadRequest, apiErr.Status)
|
||||
|
||||
_, apiErr, err = groupClient.Update(tc.Context(), g.Id, g.Version)
|
||||
_, apiErr, err = groupClient.Update(tc.Context(), g.Item.Id, g.Item.Version)
|
||||
require.NoError(err)
|
||||
assert.NotNil(apiErr)
|
||||
assert.EqualValues(http.StatusBadRequest, apiErr.Status)
|
||||
|
||||
@@ -1,20 +1,7 @@
|
||||
// Code generated by "make api"; DO NOT EDIT.
|
||||
package groups
|
||||
|
||||
import "bytes"
|
||||
|
||||
type Member struct {
|
||||
Id string `json:"id,omitempty"`
|
||||
ScopeId string `json:"scope_id,omitempty"`
|
||||
|
||||
lastResponseBody *bytes.Buffer
|
||||
lastResponseMap map[string]interface{}
|
||||
}
|
||||
|
||||
func (n Member) LastResponseBody() *bytes.Buffer {
|
||||
return n.lastResponseBody
|
||||
}
|
||||
|
||||
func (n Member) LastResponseMap() map[string]interface{} {
|
||||
return n.lastResponseMap
|
||||
}
|
||||
Reference in new issue
Block a user