Skip to content

Commit

Permalink
NOISSUE - Add pagemetadata to ReadMessage function in sdk (absmach#2055)
Browse files Browse the repository at this point in the history
Signed-off-by: ianmuchyri <[email protected]>
  • Loading branch information
ianmuchyri authored and dborovcanin committed Jan 10, 2024
1 parent 6601815 commit 9f57385
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
15 changes: 12 additions & 3 deletions cli/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

package cli

import "github.com/spf13/cobra"
import (
mgxsdk "github.com/absmach/magistrala/pkg/sdk/go"
"github.com/spf13/cobra"
)

var cmdMessages = []cobra.Command{
{
Expand All @@ -27,14 +30,20 @@ var cmdMessages = []cobra.Command{
{
Use: "read <channel_id.subtopic> <user_token>",
Short: "Read messages",
Long: `Reads all channel messages`,
Long: "Reads all channel messages\n" +
"Usage:\n" +
"\tmagistrala-cli messages read <channel_id.subtopic> <user_token> --offset <offset> --limit <limit> - lists all messages with provided offset and limit\n",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 2 {
logUsage(cmd.Use)
return
}
pageMetadata := mgxsdk.PageMetadata{
Offset: Offset,
Limit: Limit,
}

m, err := sdk.ReadMessages(args[0], args[1])
m, err := sdk.ReadMessages(pageMetadata, args[0], args[1])
if err != nil {
logError(err)
return
Expand Down
14 changes: 9 additions & 5 deletions pkg/sdk/go/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,26 @@ func (sdk mgSDK) SendMessage(chanName, msg, key string) errors.SDKError {
return err
}

func (sdk mgSDK) ReadMessages(chanName, token string) (MessagesPage, errors.SDKError) {
func (sdk mgSDK) ReadMessages(pm PageMetadata, chanName, token string) (MessagesPage, errors.SDKError) {
chanNameParts := strings.SplitN(chanName, ".", channelParts)
chanID := chanNameParts[0]
subtopicPart := ""
if len(chanNameParts) == channelParts {
subtopicPart = fmt.Sprintf("?subtopic=%s", chanNameParts[1])
}

url := fmt.Sprintf("%s/channels/%s/messages%s", sdk.readerURL, chanID, subtopicPart)
readMessagesEndpoint := fmt.Sprintf("channels/%s/messages%s", chanID, subtopicPart)
url, err := sdk.withQueryParams(sdk.readerURL, readMessagesEndpoint, pm)
if err != nil {
return MessagesPage{}, errors.NewSDKError(err)
}

header := make(map[string]string)
header["Content-Type"] = string(sdk.msgContentType)

_, body, err := sdk.processRequest(http.MethodGet, url, token, nil, header, http.StatusOK)
if err != nil {
return MessagesPage{}, err
_, body, sdkerr := sdk.processRequest(http.MethodGet, url, token, nil, header, http.StatusOK)
if sdkerr != nil {
return MessagesPage{}, sdkerr
}

var mp MessagesPage
Expand Down
8 changes: 6 additions & 2 deletions pkg/sdk/go/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -828,9 +828,13 @@ type SDK interface {
// ReadMessages read messages of specified channel.
//
// example:
// msgs, _ := sdk.ReadMessages("channelID", "token")
// pm := sdk.PageMetadata{
// Offset: 0,
// Limit: 10,
// }
// msgs, _ := sdk.ReadMessages(pm,"channelID", "token")
// fmt.Println(msgs)
ReadMessages(chanID, token string) (MessagesPage, errors.SDKError)
ReadMessages(pm PageMetadata, chanID, token string) (MessagesPage, errors.SDKError)

// SetContentType sets message content type.
//
Expand Down
18 changes: 9 additions & 9 deletions pkg/sdk/mocks/sdk.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9f57385

Please sign in to comment.