mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-29 13:49:28 +08:00

* Use per-service URL in SDK Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com> * Fix CLI Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com> * Fix CLI messaging Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com> * Fix message tests Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com> * Simplify Bootstrap Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com> * Update API doc and responses Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com> * fix failing certs, bootstrap tests Signed-off-by: mteodor <mirko.teodorovic@gmail.com> * fix failing certs, bootstrap tests Signed-off-by: mteodor <mirko.teodorovic@gmail.com> * Fix tests and rename to auth service Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com> * Clean the code Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com> * Remove unnecessary Repository logs Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Always return error in case of repo failure Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Cleanup SDK and CLI Update tests, remove linter warnings, remove dead code. Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Clean the code Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com> * Undo Bootstrap changes Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com> * Fix tests Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com> * Fix linter Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com> Co-authored-by: mteodor <mirko.teodorovic@gmail.com> Co-authored-by: dusanb94 <dusan.borovcanin@mainflux.com>
155 lines
3.1 KiB
Go
155 lines
3.1 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package sdk
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/mainflux/mainflux/pkg/errors"
|
|
)
|
|
|
|
const (
|
|
usersEndpoint = "users"
|
|
tokensEndpoint = "tokens"
|
|
passwordEndpoint = "password"
|
|
membersEndpoint = "members"
|
|
)
|
|
|
|
func (sdk mfSDK) CreateUser(u User) (string, error) {
|
|
data, err := json.Marshal(u)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
url := fmt.Sprintf("%s/%s", sdk.usersURL, usersEndpoint)
|
|
resp, err := sdk.client.Post(url, string(CTJSON), bytes.NewReader(data))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusCreated {
|
|
return "", errors.Wrap(ErrFailedCreation, errors.New(resp.Status))
|
|
}
|
|
|
|
id := strings.TrimPrefix(resp.Header.Get("Location"), fmt.Sprintf("/%s/", usersEndpoint))
|
|
return id, nil
|
|
}
|
|
|
|
func (sdk mfSDK) User(token string) (User, error) {
|
|
url := fmt.Sprintf("%s/%s", sdk.usersURL, usersEndpoint)
|
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
|
if err != nil {
|
|
return User{}, err
|
|
}
|
|
|
|
resp, err := sdk.sendRequest(req, token, string(CTJSON))
|
|
if err != nil {
|
|
return User{}, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return User{}, err
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return User{}, errors.Wrap(ErrFailedFetch, errors.New(resp.Status))
|
|
}
|
|
|
|
var u User
|
|
if err := json.Unmarshal(body, &u); err != nil {
|
|
return User{}, err
|
|
}
|
|
|
|
return u, nil
|
|
}
|
|
|
|
func (sdk mfSDK) CreateToken(user User) (string, error) {
|
|
data, err := json.Marshal(user)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
url := fmt.Sprintf("%s/%s", sdk.usersURL, tokensEndpoint)
|
|
resp, err := sdk.client.Post(url, string(CTJSON), bytes.NewReader(data))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusCreated {
|
|
return "", errors.Wrap(ErrFailedCreation, errors.New(resp.Status))
|
|
}
|
|
|
|
var tr tokenRes
|
|
if err := json.Unmarshal(body, &tr); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return tr.Token, nil
|
|
}
|
|
|
|
func (sdk mfSDK) UpdateUser(u User, token string) error {
|
|
data, err := json.Marshal(u)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
url := fmt.Sprintf("%s/%s", sdk.usersURL, usersEndpoint)
|
|
req, err := http.NewRequest(http.MethodPut, url, bytes.NewReader(data))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
resp, err := sdk.sendRequest(req, token, string(CTJSON))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return errors.Wrap(ErrFailedUpdate, errors.New(resp.Status))
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (sdk mfSDK) UpdatePassword(oldPass, newPass, token string) error {
|
|
ur := UserPasswordReq{
|
|
OldPassword: oldPass,
|
|
Password: newPass,
|
|
}
|
|
data, err := json.Marshal(ur)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
url := fmt.Sprintf("%s/%s", sdk.usersURL, passwordEndpoint)
|
|
req, err := http.NewRequest(http.MethodPatch, url, bytes.NewReader(data))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
resp, err := sdk.sendRequest(req, token, string(CTJSON))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusCreated {
|
|
return errors.Wrap(ErrFailedUpdate, errors.New(resp.Status))
|
|
}
|
|
|
|
return nil
|
|
}
|