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
}
+19 -18
View File
@@ -27,31 +27,33 @@ func TestList(t *testing.T) {
ul, apiErr, err := userClient.List(tc.Context(), org.GetPublicId())
assert.NoError(err)
assert.Nil(apiErr)
assert.Empty(ul)
assert.Empty(ul.Items)
var expected []*users.User
for i := 0; i < 10; i++ {
expected = append(expected, &users.User{Name: fmt.Sprint(i)})
}
expected[0], apiErr, err = userClient.Create(tc.Context(), org.GetPublicId(), users.WithName(expected[0].Name))
ucr, apiErr, err := userClient.Create(tc.Context(), org.GetPublicId(), users.WithName(expected[0].Name))
assert.NoError(err)
assert.Nil(apiErr)
expected[0] = ucr.Item
ul, apiErr, err = userClient.List(tc.Context(), org.GetPublicId())
assert.NoError(err)
assert.Nil(apiErr)
assert.ElementsMatch(comparableSlice(expected[:1]), comparableSlice(ul))
assert.ElementsMatch(comparableSlice(expected[:1]), comparableSlice(ul.Items))
for i := 1; i < 10; i++ {
expected[i], apiErr, err = userClient.Create(tc.Context(), org.GetPublicId(), users.WithName(expected[i].Name))
ucr, apiErr, err = userClient.Create(tc.Context(), org.GetPublicId(), users.WithName(expected[i].Name))
assert.NoError(err)
assert.Nil(apiErr)
expected[i] = ucr.Item
}
ul, apiErr, err = userClient.List(tc.Context(), org.GetPublicId())
require.NoError(err)
assert.Nil(apiErr)
assert.ElementsMatch(comparableSlice(expected), comparableSlice(ul))
assert.ElementsMatch(comparableSlice(expected), comparableSlice(ul.Items))
}
func comparableSlice(in []*users.User) []users.User {
@@ -95,26 +97,25 @@ func TestCrud(t *testing.T) {
}
u, apiErr, err := userClient.Create(tc.Context(), org.GetPublicId(), users.WithName("foo"))
checkUser("create", u, apiErr, err, "foo", 1)
checkUser("create", u.Item, apiErr, err, "foo", 1)
u, apiErr, err = userClient.Read(tc.Context(), u.Id)
checkUser("read", u, apiErr, err, "foo", 1)
u, apiErr, err = userClient.Read(tc.Context(), u.Item.Id)
checkUser("read", u.Item, apiErr, err, "foo", 1)
u, apiErr, err = userClient.Update(tc.Context(), u.Id, u.Version, users.WithName("bar"))
checkUser("update", u, apiErr, err, "bar", 2)
u, apiErr, err = userClient.Update(tc.Context(), u.Item.Id, u.Item.Version, users.WithName("bar"))
checkUser("update", u.Item, apiErr, err, "bar", 2)
u, apiErr, err = userClient.Update(tc.Context(), u.Id, u.Version, users.DefaultName())
checkUser("update", u, apiErr, err, "", 3)
u, apiErr, err = userClient.Update(tc.Context(), u.Item.Id, u.Item.Version, users.DefaultName())
checkUser("update", u.Item, apiErr, err, "", 3)
existed, _, err := userClient.Delete(tc.Context(), u.Id)
_, apiErr, err = userClient.Delete(tc.Context(), u.Item.Id)
require.NoError(err)
assert.Nil(apiErr)
assert.True(existed, "Expected existing user when deleted, but it wasn't.")
existed, apiErr, err = userClient.Delete(tc.Context(), u.Id)
_, apiErr, err = userClient.Delete(tc.Context(), u.Item.Id)
require.NoError(err)
assert.Nil(apiErr)
assert.False(existed)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusNotFound, apiErr.Status)
}
func TestErrors(t *testing.T) {
@@ -148,7 +149,7 @@ func TestErrors(t *testing.T) {
assert.NotNil(apiErr)
assert.EqualValues(http.StatusBadRequest, apiErr.Status)
_, apiErr, err = userClient.Update(tc.Context(), u.Id, u.Version)
_, apiErr, err = userClient.Update(tc.Context(), u.Item.Id, u.Item.Version)
require.NoError(err)
assert.NotNil(apiErr)
assert.EqualValues(http.StatusBadRequest, apiErr.Status)