Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update registration methods to use V2 API #30

Merged
merged 9 commits into from
Oct 6, 2022
41 changes: 19 additions & 22 deletions e2e/e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const accessToken = process.env.EVENTS_JWT_TOKEN
const consumerOrgId = process.env.EVENTS_CONSUMER_ORG_ID
const workspaceId = process.env.EVENTS_WORKSPACE_ID
const projectId = process.env.EVENTS_PROJECT_ID
const integrationId = process.env.EVENTS_INTEGRATION_ID
const httpOptions = { retries: 3 }
const randomNumber = Math.round(Math.random() * 100000)

Expand Down Expand Up @@ -77,26 +76,25 @@ test('test create event metadata', async () => {

test('test register journalling endpoint', async () => {
// create journal registration
journalReg = await sdkClient.createWebhookRegistration(consumerOrgId,
integrationId, {
name: 'Test Events SDK ' + randomNumber,
description: 'Test Events SDK ' + randomNumber,
client_id: apiKey,
delivery_type: 'JOURNAL',
events_of_interest: [
{
event_code: eventCode,
provider_id: providerId
}
]
})
expect(journalReg.status).toBe('VERIFIED')
expect(journalReg.integration_status).toBe('ENABLED')
journalReg = await sdkClient.createRegistration(consumerOrgId, projectId, workspaceId, {
name: 'Test Events SDK ' + randomNumber,
description: 'Test Events SDK ' + randomNumber,
client_id: apiKey,
delivery_type: 'journal',
events_of_interest: [
{
event_code: eventCode,
provider_id: providerId
}
]
})
expect(journalReg.webhook_status).toBe('verified')
expect(journalReg.enabled).toBe(true)
})

test('test fetch journalling position', async () => {
const journallingUrl = journalReg.events_url
logger.info('Journal endpoint has been registered')
const journallingUrl = journalReg._links['rel:events'].href
logger.info('Journal endpoint ' + journallingUrl + ' has been registered')

// sleep for one min
await sleep(60000)
Expand Down Expand Up @@ -124,7 +122,7 @@ test('test publish event', async () => {
})

test('test event received in journalling endpoint', async () => {
var count = 0
let count = 0
let nextLink = journalling.link.next
// retry to fetch from journalling 3 times ( 30 seconds )
while (count < 3 && journalling.retryAfter && journalling.events === undefined) {
Expand All @@ -141,8 +139,7 @@ test('test event received in journalling endpoint', async () => {
})

test('delete webhook registration', async () => {
await sdkClient.deleteWebhookRegistration(consumerOrgId,
integrationId, journalReg.registration_id)
await sdkClient.deleteRegistration(consumerOrgId, projectId, workspaceId, journalReg.registration_id)
journalReg = undefined
})

Expand All @@ -161,7 +158,7 @@ test('delete provider', async () => {
afterAll(async () => {
// delete webhook registration
if (journalReg) {
await sdkClient.deleteWebhookRegistration(consumerOrgId, integrationId,
await sdkClient.deleteRegistration(consumerOrgId, projectId, workspaceId,
journalReg.registration_id)
}

Expand Down
2 changes: 2 additions & 0 deletions src/SDKErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ E('ERROR_UPDATE_EVENTMETADATA', '%s')
E('ERROR_DELETE_ALL_EVENTMETADATA', '%s')
E('ERROR_DELETE_EVENTMETADATA', '%s')
E('ERROR_CREATE_REGISTRATION', '%s')
E('ERROR_UPDATE_REGISTRATION', '%s')
E('ERROR_GET_REGISTRATION', '%s')
E('ERROR_GET_ALL_REGISTRATION', '%s')
E('ERROR_GET_ALL_REGISTRATION_FOR_ORG', '%s')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: change 'ERROR_GET_ALL_REGISTRATION_FOR_ORG' to 'ERROR_GET_ALL_REGISTRATIONS_FOR_ORG .
Same for 'ERROR_GET_ALL_REGISTRATION' above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

E('ERROR_DELETE_REGISTRATION', '%s')
E('ERROR_GET_JOURNAL_DATA', '%s')
E('ERROR_PUBLISH_EVENT', '%s')
69 changes: 56 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,61 +311,104 @@ class EventsCoreAPI {
* Create a webhook or journal registration
*
* @param {string} consumerOrgId Consumer Org Id from the console
* @param {string} integrationId integration Id from the console
* @param {string} projectId Project Id from the console
* @param {string} workspaceId Workspace Id from the console
* @param {object} body Json data contains details of the registration
* @returns {Promise<object>} Details of the webhook/journal registration created
*/
createWebhookRegistration (consumerOrgId, integrationId, body) {
createRegistration (consumerOrgId, projectId, workspaceId, body) {
const headers = {}
const requestOptions = this.__createRequest('POST', headers, JSON.stringify(body))
const url = this.__getUrl(`/events/organizations/${consumerOrgId}/integrations/${integrationId}/registrations`)
const url = this.__getUrl(`/events/${consumerOrgId}/${projectId}/${workspaceId}/registrations`)
const sdkDetails = { requestOptions: requestOptions, url: url }
return this.__handleRequest(url, requestOptions, sdkDetails, codes.ERROR_CREATE_REGISTRATION)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why we pass the url and requestOptions to this method, these are already contained in the sdkDetails ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i fixed it. thanks!

}

/**
* Update a webhook or journal registration
*
* @param {string} consumerOrgId Consumer Org Id from the console
* @param {string} projectId Project Id from the console
* @param {string} workspaceId Workspace Id from the console
* @param {string} registrationId Registration id whose details are to be fetched
* @param {object} body Json data contains details of the registration
* @returns {Promise<object>} Details of the webhook/journal registration to be updated
*/
updateRegistration (consumerOrgId, projectId, workspaceId, registrationId, body) {
const headers = {}
shikhartanwar marked this conversation as resolved.
Show resolved Hide resolved
const requestOptions = this.__createRequest('POST', headers, JSON.stringify(body))
const url = this.__getUrl(`/events/${consumerOrgId}/${projectId}/${workspaceId}/registrations/${registrationId}`)
const sdkDetails = { requestOptions: requestOptions, url: url }
return this.__handleRequest(url, requestOptions, sdkDetails, codes.ERROR_UPDATE_REGISTRATION)
}

/**
* Get registration details for a given registration
*
* @param {string} consumerOrgId Consumer Org Id from the console
* @param {string} integrationId Integration Id from the console
* @param {string} projectId Project Id from the console
* @param {string} workspaceId Workspace Id from the console
* @param {string} registrationId Registration id whose details are to be fetched
* @returns {Promise<object>} Details of the webhook/journal registration
*/
getWebhookRegistration (consumerOrgId, integrationId, registrationId) {
getRegistration (consumerOrgId, projectId, workspaceId, registrationId) {
const headers = {}
const requestOptions = this.__createRequest('GET', headers)
const url = this.__getUrl(`/events/organizations/${consumerOrgId}/integrations/${integrationId}/registrations/${registrationId}`)
const url = this.__getUrl(`/events/${consumerOrgId}/${projectId}/${workspaceId}/registrations/${registrationId}`)
const sdkDetails = { requestOptions: requestOptions, url: url }
return this.__handleRequest(url, requestOptions, sdkDetails, codes.ERROR_GET_REGISTRATION)
}

/**
* Get all registration details for a given integration
* Get all registration details for a workspace
*
* @param {string} consumerOrgId Consumer Org Id from the console
* @param {string} integrationId Integration Id from the console
* @param {string} projectId Project Id from the console
* @param {string} workspaceId Workspace Id from the console
* @returns {Promise<object>} List of all webhook/journal registrations
*/
getAllWebhookRegistrations (consumerOrgId, integrationId) {
getAllRegistrationsForWorkspace (consumerOrgId, projectId, workspaceId) {
const headers = {}
const requestOptions = this.__createRequest('GET', headers)
const url = this.__getUrl(`/events/organizations/${consumerOrgId}/integrations/${integrationId}/registrations`)
const url = this.__getUrl(`/events/${consumerOrgId}/${projectId}/${workspaceId}/registrations`)
const sdkDetails = { requestOptions: requestOptions, url: url }
return this.__handleRequest(url, requestOptions, sdkDetails, codes.ERROR_GET_ALL_REGISTRATION)
}

/**
* @typedef {object} Page
* @property {number} [page] page number to be fetched. Default 0 (optional)
* @property {number} [size] size of each page. Default 10 (optional)
*/
shikhartanwar marked this conversation as resolved.
Show resolved Hide resolved
/**
* Get all registration details for an org
*
* @param {string} consumerOrgId Consumer Org Id from the console
* @param {Page} [page] page size and page number
* @returns {Promise<object>} Paginated response of all webhook/journal registrations for an org
*/
getAllRegistrationsForOrg (consumerOrgId, page) {
const headers = {}
const requestOptions = this.__createRequest('GET', headers)
const url = this.__getUrl(`/events/${consumerOrgId}/registrations`)
const urlWithQueryParams = helpers.appendQueryParams(url, page)
const sdkDetails = { requestOptions: requestOptions, url: urlWithQueryParams }
return this.__handleRequest(url, requestOptions, sdkDetails, codes.ERROR_GET_ALL_REGISTRATION_FOR_ORG)
}

/**
* Delete webhook registration
*
* @param {string} consumerOrgId Consumer Org Id from the console
* @param {string} integrationId Integration Id from the console
* @param {string} projectId Project Id from the console
* @param {string} workspaceId Workspace Id from the console
* @param {string} registrationId Id of the registration to be deleted
* @returns {Promise<object>} Empty object if deletion was successful
*/
deleteWebhookRegistration (consumerOrgId, integrationId, registrationId) {
deleteRegistration (consumerOrgId, projectId, workspaceId, registrationId) {
const headers = {}
const requestOptions = this.__createRequest('DELETE', headers)
const url = this.__getUrl(`/events/organizations/${consumerOrgId}/integrations/${integrationId}/registrations/${registrationId}`)
const url = this.__getUrl(`/events/${consumerOrgId}/${projectId}/${workspaceId}/registrations/${registrationId}`)
const sdkDetails = { requestOptions: requestOptions, url: url }
return this.__handleRequest(url, requestOptions, sdkDetails, codes.ERROR_DELETE_REGISTRATION)
}
Expand Down
Loading