1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-27 13:48:49 +08:00

NOISSUE - Fix users CLI (#1062)

* NOISSSUE - Fix users CLI

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Fix README

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* use Mainflux entities

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Mv ConnectionIDs to requests

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
This commit is contained in:
Manuel Imperiale 2020-03-09 11:31:19 +01:00 committed by GitHub
parent 6e4e5b351a
commit cc5e0288df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 66 additions and 63 deletions

View File

@ -15,12 +15,12 @@ mainflux-cli version
### Users management
#### Create User
```
mainflux-cli users create john.doe@email.com password
mainflux-cli users create <user_email> <user_password>
```
#### Login User
```
mainflux-cli users token john.doe@email.com password
mainflux-cli users token <user_email> <user_password>
```
#### Retrieve User
@ -28,9 +28,9 @@ mainflux-cli users token john.doe@email.com password
mainflux-cli users get <user_auth_token>
```
#### Update User
#### Update User Metadata
```
mainflux-cli users update '{"metadata":{"field1":"value1"}}' <user_auth_token>
mainflux-cli users update '{"key1":"value1", "key2":"value2"}' <user_auth_token>
```
#### Update User Password
@ -121,3 +121,8 @@ mainflux-cli channels connections <channel_id> <user_auth_token>
```
mainflux-cli messages send <channel_id> '[{"bn":"Dev1","n":"temp","v":20}, {"n":"hum","v":40}, {"bn":"Dev2", "n":"temp","v":20}, {"n":"hum","v":40}]' <thing_auth_token>
```
#### Read messages over HTTP
```
mainflux-cli messages read <channel_id> <thing_auth_token>
```

View File

@ -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]")
},
}

16
sdk/go/requests.go Normal file
View File

@ -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"`
}

View File

@ -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
}

View File

@ -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)

View File

@ -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
}