Skip to content

Commit

Permalink
add domainId to read messsages
Browse files Browse the repository at this point in the history
Signed-off-by: Musilah <[email protected]>
  • Loading branch information
Musilah committed Nov 19, 2024
1 parent e52b134 commit 134687d
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/real-rings-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@absmach/magistrala-sdk": minor
---

Add domainId to read messages
2 changes: 1 addition & 1 deletion examples/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ mySdk.messages
})

mySdk.messages
.Read({ offset: 0, limit: 10 }, 'channelId', token)
.Read({ offset: 0, limit: 10 }, '<channelId>', token, '<domainId>')
.then((response: any) => {
console.log('response: ', response)
})
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,15 @@ export default class Messages {
}
}

public async Read (pm: MessagesPageMetadata, channelId: string, token: string): Promise<MessagesPage> {
public async Read (pm: MessagesPageMetadata, channelId: string, token: string, domainId: string): Promise<MessagesPage> {
// Read messages
/**
*
* @method Read - Read messages from a given channel. Messages are read from a reader
* add-on such as Timescale. Messages are read from the http adapter.
* @param {string} channel_id - The ID of the channel to read the message from.
* @param {string} token - Authentication token.
* @param {string} domain_id - The ID of the domain.
*/

const stringParams: Record<string, string> = Object.fromEntries(
Expand All @@ -111,7 +112,7 @@ export default class Messages {
try {
const response = await fetch(
new URL(
`channels/${chanId}/messages${subtopicPart}?${new URLSearchParams(stringParams).toString()}`,
`${domainId}/channels/${chanId}/messages${subtopicPart}?${new URLSearchParams(stringParams).toString()}`,
this.readersUrl
).toString(),
options
Expand Down
7 changes: 4 additions & 3 deletions tests/messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ describe('Messages', () => {
time: 1276020076.001,
value: 120.1
}
const msg = '{"bn":"base-name:","bt":1.276020076001e+09, "bu":"A","bver":5, "n":"voltage","u":"V","v":120.1}'
const msg = '[{"n": "temp","bu": "C","u": "C","v": 23000}]'
const thingKey = 'bb7edb32-2eac-4aad-aebe-ed96fe073879'
const token = '<token>'
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJiYjdlZGIzM'
const domainId = 'bb7edb32-2eac-4aad-aebe-ed96fe073879'
const queryParams = {
offset: 0,
limit: 10
Expand All @@ -52,7 +53,7 @@ describe('Messages', () => {
test('Read should read messages and return success', async () => {
fetchMock.mockResponseOnce(JSON.stringify(messagesPage))

const response = await sdk.messages.Read(queryParams, channelId, token)
const response = await sdk.messages.Read(queryParams, channelId, token, domainId)
expect(response).toEqual(messagesPage)
})
})

0 comments on commit 134687d

Please sign in to comment.