2019-10-07 08:14:47 -06:00
|
|
|
// Copyright (c) Mainflux
|
2018-10-14 16:44:21 +02:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package sdk
|
|
|
|
|
|
|
|
import (
|
2018-12-18 22:04:34 +01:00
|
|
|
"encoding/json"
|
2018-10-14 16:44:21 +02:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2020-04-15 23:41:31 +02:00
|
|
|
|
2022-12-15 07:24:19 -08:00
|
|
|
"github.com/mainflux/mainflux/internal/apiutil"
|
2020-06-03 15:16:19 +02:00
|
|
|
"github.com/mainflux/mainflux/pkg/errors"
|
2018-10-14 16:44:21 +02:00
|
|
|
)
|
|
|
|
|
2022-12-15 07:24:19 -08:00
|
|
|
func (sdk mfSDK) SendMessage(chanName, msg, key string) errors.SDKError {
|
2019-03-15 18:38:07 +01:00
|
|
|
chanNameParts := strings.SplitN(chanName, ".", 2)
|
|
|
|
chanID := chanNameParts[0]
|
|
|
|
subtopicPart := ""
|
|
|
|
if len(chanNameParts) == 2 {
|
|
|
|
subtopicPart = fmt.Sprintf("/%s", strings.Replace(chanNameParts[1], ".", "/", -1))
|
|
|
|
}
|
|
|
|
|
2022-06-13 12:04:48 +02:00
|
|
|
url := fmt.Sprintf("%s/channels/%s/messages/%s", sdk.httpAdapterURL, chanID, subtopicPart)
|
2018-10-14 16:44:21 +02:00
|
|
|
|
2022-12-15 07:24:19 -08:00
|
|
|
_, _, err := sdk.processRequest(http.MethodPost, url, apiutil.ThingPrefix+key, string(CTJSON), []byte(msg), http.StatusAccepted)
|
|
|
|
return err
|
2018-10-14 16:44:21 +02:00
|
|
|
}
|
|
|
|
|
2022-12-15 07:24:19 -08:00
|
|
|
func (sdk mfSDK) ReadMessages(chanName, token string) (MessagesPage, errors.SDKError) {
|
2019-03-15 18:38:07 +01:00
|
|
|
chanNameParts := strings.SplitN(chanName, ".", 2)
|
|
|
|
chanID := chanNameParts[0]
|
|
|
|
subtopicPart := ""
|
|
|
|
if len(chanNameParts) == 2 {
|
|
|
|
subtopicPart = fmt.Sprintf("?subtopic=%s", strings.Replace(chanNameParts[1], ".", "/", -1))
|
|
|
|
}
|
|
|
|
|
2021-08-11 16:58:10 +02:00
|
|
|
url := fmt.Sprintf("%s/channels/%s/messages%s", sdk.readerURL, chanID, subtopicPart)
|
2018-12-18 22:04:34 +01:00
|
|
|
|
2022-12-15 07:24:19 -08:00
|
|
|
_, body, err := sdk.processRequest(http.MethodGet, url, token, string(sdk.msgContentType), nil, http.StatusOK)
|
2018-12-18 22:04:34 +01:00
|
|
|
if err != nil {
|
2019-05-07 15:10:02 +02:00
|
|
|
return MessagesPage{}, err
|
2018-12-18 22:04:34 +01:00
|
|
|
}
|
|
|
|
|
2020-04-14 11:04:33 +02:00
|
|
|
var mp MessagesPage
|
2019-05-07 15:10:02 +02:00
|
|
|
if err := json.Unmarshal(body, &mp); err != nil {
|
2022-12-15 07:24:19 -08:00
|
|
|
return MessagesPage{}, errors.NewSDKError(err)
|
2018-12-18 22:04:34 +01:00
|
|
|
}
|
|
|
|
|
2020-03-07 05:02:26 +01:00
|
|
|
return mp, nil
|
2018-12-18 22:04:34 +01:00
|
|
|
}
|
|
|
|
|
2022-12-15 07:24:19 -08:00
|
|
|
func (sdk *mfSDK) SetContentType(ct ContentType) errors.SDKError {
|
2018-10-24 11:21:03 +02:00
|
|
|
if ct != CTJSON && ct != CTJSONSenML && ct != CTBinary {
|
2022-12-15 07:24:19 -08:00
|
|
|
return errors.NewSDKError(errors.ErrUnsupportedContentType)
|
2018-10-14 16:44:21 +02:00
|
|
|
}
|
|
|
|
|
2018-10-24 11:21:03 +02:00
|
|
|
sdk.msgContentType = ct
|
2018-10-14 16:44:21 +02:00
|
|
|
return nil
|
|
|
|
}
|