diff --git a/cli/message.go b/cli/message.go index 61058bb3b6..79f86571fb 100644 --- a/cli/message.go +++ b/cli/message.go @@ -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{ { @@ -27,14 +30,20 @@ var cmdMessages = []cobra.Command{ { Use: "read ", Short: "Read messages", - Long: `Reads all channel messages`, + Long: "Reads all channel messages\n" + + "Usage:\n" + + "\tmagistrala-cli messages read --offset --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 diff --git a/pkg/sdk/go/message.go b/pkg/sdk/go/message.go index 5ad50af9fd..591436ef51 100644 --- a/pkg/sdk/go/message.go +++ b/pkg/sdk/go/message.go @@ -30,7 +30,7 @@ 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 := "" @@ -38,14 +38,18 @@ func (sdk mgSDK) ReadMessages(chanName, token string) (MessagesPage, errors.SDKE 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 diff --git a/pkg/sdk/go/sdk.go b/pkg/sdk/go/sdk.go index 219e84a953..98e3e6aab5 100644 --- a/pkg/sdk/go/sdk.go +++ b/pkg/sdk/go/sdk.go @@ -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. // diff --git a/pkg/sdk/mocks/sdk.go b/pkg/sdk/mocks/sdk.go index 8c58eac30c..e8eb18f56b 100644 --- a/pkg/sdk/mocks/sdk.go +++ b/pkg/sdk/mocks/sdk.go @@ -1804,9 +1804,9 @@ func (_m *SDK) Parents(id string, pm sdk.PageMetadata, token string) (sdk.Groups return r0, r1 } -// ReadMessages provides a mock function with given fields: chanID, token -func (_m *SDK) ReadMessages(chanID string, token string) (sdk.MessagesPage, errors.SDKError) { - ret := _m.Called(chanID, token) +// ReadMessages provides a mock function with given fields: pm, chanID, token +func (_m *SDK) ReadMessages(pm sdk.PageMetadata, chanID string, token string) (sdk.MessagesPage, errors.SDKError) { + ret := _m.Called(pm, chanID, token) if len(ret) == 0 { panic("no return value specified for ReadMessages") @@ -1814,17 +1814,17 @@ func (_m *SDK) ReadMessages(chanID string, token string) (sdk.MessagesPage, erro var r0 sdk.MessagesPage var r1 errors.SDKError - if rf, ok := ret.Get(0).(func(string, string) (sdk.MessagesPage, errors.SDKError)); ok { - return rf(chanID, token) + if rf, ok := ret.Get(0).(func(sdk.PageMetadata, string, string) (sdk.MessagesPage, errors.SDKError)); ok { + return rf(pm, chanID, token) } - if rf, ok := ret.Get(0).(func(string, string) sdk.MessagesPage); ok { - r0 = rf(chanID, token) + if rf, ok := ret.Get(0).(func(sdk.PageMetadata, string, string) sdk.MessagesPage); ok { + r0 = rf(pm, chanID, token) } else { r0 = ret.Get(0).(sdk.MessagesPage) } - if rf, ok := ret.Get(1).(func(string, string) errors.SDKError); ok { - r1 = rf(chanID, token) + if rf, ok := ret.Get(1).(func(sdk.PageMetadata, string, string) errors.SDKError); ok { + r1 = rf(pm, chanID, token) } else { if ret.Get(1) != nil { r1 = ret.Get(1).(errors.SDKError)