2019-10-07 22:14:47 +08:00
|
|
|
// Copyright (c) Mainflux
|
2018-10-14 22:44:21 +08:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package sdk
|
|
|
|
|
|
|
|
import (
|
2018-10-24 17:21:03 +08:00
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
2018-10-14 22:44:21 +08:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2019-02-06 20:57:12 +08:00
|
|
|
|
|
|
|
"github.com/mainflux/mainflux"
|
2018-10-24 17:21:03 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// CTJSON represents JSON content type.
|
|
|
|
CTJSON ContentType = "application/json"
|
|
|
|
|
|
|
|
// CTJSONSenML represents JSON SenML content type.
|
|
|
|
CTJSONSenML ContentType = "application/senml+json"
|
2018-10-14 22:44:21 +08:00
|
|
|
|
2018-10-24 17:21:03 +08:00
|
|
|
// CTBinary represents binary content type.
|
|
|
|
CTBinary ContentType = "application/octet-stream"
|
2018-10-14 22:44:21 +08:00
|
|
|
)
|
|
|
|
|
2018-10-24 17:21:03 +08:00
|
|
|
var (
|
|
|
|
// ErrConflict indicates that create or update of entity failed because
|
|
|
|
// entity with same name already exists.
|
|
|
|
ErrConflict = errors.New("entity already exists")
|
|
|
|
|
|
|
|
// ErrFailedCreation indicates that entity creation failed.
|
|
|
|
ErrFailedCreation = errors.New("failed to create entity")
|
|
|
|
|
|
|
|
// ErrFailedUpdate indicates that entity update failed.
|
|
|
|
ErrFailedUpdate = errors.New("failed to update entity")
|
|
|
|
|
2018-11-20 04:27:01 +08:00
|
|
|
// ErrFailedPublish indicates that publishing message failed.
|
|
|
|
ErrFailedPublish = errors.New("failed to publish message")
|
|
|
|
|
2018-12-19 05:04:34 +08:00
|
|
|
// ErrFailedRead indicates that read messages failed.
|
|
|
|
ErrFailedRead = errors.New("failed to read messages")
|
|
|
|
|
2018-10-24 17:21:03 +08:00
|
|
|
// ErrFailedRemoval indicates that entity removal failed.
|
|
|
|
ErrFailedRemoval = errors.New("failed to remove entity")
|
|
|
|
|
|
|
|
// ErrFailedConnection indicates that connecting thing to channel failed.
|
|
|
|
ErrFailedConnection = errors.New("failed to connect thing to channel")
|
|
|
|
|
|
|
|
// ErrFailedDisconnect indicates that disconnecting thing from a channel failed.
|
2018-11-20 04:27:01 +08:00
|
|
|
ErrFailedDisconnect = errors.New("failed to disconnect thing from channel")
|
2018-10-24 17:21:03 +08:00
|
|
|
|
|
|
|
// ErrInvalidArgs indicates that invalid argument was passed.
|
|
|
|
ErrInvalidArgs = errors.New("invalid argument passed")
|
|
|
|
|
|
|
|
// ErrFetchFailed indicates that fetching of entity data failed.
|
|
|
|
ErrFetchFailed = errors.New("failed to fetch entity")
|
|
|
|
|
|
|
|
// ErrUnauthorized indicates unauthorized access.
|
|
|
|
ErrUnauthorized = errors.New("unauthorized access")
|
|
|
|
|
|
|
|
// ErrNotFound indicates that entity doesn't exist.
|
|
|
|
ErrNotFound = errors.New("entity not found")
|
|
|
|
|
|
|
|
// ErrInvalidContentType indicates that nonexistent message content type
|
|
|
|
// was passed.
|
|
|
|
ErrInvalidContentType = errors.New("Unknown Content Type")
|
|
|
|
)
|
|
|
|
|
|
|
|
// ContentType represents all possible content types.
|
|
|
|
type ContentType string
|
|
|
|
|
|
|
|
var _ SDK = (*mfSDK)(nil)
|
|
|
|
|
|
|
|
// User represents mainflux user its credentials.
|
|
|
|
type User struct {
|
|
|
|
Email string `json:"email"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Thing represents mainflux thing.
|
|
|
|
type Thing struct {
|
2019-04-16 20:58:56 +08:00
|
|
|
ID string `json:"id,omitempty"`
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Key string `json:"key,omitempty"`
|
|
|
|
Metadata map[string]interface{} `json:"metadata,omitempty"`
|
2018-10-24 17:21:03 +08:00
|
|
|
}
|
|
|
|
|
2019-01-08 18:53:24 +08:00
|
|
|
// 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"`
|
|
|
|
}
|
|
|
|
|
2018-10-24 17:21:03 +08:00
|
|
|
// Channel represents mainflux channel.
|
|
|
|
type Channel struct {
|
2019-04-16 20:58:56 +08:00
|
|
|
ID string `json:"id,omitempty"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Metadata map[string]interface{} `json:"metadata,omitempty"`
|
2019-01-08 18:53:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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"`
|
2018-10-24 17:21:03 +08:00
|
|
|
}
|
2018-10-14 22:44:21 +08:00
|
|
|
|
2019-05-07 21:10:02 +08:00
|
|
|
// 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 []mainflux.Message `json:"messages,omitempty"`
|
|
|
|
}
|
|
|
|
|
2019-11-21 19:29:37 +08:00
|
|
|
// ConnectionIDs contains ID lists of things and channels to be connected
|
|
|
|
type ConnectionIDs struct {
|
|
|
|
ChannelIDs []string `json:"channel_ids"`
|
|
|
|
ThingIDs []string `json:"thing_ids"`
|
|
|
|
}
|
|
|
|
|
2018-10-24 17:21:03 +08:00
|
|
|
// SDK contains Mainflux API.
|
2018-10-14 22:44:21 +08:00
|
|
|
type SDK interface {
|
2018-10-24 17:21:03 +08:00
|
|
|
// CreateUser registers mainflux user.
|
|
|
|
CreateUser(user User) error
|
|
|
|
|
|
|
|
// CreateToken receives credentials and returns user token.
|
|
|
|
CreateToken(user User) (string, error)
|
|
|
|
|
|
|
|
// CreateThing registers new thing and returns its id.
|
|
|
|
CreateThing(thing Thing, token string) (string, error)
|
|
|
|
|
2019-10-29 19:59:54 +08:00
|
|
|
// CreateThings registers new things and returns their ids.
|
|
|
|
CreateThings(things []Thing, token string) ([]Thing, error)
|
|
|
|
|
2018-10-24 17:21:03 +08:00
|
|
|
// Things returns page of things.
|
2019-05-30 21:33:49 +08:00
|
|
|
Things(token string, offset, limit uint64, name string) (ThingsPage, error)
|
2019-01-08 18:53:24 +08:00
|
|
|
|
|
|
|
// ThingsByChannel returns page of things that are connected to specified
|
|
|
|
// channel.
|
|
|
|
ThingsByChannel(token, chanID string, offset, limit uint64) (ThingsPage, error)
|
2018-10-24 17:21:03 +08:00
|
|
|
|
|
|
|
// Thing returns thing object by id.
|
|
|
|
Thing(id, token string) (Thing, error)
|
|
|
|
|
|
|
|
// UpdateThing updates existing thing.
|
|
|
|
UpdateThing(thing Thing, token string) error
|
|
|
|
|
|
|
|
// DeleteThing removes existing thing.
|
2018-10-14 22:44:21 +08:00
|
|
|
DeleteThing(id, token string) error
|
2018-10-24 17:21:03 +08:00
|
|
|
|
|
|
|
// ConnectThing connects thing to specified channel by id.
|
2018-10-14 22:44:21 +08:00
|
|
|
ConnectThing(thingID, chanID, token string) error
|
2018-10-24 17:21:03 +08:00
|
|
|
|
2019-11-21 19:29:37 +08:00
|
|
|
// Connect bulk connects things to channels specified by id.
|
|
|
|
Connect(conns ConnectionIDs, token string) error
|
|
|
|
|
2018-10-24 17:21:03 +08:00
|
|
|
// DisconnectThing disconnect thing from specified channel by id.
|
2018-10-14 22:44:21 +08:00
|
|
|
DisconnectThing(thingID, chanID, token string) error
|
|
|
|
|
2018-10-24 17:21:03 +08:00
|
|
|
// CreateChannel creates new channel and returns its id.
|
|
|
|
CreateChannel(channel Channel, token string) (string, error)
|
|
|
|
|
2019-10-29 19:59:54 +08:00
|
|
|
// CreateChannels registers new channels and returns their ids.
|
|
|
|
CreateChannels(channels []Channel, token string) ([]Channel, error)
|
|
|
|
|
2018-10-24 17:21:03 +08:00
|
|
|
// Channels returns page of channels.
|
2019-06-11 16:37:25 +08:00
|
|
|
Channels(token string, offset, limit uint64, name string) (ChannelsPage, error)
|
2019-01-08 18:53:24 +08:00
|
|
|
|
|
|
|
// ChannelsByThing returns page of channels that are connected to specified
|
|
|
|
// thing.
|
|
|
|
ChannelsByThing(token, thingID string, offset, limit uint64) (ChannelsPage, error)
|
2018-10-24 17:21:03 +08:00
|
|
|
|
|
|
|
// Channel returns channel data by id.
|
|
|
|
Channel(id, token string) (Channel, error)
|
|
|
|
|
|
|
|
// UpdateChannel updates existing channel.
|
|
|
|
UpdateChannel(channel Channel, token string) error
|
|
|
|
|
|
|
|
// DeleteChannel removes existing channel.
|
2018-10-14 22:44:21 +08:00
|
|
|
DeleteChannel(id, token string) error
|
|
|
|
|
2018-10-24 17:21:03 +08:00
|
|
|
// SendMessage send message to specified channel.
|
|
|
|
SendMessage(chanID, msg, token string) error
|
|
|
|
|
2019-02-06 20:57:12 +08:00
|
|
|
// ReadMessages read messages of specified channel.
|
2019-05-07 21:10:02 +08:00
|
|
|
ReadMessages(chanID, token string) (MessagesPage, error)
|
2018-12-19 05:04:34 +08:00
|
|
|
|
2018-10-24 17:21:03 +08:00
|
|
|
// SetContentType sets message content type.
|
|
|
|
SetContentType(ct ContentType) error
|
|
|
|
|
|
|
|
// Version returns used mainflux version.
|
|
|
|
Version() (string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type mfSDK struct {
|
2018-12-19 05:04:34 +08:00
|
|
|
baseURL string
|
|
|
|
readerURL string
|
|
|
|
readerPrefix string
|
2018-10-24 17:21:03 +08:00
|
|
|
usersPrefix string
|
|
|
|
thingsPrefix string
|
2019-10-29 19:59:54 +08:00
|
|
|
channelsPrefix string
|
2018-10-24 17:21:03 +08:00
|
|
|
httpAdapterPrefix string
|
|
|
|
msgContentType ContentType
|
|
|
|
client *http.Client
|
2018-10-14 22:44:21 +08:00
|
|
|
}
|
|
|
|
|
2018-10-24 17:21:03 +08:00
|
|
|
// Config contains sdk configuration parameters.
|
|
|
|
type Config struct {
|
|
|
|
BaseURL string
|
2018-12-19 05:04:34 +08:00
|
|
|
ReaderURL string
|
|
|
|
ReaderPrefix string
|
2018-10-24 17:21:03 +08:00
|
|
|
UsersPrefix string
|
|
|
|
ThingsPrefix string
|
|
|
|
HTTPAdapterPrefix string
|
|
|
|
MsgContentType ContentType
|
|
|
|
TLSVerification bool
|
2018-10-14 22:44:21 +08:00
|
|
|
}
|
|
|
|
|
2018-10-24 17:21:03 +08:00
|
|
|
// NewSDK returns new mainflux SDK instance.
|
|
|
|
func NewSDK(conf Config) SDK {
|
|
|
|
return &mfSDK{
|
2018-12-19 05:04:34 +08:00
|
|
|
baseURL: conf.BaseURL,
|
|
|
|
readerURL: conf.ReaderURL,
|
|
|
|
readerPrefix: conf.ReaderPrefix,
|
2018-10-24 17:21:03 +08:00
|
|
|
usersPrefix: conf.UsersPrefix,
|
|
|
|
thingsPrefix: conf.ThingsPrefix,
|
|
|
|
httpAdapterPrefix: conf.HTTPAdapterPrefix,
|
|
|
|
msgContentType: conf.MsgContentType,
|
|
|
|
client: &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: !conf.TLSVerification,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-10-14 22:44:21 +08:00
|
|
|
}
|
2018-10-24 17:21:03 +08:00
|
|
|
}
|
2018-10-14 22:44:21 +08:00
|
|
|
|
2018-10-24 17:21:03 +08:00
|
|
|
func (sdk mfSDK) sendRequest(req *http.Request, token, contentType string) (*http.Response, error) {
|
|
|
|
if token != "" {
|
|
|
|
req.Header.Set("Authorization", token)
|
2018-10-14 22:44:21 +08:00
|
|
|
}
|
|
|
|
|
2018-10-24 17:21:03 +08:00
|
|
|
if contentType != "" {
|
|
|
|
req.Header.Add("Content-Type", contentType)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sdk.client.Do(req)
|
2018-10-14 22:44:21 +08:00
|
|
|
}
|
|
|
|
|
2018-10-24 17:21:03 +08:00
|
|
|
func createURL(baseURL, prefix, endpoint string) string {
|
|
|
|
if prefix == "" {
|
|
|
|
return fmt.Sprintf("%s/%s", baseURL, endpoint)
|
|
|
|
}
|
2018-10-14 22:44:21 +08:00
|
|
|
|
2018-10-24 17:21:03 +08:00
|
|
|
return fmt.Sprintf("%s/%s/%s", baseURL, prefix, endpoint)
|
2018-10-14 22:44:21 +08:00
|
|
|
}
|