1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-29 13:49:28 +08:00
Drasko DRASKOVIC d73a5d53fe
MF 1413 - Use per-service URL in SDK (#1444)
* 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>
2021-08-11 16:58:10 +02:00

88 lines
2.0 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package sdk
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/mainflux/mainflux/pkg/errors"
)
func (sdk mfSDK) SendMessage(chanName, msg, token string) error {
chanNameParts := strings.SplitN(chanName, ".", 2)
chanID := chanNameParts[0]
subtopicPart := ""
if len(chanNameParts) == 2 {
subtopicPart = fmt.Sprintf("/%s", strings.Replace(chanNameParts[1], ".", "/", -1))
}
url := fmt.Sprintf("%s/channels/%s/messages%s", sdk.httpAdapterURL, chanID, subtopicPart)
req, err := http.NewRequest(http.MethodPost, url, strings.NewReader(msg))
if err != nil {
return err
}
resp, err := sdk.sendRequest(req, token, string(sdk.msgContentType))
if err != nil {
return err
}
if resp.StatusCode != http.StatusAccepted {
return errors.Wrap(ErrFailedPublish, errors.New(resp.Status))
}
return nil
}
func (sdk mfSDK) ReadMessages(chanName, token string) (MessagesPage, error) {
chanNameParts := strings.SplitN(chanName, ".", 2)
chanID := chanNameParts[0]
subtopicPart := ""
if len(chanNameParts) == 2 {
subtopicPart = fmt.Sprintf("?subtopic=%s", strings.Replace(chanNameParts[1], ".", "/", -1))
}
url := fmt.Sprintf("%s/channels/%s/messages%s", sdk.readerURL, chanID, subtopicPart)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return MessagesPage{}, err
}
resp, err := sdk.sendRequest(req, token, string(sdk.msgContentType))
if err != nil {
return MessagesPage{}, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return MessagesPage{}, err
}
if resp.StatusCode != http.StatusOK {
return MessagesPage{}, errors.Wrap(ErrFailedRead, errors.New(resp.Status))
}
var mp MessagesPage
if err := json.Unmarshal(body, &mp); err != nil {
return MessagesPage{}, err
}
return mp, nil
}
func (sdk mfSDK) SetContentType(ct ContentType) error {
if ct != CTJSON && ct != CTJSONSenML && ct != CTBinary {
return ErrInvalidContentType
}
sdk.msgContentType = ct
return nil
}