Skip to content

Commit 099aaad

Browse files
committed
feat: adds webhook functions in installation and deletes previous webhook implementation [cs-38336]
1 parent 5911fb0 commit 099aaad

File tree

8 files changed

+327
-597
lines changed

8 files changed

+327
-597
lines changed

lib/marketplace/installation/index.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import cloneDeep from 'lodash/cloneDeep'
22
import error from '../../core/contentstackError'
33
import { deleteEntity, fetch, fetchAll, update } from '../../entity'
4+
import { WebHooks } from './webhooks'
45

56
export function Installation (http, data, params = {}) {
67
this.params = params
@@ -131,6 +132,36 @@ export function Installation (http, data, params = {}) {
131132
}
132133
}
133134

135+
/**
136+
* @description To update organization level app installation configuration.
137+
* @memberof Installation
138+
* @func getConfigLocation
139+
* @param {*} config Config that needs to be updated
140+
* @returns {Promise<Response>}
141+
*
142+
* @example
143+
* import * as contentstack from '@contentstack/management'
144+
* const client = contentstack.client({ authtoken: 'TOKEN'})
145+
*
146+
* client.organization('organization_uid').marketplace().installation('installation_uid').getConfigLocation()
147+
* .then((response) => console.log(response))
148+
*/
149+
this.getConfigLocation = async () => {
150+
try {
151+
const headers = {
152+
headers: { ...cloneDeep(params) }
153+
} || {}
154+
const response = await http.get(`${this.urlPath}/locations/configuration`, headers)
155+
if (response.data) {
156+
return response.data
157+
} else {
158+
throw error(response)
159+
}
160+
} catch (err) {
161+
throw error(err)
162+
}
163+
}
164+
134165
/**
135166
* @description To fetch server side organization level config required for the app.
136167
* @memberof Installation
@@ -222,6 +253,10 @@ export function Installation (http, data, params = {}) {
222253
throw error(err)
223254
}
224255
}
256+
257+
this.webhooks = (webhookUid) => {
258+
return new WebHooks(http, { uid: webhookUid, installationUid: this.uid })
259+
}
225260
} else {
226261
this.urlPath = `/installations`
227262

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import cloneDeep from 'lodash/cloneDeep'
2+
import error from '../../../core/contentstackError'
3+
4+
export function WebHooks (http, data, params = {}) {
5+
this.params = params
6+
if (data) {
7+
Object.assign(this, cloneDeep(data))
8+
if (this.organization_uid) {
9+
this.params = {
10+
organization_uid: this.organization_uid
11+
}
12+
}
13+
if (this.uid) {
14+
this.urlPath = `installations/${this.installationUid}/webhooks/${this.uid}`
15+
16+
/**
17+
* @description The GET installation call is used to retrieve a specific installation of an app.
18+
* @memberof WebHook
19+
* @func listExecutionLogs
20+
* @returns {Promise<WebHook>}
21+
*
22+
* @example
23+
* import * as contentstack from '@contentstack/management'
24+
* const client = contentstack.client({ authtoken: 'TOKEN'})
25+
*
26+
* client.organization('organization_uid').marketplace().installation('installation_uid').listExecutionLogs()
27+
* .then((installation) => console.log(installation))
28+
*
29+
*/
30+
this.listExecutionLogs = async () => {
31+
try {
32+
const headers = {
33+
headers: { ...cloneDeep(params) }
34+
} || {}
35+
const response = await http.get(`${this.urlPath}/executions`, headers)
36+
if (response.data) {
37+
return response.data
38+
} else {
39+
throw error(response)
40+
}
41+
} catch (err) {
42+
throw error(err)
43+
}
44+
}
45+
46+
/**
47+
* @description The GET installation call is used to retrieve a specific installation of an app.
48+
* @memberof WebHook
49+
* @func getExecutionLog
50+
* @param {string} executionUid uid of the execution
51+
* @returns {Promise<WebHook>}
52+
*
53+
* @example
54+
* import * as contentstack from '@contentstack/management'
55+
* const client = contentstack.client({ authtoken: 'TOKEN'})
56+
*
57+
* client.organization('organization_uid').marketplace().installation('installation_uid').getExecutionLog('executionUid')
58+
* .then((installation) => console.log(installation))
59+
*
60+
*/
61+
this.getExecutionLog = async (executionUid) => {
62+
try {
63+
const headers = {
64+
headers: { ...cloneDeep(params) }
65+
} || {}
66+
const response = await http.get(`${this.urlPath}/executions/${executionUid}`, headers)
67+
if (response.data) {
68+
return response.data
69+
} else {
70+
throw error(response)
71+
}
72+
} catch (err) {
73+
throw error(err)
74+
}
75+
}
76+
77+
/**
78+
* @description The GET installation call is used to retrieve a specific installation of an app.
79+
* @memberof WebHook
80+
* @func retryExecution
81+
* @param {string} executionUid uid of the execution
82+
* @returns {Promise<WebHook>}
83+
*
84+
* @example
85+
* import * as contentstack from '@contentstack/management'
86+
* const client = contentstack.client({ authtoken: 'TOKEN'})
87+
*
88+
* client.organization('organization_uid').marketplace().installation('installation_uid').retryExecution('executionUid')
89+
* .then((installation) => console.log(installation))
90+
*
91+
*/
92+
this.retryExecution = async (executionUid) => {
93+
try {
94+
const headers = {
95+
headers: { ...cloneDeep(params) }
96+
} || {}
97+
const response = await http.post(`${this.urlPath}/executions/${executionUid}/retry`, headers)
98+
if (response.data) {
99+
return response.data
100+
} else {
101+
throw error(response)
102+
}
103+
} catch (err) {
104+
throw error(err)
105+
}
106+
}
107+
}
108+
}
109+
return this
110+
}

lib/stack/index.js

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { Environment } from './environment'
1010
import { Asset } from './asset'
1111
import { Locale } from './locale'
1212
import { Extension } from './extension'
13-
import { Webhook } from './webhook'
1413
import { Workflow } from './workflow'
1514
import { Release } from './release'
1615
import { BulkOperation } from './bulkOperation'
@@ -299,28 +298,6 @@ export function Stack (http, data) {
299298
return new Workflow(http, data)
300299
}
301300

302-
/**
303-
* @description Webhooks allow you to specify a URL to which you would like Contentstack to post data when an event happens.
304-
* @param {String} webhookUid The UID of the Webhook you want to get details.
305-
* @returns {Webhook} Instance of Webhook.
306-
* @example
307-
* import * as contentstack from '@contentstack/management'
308-
* const client = contentstack.client()
309-
*
310-
* client.stack({ api_key: 'api_key'}).webhook().create()
311-
* .then((webhook) => console.log(webhook))
312-
*
313-
* client.stack({ api_key: 'api_key'}).webhook('webhook_uid').fetch()
314-
* .then((webhook) => console.log(webhook))
315-
*/
316-
this.webhook = (webhookUid = null) => {
317-
const data = { stackHeaders: this.stackHeaders }
318-
if (webhookUid) {
319-
data.webhook = { uid: webhookUid }
320-
}
321-
return new Webhook(http, data)
322-
}
323-
324301
/**
325302
* @description Labels allow you to group a collection of content within a stack. Using labels you can group content types that need to work together
326303
* @param {String} uid The UID of the Label you want to get details.

0 commit comments

Comments
 (0)