Skip to content

Commit

Permalink
NOISSUE - Fix users CLI (#1062)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
manuio committed Mar 9, 2020
1 parent 3967e7d commit a452ade
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 63 deletions.
13 changes: 9 additions & 4 deletions cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ 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
```
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
Expand Down Expand Up @@ -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>
```
11 changes: 3 additions & 8 deletions cli/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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]")
},
}

Expand Down
16 changes: 16 additions & 0 deletions sdk/go/requests.go
Original file line number Diff line number Diff line change
@@ -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"`
}
26 changes: 26 additions & 0 deletions sdk/go/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

package sdk

import "github.com/mainflux/mainflux/transformers/senml"

type tokenRes struct {
Token string `json:"token,omitempty"`
}
Expand All @@ -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
}
55 changes: 6 additions & 49 deletions sdk/go/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions sdk/go/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit a452ade

Please sign in to comment.