|
| 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 | +} |
0 commit comments