1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-28 13:48:49 +08:00
Mainflux.mainflux/sdk/go/message.go

93 lines
1.8 KiB
Go
Raw Normal View History

//
// Copyright (c) 2018
// Mainflux
//
// SPDX-License-Identifier: Apache-2.0
//
package sdk
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
func (sdk mfSDK) SendMessage(chanID, msg, token string) error {
endpoint := fmt.Sprintf("channels/%s/messages", chanID)
url := createURL(sdk.baseURL, sdk.httpAdapterPrefix, endpoint)
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 {
switch resp.StatusCode {
case http.StatusBadRequest:
return ErrInvalidArgs
case http.StatusForbidden:
return ErrUnauthorized
default:
MF-417 - Implement SDK tests (#438) * Add SKD tests for creating channel Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add SDK tests for Channel function Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add list channels over SDK tests Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add SDK tests for updating channel Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add SDK tests for deleting channel Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add users service SDK tests Signed-off-by: Ivan Milošević <iva@blokovi.com> * SDK things tests Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add SDK test for connecting and disconnecting things from channel Signed-off-by: Ivan Milošević <iva@blokovi.com> * testing SDK sending messages Signed-off-by: Ivan Milošević <iva@blokovi.com> * add tests for SDK func SetContentType Signed-off-by: Ivan Milošević <iva@blokovi.com> * add all test cases for sending messages Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add handling StatusBadRequest when deleting thing over SDK Signed-off-by: Ivan Milošević <iva@blokovi.com> * Update error responses when deleting channel and thing Signed-off-by: Ivan Milošević <iva@blokovi.com> * Removed unused Unauthorized response when creating user Signed-off-by: Ivan Milošević <iva@blokovi.com> * update testing CreateChannel, tests if response is some string Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add bad request case in testing CreateToken Signed-off-by: Ivan Milošević <iva@blokovi.com> * Remove response error conflict from things service Signed-off-by: Ivan Milošević <iva@blokovi.com> * Fix definition of sdk.ErrFailedDisconnect and return error in test cases for disconnecting things Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add handling errors and formatting code Signed-off-by: Ivan Milošević <iva@blokovi.com> * Defined new ErrFailedPublish error in SDK Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add SDK test for version Signed-off-by: Ivan Milošević <iva@blokovi.com> * Delete unused http response status in sdk.DeleteChannel Signed-off-by: Ivan Milošević <iva@blokovi.com>
2018-11-19 21:27:01 +01:00
return ErrFailedPublish
}
}
return nil
}
func (sdk mfSDK) ReadMessages(chanID, token string) ([]Message, error) {
endpoint := fmt.Sprintf("channels/%s/messages", chanID)
url := createURL(sdk.readerURL, "", endpoint)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
resp, err := sdk.sendRequest(req, token, string(sdk.msgContentType))
if err != nil {
return nil, err
}
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, ErrFailedRead
}
}
var l listMessagesRes
if err := json.Unmarshal(body, &l); err != nil {
return nil, err
}
return l.Messages, nil
}
func (sdk *mfSDK) SetContentType(ct ContentType) error {
if ct != CTJSON && ct != CTJSONSenML && ct != CTBinary {
return ErrInvalidContentType
}
sdk.msgContentType = ct
return nil
}