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

* initial commit Signed-off-by: aryan <aryangodara03@gmail.com> * remove unused variables. Signed-off-by: aryan <aryangodara03@gmail.com> * removed temporarily created file. Signed-off-by: aryan <aryangodara03@gmail.com> * Fix failing CI Signed-off-by: aryan <aryangodara03@gmail.com> * Fix thing_test failing cases. Signed-off-by: aryan <aryangodara03@gmail.com> * Remove dead code, debug statements, and add comments. Signed-off-by: aryan <aryangodara03@gmail.com> * Extract errors to separate file. Signed-off-by: aryan <aryangodara03@gmail.com> * Updated things/api/http tests Signed-off-by: aryan <aryangodara03@gmail.com> * Created custom SDK error. Signed-off-by: aryan <aryangodara03@gmail.com> * Changed to using CheckError. All tests passing. Signed-off-by: aryan <aryangodara03@gmail.com> * Replace error interface with errors.SDKError interface. Signed-off-by: aryan <aryangodara03@gmail.com> * Fix failing CI. Signed-off-by: aryan <aryangodara03@gmail.com> * Remove unused sdk errors. Signed-off-by: aryan <aryangodara03@gmail.com> * Change SDKError to error in internal function of sdk package. Signed-off-by: aryan <aryangodara03@gmail.com> * Remove unused error. Signed-off-by: aryan <aryangodara03@gmail.com> * Remove encodeError. All tests working. Signed-off-by: aryan <aryangodara03@gmail.com> * Rename sdkerr vars, convert common strings to constants. Signed-off-by: aryan <aryangodara03@gmail.com> * Change checkerror to take error instead of string. Signed-off-by: aryan <aryangodara03@gmail.com> * Remove unused errors, and removed errfailedwhitelist wrap. Signed-off-by: aryan <aryangodara03@gmail.com> * Removed unused errors, and remove errors.go since it only had a repeated error from errors package Signed-off-by: aryan <aryangodara03@gmail.com> * Remove unused errors. Signed-off-by: aryan <aryangodara03@gmail.com> * Update sdk_error. Signed-off-by: aryan <aryangodara03@gmail.com> * Used function to reduce code for sending and receiving requests. Signed-off-by: aryan <aryangodara03@gmail.com> * Added function sendrequestandgetheadersorerror. Signed-off-by: aryan <aryangodara03@gmail.com> * sdk_error updated. Signed-off-by: aryan <aryangodara03@gmail.com> * Updated function names to processRequest. Signed-off-by: aryan <aryangodara03@gmail.com> * Made errors internal, fixed typo in http. Signed-off-by: aryan <aryangodara03@gmail.com> * Remove empty line. Signed-off-by: aryan <aryangodara03@gmail.com> * merged proceessBody and processHeaders functions in sdk. Signed-off-by: aryan <aryangodara03@gmail.com> * remove sendThingRequest function. Signed-off-by: aryan <aryangodara03@gmail.com> * changed processRequest signature Signed-off-by: aryan <aryangodara03@gmail.com> * changed processRequest signature, changed error names. Signed-off-by: aryan <aryangodara03@gmail.com> Signed-off-by: aryan <aryangodara03@gmail.com> Co-authored-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
177 lines
4.7 KiB
Go
177 lines
4.7 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package sdk
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/mainflux/mainflux/pkg/errors"
|
|
)
|
|
|
|
const (
|
|
thingsEndpoint = "things"
|
|
connectEndpoint = "connect"
|
|
identifyEndpoint = "identify"
|
|
)
|
|
|
|
type identifyThingReq struct {
|
|
Token string `json:"token,omitempty"`
|
|
}
|
|
|
|
type identifyThingResp struct {
|
|
ID string `json:"id,omitempty"`
|
|
}
|
|
|
|
func (sdk mfSDK) CreateThing(t Thing, token string) (string, errors.SDKError) {
|
|
data, err := json.Marshal(t)
|
|
if err != nil {
|
|
return "", errors.NewSDKError(err)
|
|
}
|
|
url := fmt.Sprintf("%s/%s", sdk.thingsURL, thingsEndpoint)
|
|
|
|
headers, _, sdkerr := sdk.processRequest(http.MethodPost, url, token, string(CTJSON), data, http.StatusCreated)
|
|
if sdkerr != nil {
|
|
return "", sdkerr
|
|
}
|
|
|
|
id := strings.TrimPrefix(headers.Get("Location"), fmt.Sprintf("/%s/", thingsEndpoint))
|
|
return id, nil
|
|
}
|
|
|
|
func (sdk mfSDK) CreateThings(things []Thing, token string) ([]Thing, errors.SDKError) {
|
|
data, err := json.Marshal(things)
|
|
if err != nil {
|
|
return []Thing{}, errors.NewSDKError(err)
|
|
}
|
|
|
|
url := fmt.Sprintf("%s/%s/%s", sdk.thingsURL, thingsEndpoint, "bulk")
|
|
|
|
_, body, sdkerr := sdk.processRequest(http.MethodPost, url, token, string(CTJSON), data, http.StatusCreated)
|
|
if sdkerr != nil {
|
|
return []Thing{}, sdkerr
|
|
}
|
|
|
|
var ctr createThingsRes
|
|
if err := json.Unmarshal(body, &ctr); err != nil {
|
|
return []Thing{}, errors.NewSDKError(err)
|
|
}
|
|
|
|
return ctr.Things, nil
|
|
}
|
|
|
|
func (sdk mfSDK) Things(token string, pm PageMetadata) (ThingsPage, errors.SDKError) {
|
|
url, err := sdk.withQueryParams(sdk.thingsURL, thingsEndpoint, pm)
|
|
|
|
if err != nil {
|
|
return ThingsPage{}, errors.NewSDKError(err)
|
|
}
|
|
|
|
_, body, sdkerr := sdk.processRequest(http.MethodGet, url, token, string(CTJSON), nil, http.StatusOK)
|
|
if sdkerr != nil {
|
|
return ThingsPage{}, sdkerr
|
|
}
|
|
|
|
var tp ThingsPage
|
|
if err := json.Unmarshal(body, &tp); err != nil {
|
|
return ThingsPage{}, errors.NewSDKError(err)
|
|
}
|
|
|
|
return tp, nil
|
|
}
|
|
|
|
func (sdk mfSDK) ThingsByChannel(token, chanID string, offset, limit uint64, disconn bool) (ThingsPage, errors.SDKError) {
|
|
url := fmt.Sprintf("%s/channels/%s/things?offset=%d&limit=%d&disconnected=%t", sdk.thingsURL, chanID, offset, limit, disconn)
|
|
|
|
_, body, err := sdk.processRequest(http.MethodGet, url, token, string(CTJSON), nil, http.StatusOK)
|
|
if err != nil {
|
|
return ThingsPage{}, err
|
|
}
|
|
|
|
var tp ThingsPage
|
|
if err := json.Unmarshal(body, &tp); err != nil {
|
|
return ThingsPage{}, errors.NewSDKError(err)
|
|
}
|
|
|
|
return tp, nil
|
|
}
|
|
|
|
func (sdk mfSDK) Thing(id, token string) (Thing, errors.SDKError) {
|
|
url := fmt.Sprintf("%s/%s/%s", sdk.thingsURL, thingsEndpoint, id)
|
|
|
|
_, body, err := sdk.processRequest(http.MethodGet, url, token, string(CTJSON), nil, http.StatusOK)
|
|
if err != nil {
|
|
return Thing{}, err
|
|
}
|
|
|
|
var t Thing
|
|
if err := json.Unmarshal(body, &t); err != nil {
|
|
return Thing{}, errors.NewSDKError(err)
|
|
}
|
|
|
|
return t, nil
|
|
}
|
|
|
|
func (sdk mfSDK) UpdateThing(t Thing, token string) errors.SDKError {
|
|
data, err := json.Marshal(t)
|
|
if err != nil {
|
|
return errors.NewSDKError(err)
|
|
}
|
|
|
|
url := fmt.Sprintf("%s/%s/%s", sdk.thingsURL, thingsEndpoint, t.ID)
|
|
|
|
_, _, sdkerr := sdk.processRequest(http.MethodPut, url, token, string(CTJSON), data, http.StatusOK)
|
|
return sdkerr
|
|
}
|
|
|
|
func (sdk mfSDK) DeleteThing(id, token string) errors.SDKError {
|
|
url := fmt.Sprintf("%s/%s/%s", sdk.thingsURL, thingsEndpoint, id)
|
|
|
|
_, _, err := sdk.processRequest(http.MethodDelete, url, token, string(CTJSON), nil, http.StatusNoContent)
|
|
return err
|
|
}
|
|
|
|
func (sdk mfSDK) IdentifyThing(key string) (string, errors.SDKError) {
|
|
idReq := identifyThingReq{Token: key}
|
|
data, err := json.Marshal(idReq)
|
|
if err != nil {
|
|
return "", errors.NewSDKError(err)
|
|
}
|
|
|
|
url := fmt.Sprintf("%s/%s", sdk.thingsURL, identifyEndpoint)
|
|
|
|
_, body, sdkerr := sdk.processRequest(http.MethodPost, url, "", string(CTJSON), data, http.StatusOK)
|
|
if sdkerr != nil {
|
|
return "", sdkerr
|
|
}
|
|
|
|
var i identifyThingResp
|
|
if err := json.Unmarshal(body, &i); err != nil {
|
|
return "", errors.NewSDKError(err)
|
|
}
|
|
|
|
return i.ID, nil
|
|
}
|
|
|
|
func (sdk mfSDK) Connect(connIDs ConnectionIDs, token string) errors.SDKError {
|
|
data, err := json.Marshal(connIDs)
|
|
if err != nil {
|
|
return errors.NewSDKError(err)
|
|
}
|
|
|
|
url := fmt.Sprintf("%s/%s", sdk.thingsURL, connectEndpoint)
|
|
|
|
_, _, sdkerr := sdk.processRequest(http.MethodPost, url, token, string(CTJSON), data, http.StatusOK)
|
|
return sdkerr
|
|
}
|
|
|
|
func (sdk mfSDK) DisconnectThing(thingID, chanID, token string) errors.SDKError {
|
|
url := fmt.Sprintf("%s/%s/%s/%s/%s", sdk.thingsURL, channelsEndpoint, chanID, thingsEndpoint, thingID)
|
|
|
|
_, _, err := sdk.processRequest(http.MethodDelete, url, token, string(CTJSON), nil, http.StatusNoContent)
|
|
return err
|
|
}
|