diff --git a/cli/things.go b/cli/things.go index 1445827965..d99e9772b3 100644 --- a/cli/things.go +++ b/cli/things.go @@ -10,8 +10,6 @@ import ( "github.com/spf13/cobra" ) -const thingsEP = "things" - var cmdThings = []cobra.Command{ cobra.Command{ Use: "create", diff --git a/sdk/go/channels.go b/sdk/go/channels.go index d47abac934..707aca0449 100644 --- a/sdk/go/channels.go +++ b/sdk/go/channels.go @@ -32,14 +32,10 @@ func (sdk mfSDK) CreateChannel(channel Channel, token string) (string, error) { } if resp.StatusCode != http.StatusCreated { - switch resp.StatusCode { - case http.StatusBadRequest: - return "", ErrInvalidArgs - case http.StatusForbidden: - return "", ErrUnauthorized - default: - return "", ErrFailedCreation + if err := encodeError(resp.StatusCode); err != nil { + return "", err } + return "", ErrFailedCreation } id := strings.TrimPrefix(resp.Header.Get("Location"), fmt.Sprintf("/%s/", channelsEndpoint)) @@ -67,14 +63,10 @@ func (sdk mfSDK) CreateChannels(channels []Channel, token string) ([]Channel, er defer resp.Body.Close() if resp.StatusCode != http.StatusCreated { - switch resp.StatusCode { - case http.StatusBadRequest: - return []Channel{}, ErrInvalidArgs - case http.StatusForbidden: - return []Channel{}, ErrUnauthorized - default: - return []Channel{}, ErrFailedCreation + if err := encodeError(resp.StatusCode); err != nil { + return []Channel{}, err } + return []Channel{}, ErrFailedCreation } body, err := ioutil.ReadAll(resp.Body) @@ -82,12 +74,12 @@ func (sdk mfSDK) CreateChannels(channels []Channel, token string) ([]Channel, er return []Channel{}, err } - var p createChannelsRes - if err := json.Unmarshal(body, &p); err != nil { + var ccr createChannelsRes + if err := json.Unmarshal(body, &ccr); err != nil { return []Channel{}, err } - return p.Channels, nil + return ccr.Channels, nil } func (sdk mfSDK) Channels(token string, offset, limit uint64, name string) (ChannelsPage, error) { @@ -111,14 +103,10 @@ func (sdk mfSDK) Channels(token string, offset, limit uint64, name string) (Chan } if resp.StatusCode != http.StatusOK { - switch resp.StatusCode { - case http.StatusBadRequest: - return ChannelsPage{}, ErrInvalidArgs - case http.StatusForbidden: - return ChannelsPage{}, ErrUnauthorized - default: - return ChannelsPage{}, ErrFetchFailed + if err := encodeError(resp.StatusCode); err != nil { + return ChannelsPage{}, err } + return ChannelsPage{}, ErrFetchFailed } var cp ChannelsPage @@ -150,14 +138,10 @@ func (sdk mfSDK) ChannelsByThing(token, thingID string, offset, limit uint64) (C } if resp.StatusCode != http.StatusOK { - switch resp.StatusCode { - case http.StatusBadRequest: - return ChannelsPage{}, ErrInvalidArgs - case http.StatusForbidden: - return ChannelsPage{}, ErrUnauthorized - default: - return ChannelsPage{}, ErrFetchFailed + if err := encodeError(resp.StatusCode); err != nil { + return ChannelsPage{}, err } + return ChannelsPage{}, ErrFetchFailed } var cp ChannelsPage @@ -189,14 +173,10 @@ func (sdk mfSDK) Channel(id, token string) (Channel, error) { } if resp.StatusCode != http.StatusOK { - switch resp.StatusCode { - case http.StatusForbidden: - return Channel{}, ErrUnauthorized - case http.StatusNotFound: - return Channel{}, ErrNotFound - default: - return Channel{}, ErrFetchFailed + if err := encodeError(resp.StatusCode); err != nil { + return Channel{}, err } + return Channel{}, ErrFetchFailed } var c Channel @@ -227,16 +207,10 @@ func (sdk mfSDK) UpdateChannel(channel Channel, token string) error { } if resp.StatusCode != http.StatusOK { - switch resp.StatusCode { - case http.StatusBadRequest: - return ErrInvalidArgs - case http.StatusForbidden: - return ErrUnauthorized - case http.StatusNotFound: - return ErrNotFound - default: - return ErrFailedUpdate + if err := encodeError(resp.StatusCode); err != nil { + return err } + return ErrFailedUpdate } return nil @@ -257,14 +231,10 @@ func (sdk mfSDK) DeleteChannel(id, token string) error { } if resp.StatusCode != http.StatusNoContent { - switch resp.StatusCode { - case http.StatusBadRequest: - return ErrInvalidArgs - case http.StatusForbidden: - return ErrUnauthorized - default: - return ErrFailedUpdate + if err := encodeError(resp.StatusCode); err != nil { + return err } + return ErrFailedUpdate } return nil diff --git a/sdk/go/message.go b/sdk/go/message.go index 402508be76..83b0e5cc61 100644 --- a/sdk/go/message.go +++ b/sdk/go/message.go @@ -34,14 +34,10 @@ func (sdk mfSDK) SendMessage(chanName, msg, token string) error { } if resp.StatusCode != http.StatusAccepted { - switch resp.StatusCode { - case http.StatusBadRequest: - return ErrInvalidArgs - case http.StatusForbidden: - return ErrUnauthorized - default: - return ErrFailedPublish + if err := encodeError(resp.StatusCode); err != nil { + return err } + return ErrFailedPublish } return nil @@ -74,17 +70,13 @@ func (sdk mfSDK) ReadMessages(chanName, token string) (MessagesPage, error) { } if resp.StatusCode != http.StatusOK { - switch resp.StatusCode { - case http.StatusBadRequest: - return MessagesPage{}, ErrInvalidArgs - case http.StatusForbidden: - return MessagesPage{}, ErrUnauthorized - default: - return MessagesPage{}, ErrFailedRead + if err := encodeError(resp.StatusCode); err != nil { + return MessagesPage{}, err } + return MessagesPage{}, ErrFailedRead } - mp := MessagesPage{} + var mp MessagesPage if err := json.Unmarshal(body, &mp); err != nil { return MessagesPage{}, err } diff --git a/sdk/go/sdk.go b/sdk/go/sdk.go index 0e7ba39ecd..78851412a5 100644 --- a/sdk/go/sdk.go +++ b/sdk/go/sdk.go @@ -240,3 +240,18 @@ func createURL(baseURL, prefix, endpoint string) string { return fmt.Sprintf("%s/%s/%s", baseURL, prefix, endpoint) } + +func encodeError(statusCode int) error { + switch statusCode { + case http.StatusBadRequest: + return ErrInvalidArgs + case http.StatusForbidden: + return ErrUnauthorized + case http.StatusNotFound: + return ErrNotFound + case http.StatusConflict: + return ErrConflict + default: + return nil + } +} diff --git a/sdk/go/things.go b/sdk/go/things.go index 2b3b0f39de..cdcdd6c8a9 100644 --- a/sdk/go/things.go +++ b/sdk/go/things.go @@ -35,14 +35,10 @@ func (sdk mfSDK) CreateThing(thing Thing, token string) (string, error) { defer resp.Body.Close() if resp.StatusCode != http.StatusCreated { - switch resp.StatusCode { - case http.StatusBadRequest: - return "", ErrInvalidArgs - case http.StatusForbidden: - return "", ErrUnauthorized - default: - return "", ErrFailedCreation + if err := encodeError(resp.StatusCode); err != nil { + return "", err } + return "", ErrFailedCreation } id := strings.TrimPrefix(resp.Header.Get("Location"), fmt.Sprintf("/%s/", thingsEndpoint)) @@ -70,14 +66,10 @@ func (sdk mfSDK) CreateThings(things []Thing, token string) ([]Thing, error) { defer resp.Body.Close() if resp.StatusCode != http.StatusCreated { - switch resp.StatusCode { - case http.StatusBadRequest: - return []Thing{}, ErrInvalidArgs - case http.StatusForbidden: - return []Thing{}, ErrUnauthorized - default: - return []Thing{}, ErrFailedCreation + if err := encodeError(resp.StatusCode); err != nil { + return []Thing{}, err } + return []Thing{}, ErrFailedCreation } body, err := ioutil.ReadAll(resp.Body) @@ -85,12 +77,12 @@ func (sdk mfSDK) CreateThings(things []Thing, token string) ([]Thing, error) { return []Thing{}, err } - var p createThingsRes - if err := json.Unmarshal(body, &p); err != nil { + var ctr createThingsRes + if err := json.Unmarshal(body, &ctr); err != nil { return []Thing{}, err } - return p.Things, nil + return ctr.Things, nil } func (sdk mfSDK) Things(token string, offset, limit uint64, name string) (ThingsPage, error) { @@ -114,14 +106,10 @@ func (sdk mfSDK) Things(token string, offset, limit uint64, name string) (Things } if resp.StatusCode != http.StatusOK { - switch resp.StatusCode { - case http.StatusBadRequest: - return ThingsPage{}, ErrInvalidArgs - case http.StatusForbidden: - return ThingsPage{}, ErrUnauthorized - default: - return ThingsPage{}, ErrFetchFailed + if err := encodeError(resp.StatusCode); err != nil { + return ThingsPage{}, err } + return ThingsPage{}, ErrFetchFailed } var tp ThingsPage @@ -153,14 +141,10 @@ func (sdk mfSDK) ThingsByChannel(token, chanID string, offset, limit uint64) (Th } if resp.StatusCode != http.StatusOK { - switch resp.StatusCode { - case http.StatusBadRequest: - return ThingsPage{}, ErrInvalidArgs - case http.StatusForbidden: - return ThingsPage{}, ErrUnauthorized - default: - return ThingsPage{}, ErrFetchFailed + if err := encodeError(resp.StatusCode); err != nil { + return ThingsPage{}, err } + return ThingsPage{}, ErrFetchFailed } var tp ThingsPage @@ -192,14 +176,10 @@ func (sdk mfSDK) Thing(id, token string) (Thing, error) { } if resp.StatusCode != http.StatusOK { - switch resp.StatusCode { - case http.StatusForbidden: - return Thing{}, ErrUnauthorized - case http.StatusNotFound: - return Thing{}, ErrNotFound - default: - return Thing{}, ErrFetchFailed + if err := encodeError(resp.StatusCode); err != nil { + return Thing{}, err } + return Thing{}, ErrFetchFailed } var t Thing @@ -230,16 +210,10 @@ func (sdk mfSDK) UpdateThing(thing Thing, token string) error { } if resp.StatusCode != http.StatusOK { - switch resp.StatusCode { - case http.StatusBadRequest: - return ErrInvalidArgs - case http.StatusForbidden: - return ErrUnauthorized - case http.StatusNotFound: - return ErrNotFound - default: - return ErrFailedUpdate + if err := encodeError(resp.StatusCode); err != nil { + return err } + return ErrFailedUpdate } return nil @@ -260,14 +234,10 @@ func (sdk mfSDK) DeleteThing(id, token string) error { } if resp.StatusCode != http.StatusNoContent { - switch resp.StatusCode { - case http.StatusForbidden: - return ErrUnauthorized - case http.StatusBadRequest: - return ErrInvalidArgs - default: - return ErrFailedRemoval + if err := encodeError(resp.StatusCode); err != nil { + return err } + return ErrFailedRemoval } return nil @@ -291,14 +261,10 @@ func (sdk mfSDK) Connect(connIDs ConnectionIDs, token string) error { } if resp.StatusCode != http.StatusOK { - switch resp.StatusCode { - case http.StatusForbidden: - return ErrUnauthorized - case http.StatusNotFound: - return ErrNotFound - default: - return ErrFailedConnection + if err := encodeError(resp.StatusCode); err != nil { + return err } + return ErrFailedConnection } return nil @@ -319,14 +285,10 @@ func (sdk mfSDK) DisconnectThing(thingID, chanID, token string) error { } if resp.StatusCode != http.StatusNoContent { - switch resp.StatusCode { - case http.StatusForbidden: - return ErrUnauthorized - case http.StatusNotFound: - return ErrNotFound - default: - return ErrFailedDisconnect + if err := encodeError(resp.StatusCode); err != nil { + return err } + return ErrFailedDisconnect } return nil diff --git a/sdk/go/things_test.go b/sdk/go/things_test.go index 7e74191845..c805b5a5ba 100644 --- a/sdk/go/things_test.go +++ b/sdk/go/things_test.go @@ -665,14 +665,14 @@ func TestConnectThing(t *testing.T) { thingID: thingID, chanID: "", token: token, - err: sdk.ErrFailedConnection, + err: sdk.ErrInvalidArgs, }, { desc: "connect thing with invalid ID to existing channel", thingID: "", chanID: chanID1, token: token, - err: sdk.ErrFailedConnection, + err: sdk.ErrInvalidArgs, }, { @@ -769,14 +769,14 @@ func TestConnect(t *testing.T) { thingID: thingID, chanID: emptyValue, token: token, - err: sdk.ErrFailedConnection, + err: sdk.ErrInvalidArgs, }, { desc: "connect things with invalid ID to existing channels", thingID: emptyValue, chanID: chanID1, token: token, - err: sdk.ErrFailedConnection, + err: sdk.ErrInvalidArgs, }, { @@ -881,14 +881,14 @@ func TestDisconnectThing(t *testing.T) { thingID: thingID, chanID: "", token: token, - err: sdk.ErrFailedDisconnect, + err: sdk.ErrInvalidArgs, }, { desc: "disconnect thing with invalid ID from existing channel", thingID: "", chanID: chanID1, token: token, - err: sdk.ErrFailedDisconnect, + err: sdk.ErrInvalidArgs, }, { desc: "disconnect existing thing from existing channel with invalid token", diff --git a/sdk/go/users.go b/sdk/go/users.go index ee06074f57..7050ee94e7 100644 --- a/sdk/go/users.go +++ b/sdk/go/users.go @@ -28,14 +28,10 @@ func (sdk mfSDK) CreateUser(user User) error { } if resp.StatusCode != http.StatusCreated { - switch resp.StatusCode { - case http.StatusBadRequest: - return ErrInvalidArgs - case http.StatusConflict: - return ErrConflict - default: - return ErrFailedCreation + if err := encodeError(resp.StatusCode); err != nil { + return err } + return ErrFailedCreation } return nil @@ -61,12 +57,10 @@ func (sdk mfSDK) User(token string) (User, error) { } if resp.StatusCode != http.StatusOK { - switch resp.StatusCode { - case http.StatusForbidden: - return User{}, ErrUnauthorized - default: - return User{}, ErrFetchFailed + if err := encodeError(resp.StatusCode); err != nil { + return User{}, err } + return User{}, ErrFetchFailed } var u User @@ -97,22 +91,18 @@ func (sdk mfSDK) CreateToken(user User) (string, error) { } if resp.StatusCode != http.StatusCreated { - switch resp.StatusCode { - case http.StatusBadRequest: - return "", ErrInvalidArgs - case http.StatusForbidden: - return "", ErrUnauthorized - default: - return "", ErrFailedCreation + if err := encodeError(resp.StatusCode); err != nil { + return "", err } + return "", ErrFailedCreation } - var t tokenRes - if err := json.Unmarshal(body, &t); err != nil { + var tr tokenRes + if err := json.Unmarshal(body, &tr); err != nil { return "", err } - return t.Token, nil + return tr.Token, nil } func (sdk mfSDK) UpdateUser(user User, token string) error { @@ -134,12 +124,10 @@ func (sdk mfSDK) UpdateUser(user User, token string) error { } if resp.StatusCode != http.StatusOK { - switch resp.StatusCode { - case http.StatusForbidden: - return ErrUnauthorized - default: - return ErrFailedUpdate + if err := encodeError(resp.StatusCode); err != nil { + return err } + return ErrFailedUpdate } return nil @@ -168,14 +156,10 @@ func (sdk mfSDK) UpdatePassword(oldPass, newPass, token string) error { } if resp.StatusCode != http.StatusCreated { - switch resp.StatusCode { - case http.StatusBadRequest: - return ErrInvalidArgs - case http.StatusForbidden: - return ErrUnauthorized - default: - return ErrFailedUpdate + if err := encodeError(resp.StatusCode); err != nil { + return err } + return ErrFailedUpdate } return nil