diff --git a/cli/README.md b/cli/README.md index d3bcd1271d7..5640e4cb797 100644 --- a/cli/README.md +++ b/cli/README.md @@ -15,12 +15,12 @@ mainflux-cli version ### Users management #### Create User ``` -mainflux-cli users create john.doe@email.com password +mainflux-cli users create ``` #### Login User ``` -mainflux-cli users token john.doe@email.com password +mainflux-cli users token ``` #### Retrieve User @@ -28,9 +28,9 @@ mainflux-cli users token john.doe@email.com password mainflux-cli users get ``` -#### Update User +#### Update User Metadata ``` -mainflux-cli users update '{"metadata":{"field1":"value1"}}' +mainflux-cli users update '{"key1":"value1", "key2":"value2"}' ``` #### Update User Password @@ -121,3 +121,8 @@ mainflux-cli channels connections ``` mainflux-cli messages send '[{"bn":"Dev1","n":"temp","v":20}, {"n":"hum","v":40}, {"bn":"Dev2", "n":"temp","v":20}, {"n":"hum","v":40}]' ``` + +#### Read messages over HTTP +``` +mainflux-cli messages read +``` diff --git a/cli/users.go b/cli/users.go index 69f8dfdb6fb..297e44de5dd 100644 --- a/cli/users.go +++ b/cli/users.go @@ -86,7 +86,7 @@ var cmdUsers = []cobra.Command{ } var user mfxsdk.User - if err := json.Unmarshal([]byte(args[0]), &user); err != nil { + if err := json.Unmarshal([]byte(args[0]), &user.Metadata); err != nil { logError(err) return } @@ -109,12 +109,7 @@ var cmdUsers = []cobra.Command{ return } - user := mfxsdk.User{ - OldPassword: args[0], - Password: args[1], - } - - if err := sdk.UpdatePassword(user, args[2]); err != nil { + if err := sdk.UpdatePassword(args[0], args[1], args[2]); err != nil { logError(err) return } @@ -131,7 +126,7 @@ func NewUsersCmd() *cobra.Command { Short: "Users management", Long: `Users management: create accounts and tokens"`, Run: func(cmd *cobra.Command, args []string) { - logUsage("Usage: users [create | get | token | password]") + logUsage("Usage: users [create | get | update | token | password]") }, } diff --git a/sdk/go/requests.go b/sdk/go/requests.go new file mode 100644 index 00000000000..e4c59c2f53f --- /dev/null +++ b/sdk/go/requests.go @@ -0,0 +1,16 @@ +// Copyright (c) Mainflux +// SPDX-License-Identifier: Apache-2.0 + +package sdk + +// UserPasswordReq contains old and new passwords +type UserPasswordReq struct { + OldPassword string `json:"old_password,omitempty"` + Password string `json:"password,omitempty"` +} + +// ConnectionIDs contains ID lists of things and channels to be connected +type ConnectionIDs struct { + ChannelIDs []string `json:"channel_ids"` + ThingIDs []string `json:"thing_ids"` +} diff --git a/sdk/go/responses.go b/sdk/go/responses.go index ecd9f8a2174..ced197a2c95 100644 --- a/sdk/go/responses.go +++ b/sdk/go/responses.go @@ -3,6 +3,8 @@ package sdk +import "github.com/mainflux/mainflux/transformers/senml" + type tokenRes struct { Token string `json:"token,omitempty"` } @@ -14,3 +16,27 @@ type createThingsRes struct { type createChannelsRes struct { Channels []Channel `json:"channels"` } + +type pageRes struct { + Total uint64 `json:"total"` + Offset uint64 `json:"offset"` + Limit uint64 `json:"limit"` +} + +// ThingsPage contains list of things in a page with proper metadata. +type ThingsPage struct { + Things []Thing `json:"things"` + pageRes +} + +// ChannelsPage contains list of channels in a page with proper metadata. +type ChannelsPage struct { + Channels []Channel `json:"channels"` + pageRes +} + +// MessagesPage contains list of messages in a page with proper metadata. +type MessagesPage struct { + Messages []senml.Message `json:"messages,omitempty"` + pageRes +} diff --git a/sdk/go/sdk.go b/sdk/go/sdk.go index d480cb99dca..0e7ba39ecd6 100644 --- a/sdk/go/sdk.go +++ b/sdk/go/sdk.go @@ -9,7 +9,8 @@ import ( "fmt" "net/http" - "github.com/mainflux/mainflux/transformers/senml" + "github.com/mainflux/mainflux/things" + "github.com/mainflux/mainflux/users" ) const ( @@ -74,12 +75,7 @@ type ContentType string var _ SDK = (*mfSDK)(nil) // User represents mainflux user its credentials. -type User struct { - Email string `json:"email"` - Metadata map[string]interface{} `json:"metadata,omitempty"` - Password string `json:"password,omitempty"` - OldPassword string `json:"old_password,omitempty"` -} +type User users.User // Validate returns an error if user representation is invalid. func (u User) validate() error { @@ -96,49 +92,10 @@ func (u User) validate() error { } // Thing represents mainflux thing. -type Thing struct { - ID string `json:"id,omitempty"` - Name string `json:"name,omitempty"` - Key string `json:"key,omitempty"` - Metadata map[string]interface{} `json:"metadata,omitempty"` -} - -// ThingsPage contains list of things in a page with proper metadata. -type ThingsPage struct { - Things []Thing `json:"things"` - Total uint64 `json:"total"` - Offset uint64 `json:"offset"` - Limit uint64 `json:"limit"` -} +type Thing things.Thing // Channel represents mainflux channel. -type Channel struct { - ID string `json:"id,omitempty"` - Name string `json:"name"` - Metadata map[string]interface{} `json:"metadata,omitempty"` -} - -// ChannelsPage contains list of channels in a page with proper metadata. -type ChannelsPage struct { - Channels []Channel `json:"channels"` - Total uint64 `json:"total"` - Offset uint64 `json:"offset"` - Limit uint64 `json:"limit"` -} - -// MessagesPage contains list of messages in a page with proper metadata. -type MessagesPage struct { - Total uint64 `json:"total"` - Offset uint64 `json:"offset"` - Limit uint64 `json:"limit"` - Messages []senml.Message `json:"messages,omitempty"` -} - -// ConnectionIDs contains ID lists of things and channels to be connected -type ConnectionIDs struct { - ChannelIDs []string `json:"channel_ids"` - ThingIDs []string `json:"thing_ids"` -} +type Channel things.Channel // SDK contains Mainflux API. type SDK interface { @@ -155,7 +112,7 @@ type SDK interface { UpdateUser(user User, token string) error // UpdatePassword updates user password. - UpdatePassword(user User, token string) error + UpdatePassword(oldPass, newPass, token string) error // CreateThing registers new thing and returns its id. CreateThing(thing Thing, token string) (string, error) diff --git a/sdk/go/users.go b/sdk/go/users.go index 8883299705b..ee06074f57d 100644 --- a/sdk/go/users.go +++ b/sdk/go/users.go @@ -145,8 +145,12 @@ func (sdk mfSDK) UpdateUser(user User, token string) error { return nil } -func (sdk mfSDK) UpdatePassword(user User, token string) error { - data, err := json.Marshal(user) +func (sdk mfSDK) UpdatePassword(oldPass, newPass, token string) error { + ur := UserPasswordReq{ + OldPassword: oldPass, + Password: newPass, + } + data, err := json.Marshal(ur) if err != nil { return ErrInvalidArgs }