mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-04 22:17:59 +08:00

* Update increment ID to UUID in things service Update increment ID to UUID for things and channels in things service and proto files. Also, update ID type from uint to string. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in http adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in ws adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in CoAP adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in normalizer service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in writer services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in reader services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in SDK Update increment ID to UUID in SDK. Update id type to string. Update tests. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in mqtt adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unnecessary case from influxdb reader Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update tests in order to increase code coverage Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update lora adapter to use string ID instead of unsigned int Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
251 lines
5.2 KiB
Go
251 lines
5.2 KiB
Go
//
|
|
// Copyright (c) 2018
|
|
// Mainflux
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
package sdk
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
const thingsEndpoint = "things"
|
|
|
|
func (sdk mfSDK) CreateThing(thing Thing, token string) (string, error) {
|
|
data, err := json.Marshal(thing)
|
|
if err != nil {
|
|
return "", ErrInvalidArgs
|
|
}
|
|
|
|
url := createURL(sdk.url, sdk.thingsPrefix, thingsEndpoint)
|
|
|
|
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(data))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
resp, err := sdk.sendRequest(req, token, string(CTJSON))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
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
|
|
}
|
|
}
|
|
|
|
id := strings.TrimPrefix(resp.Header.Get("Location"), fmt.Sprintf("/%s/", thingsEndpoint))
|
|
return id, nil
|
|
}
|
|
|
|
func (sdk mfSDK) Things(token string, offset, limit uint64) ([]Thing, error) {
|
|
endpoint := fmt.Sprintf("%s?offset=%d&limit=%d", thingsEndpoint, offset, limit)
|
|
url := createURL(sdk.url, sdk.thingsPrefix, endpoint)
|
|
|
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
resp, err := sdk.sendRequest(req, token, string(CTJSON))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
switch resp.StatusCode {
|
|
case http.StatusBadRequest:
|
|
return nil, ErrInvalidArgs
|
|
case http.StatusForbidden:
|
|
return nil, ErrUnauthorized
|
|
default:
|
|
return nil, ErrFetchFailed
|
|
}
|
|
}
|
|
|
|
var l listThingsRes
|
|
if err := json.Unmarshal(body, &l); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return l.Things, nil
|
|
}
|
|
|
|
func (sdk mfSDK) Thing(id, token string) (Thing, error) {
|
|
endpoint := fmt.Sprintf("%s/%s", thingsEndpoint, id)
|
|
url := createURL(sdk.url, sdk.thingsPrefix, endpoint)
|
|
|
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
|
if err != nil {
|
|
return Thing{}, err
|
|
}
|
|
|
|
resp, err := sdk.sendRequest(req, token, string(CTJSON))
|
|
if err != nil {
|
|
return Thing{}, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return Thing{}, err
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
switch resp.StatusCode {
|
|
case http.StatusForbidden:
|
|
return Thing{}, ErrUnauthorized
|
|
case http.StatusNotFound:
|
|
return Thing{}, ErrNotFound
|
|
default:
|
|
return Thing{}, ErrFetchFailed
|
|
}
|
|
}
|
|
|
|
var t Thing
|
|
if err := json.Unmarshal(body, &t); err != nil {
|
|
return Thing{}, err
|
|
}
|
|
|
|
return t, nil
|
|
}
|
|
|
|
func (sdk mfSDK) UpdateThing(thing Thing, token string) error {
|
|
data, err := json.Marshal(thing)
|
|
if err != nil {
|
|
return ErrInvalidArgs
|
|
}
|
|
|
|
endpoint := fmt.Sprintf("%s/%s", thingsEndpoint, thing.ID)
|
|
url := createURL(sdk.url, sdk.thingsPrefix, endpoint)
|
|
|
|
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 {
|
|
switch resp.StatusCode {
|
|
case http.StatusBadRequest:
|
|
return ErrInvalidArgs
|
|
case http.StatusForbidden:
|
|
return ErrUnauthorized
|
|
case http.StatusNotFound:
|
|
return ErrNotFound
|
|
default:
|
|
return ErrFailedUpdate
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (sdk mfSDK) DeleteThing(id, token string) error {
|
|
endpoint := fmt.Sprintf("%s/%s", thingsEndpoint, id)
|
|
url := createURL(sdk.url, sdk.thingsPrefix, endpoint)
|
|
|
|
req, err := http.NewRequest(http.MethodDelete, url, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
resp, err := sdk.sendRequest(req, token, string(CTJSON))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusNoContent {
|
|
switch resp.StatusCode {
|
|
case http.StatusForbidden:
|
|
return ErrUnauthorized
|
|
case http.StatusBadRequest:
|
|
return ErrInvalidArgs
|
|
default:
|
|
return ErrFailedRemoval
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (sdk mfSDK) ConnectThing(thingID, chanID, token string) error {
|
|
endpoint := fmt.Sprintf("%s/%s/%s/%s", channelsEndpoint, chanID, thingsEndpoint, thingID)
|
|
url := createURL(sdk.url, sdk.thingsPrefix, endpoint)
|
|
|
|
req, err := http.NewRequest(http.MethodPut, url, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
resp, err := sdk.sendRequest(req, token, string(CTJSON))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
switch resp.StatusCode {
|
|
case http.StatusForbidden:
|
|
return ErrUnauthorized
|
|
case http.StatusNotFound:
|
|
return ErrNotFound
|
|
default:
|
|
return ErrFailedConnection
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (sdk mfSDK) DisconnectThing(thingID, chanID, token string) error {
|
|
endpoint := fmt.Sprintf("%s/%s/%s/%s", channelsEndpoint, chanID, thingsEndpoint, thingID)
|
|
url := createURL(sdk.url, sdk.thingsPrefix, endpoint)
|
|
|
|
req, err := http.NewRequest(http.MethodDelete, url, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
resp, err := sdk.sendRequest(req, token, string(CTJSON))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusNoContent {
|
|
switch resp.StatusCode {
|
|
case http.StatusForbidden:
|
|
return ErrUnauthorized
|
|
case http.StatusNotFound:
|
|
return ErrNotFound
|
|
default:
|
|
return ErrFailedDisconnect
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|