diff --git a/app/services/clients/webhooks.client.js b/app/services/clients/webhooks.client.js index d5030a7ec9..570a1aa588 100644 --- a/app/services/clients/webhooks.client.js +++ b/app/services/clients/webhooks.client.js @@ -10,74 +10,69 @@ const defaultRequestOptions = { service: 'webhooks' } +const client = new Client(defaultRequestOptions.service) + async function webhook (id, serviceId, gatewayAccountId, options = {}) { const baseUrl = options.baseUrl ? options.baseUrl : defaultRequestOptions.baseUrl const url = urlJoin(baseUrl, '/v1/webhook', id) - this.client = new Client(defaultRequestOptions.service) const fullUrl = `${url}?service_id=${serviceId}&gateway_account_id=${gatewayAccountId}` - configureClient(this.client, fullUrl) - const response = await this.client.get(fullUrl, 'Get one webhook') + configureClient(client, fullUrl) + const response = await client.get(fullUrl, 'Get one webhook') return response.data } async function signingSecret (webhookId, serviceId, gatewayAccountId, options = {}) { const baseUrl = options.baseUrl ? options.baseUrl : defaultRequestOptions.baseUrl const url = urlJoin(baseUrl, '/v1/webhook/', webhookId, '/signing-key') - this.client = new Client(defaultRequestOptions.service) const fullUrl = `${url}?service_id=${serviceId}&gateway_account_id=${gatewayAccountId}` - configureClient(this.client, fullUrl) - const response = await this.client.get(fullUrl, 'Get a Webhook signing secret') + configureClient(client, fullUrl) + const response = await client.get(fullUrl, 'Get a Webhook signing secret') return response.data } async function resetSigningSecret (webhookId, serviceId, gatewayAccountId, options = {}) { const baseUrl = options.baseUrl ? options.baseUrl : defaultRequestOptions.baseUrl const url = urlJoin(baseUrl, '/v1/webhook/', webhookId, '/signing-key') - this.client = new Client(defaultRequestOptions.service) const fullUrl = `${url}?service_id=${serviceId}&gateway_account_id=${gatewayAccountId}` - configureClient(this.client, fullUrl) - const response = await this.client.post(fullUrl, 'Reset a Webhook signing secret') + configureClient(client, fullUrl) + const response = await client.post(fullUrl, 'Reset a Webhook signing secret') return response.data } async function webhooks (serviceId, gatewayAccountId, isLive, options = {}) { const baseUrl = options.baseUrl ? options.baseUrl : defaultRequestOptions.baseUrl const url = urlJoin(baseUrl, '/v1/webhook') - this.client = new Client(defaultRequestOptions.service) const fullUrl = `${url}?service_id=${serviceId}&gateway_account_id=${gatewayAccountId}&live=${isLive}` - configureClient(this.client, fullUrl) - const response = await this.client.get(fullUrl, 'List webhooks for service') + configureClient(client, fullUrl) + const response = await client.get(fullUrl, 'List webhooks for service') return response.data } async function message (id, webhookId, options = {}) { const baseUrl = options.baseUrl ? options.baseUrl : defaultRequestOptions.baseUrl const url = urlJoin(baseUrl, '/v1/webhook', webhookId, 'message', id) - this.client = new Client(defaultRequestOptions.service) - configureClient(this.client, url) - const response = await this.client.get(url, 'Get webhook message') + configureClient(client, url) + const response = await client.get(url, 'Get webhook message') return response.data } async function attempts (messageId, webhookId, options = {}) { const baseUrl = options.baseUrl ? options.baseUrl : defaultRequestOptions.baseUrl const url = urlJoin(baseUrl, '/v1/webhook', webhookId, 'message', messageId, 'attempt') - this.client = new Client(defaultRequestOptions.service) - configureClient(this.client, url) - const response = await this.client.get(url, 'Get webhook message delivery attempts') + configureClient(client, url) + const response = await client.get(url, 'Get webhook message delivery attempts') return response.data } async function messages (id, options = {}) { const baseUrl = options.baseUrl ? options.baseUrl : defaultRequestOptions.baseUrl const url = urlJoin(baseUrl, '/v1/webhook', id, 'message') - this.client = new Client(defaultRequestOptions.service) let fullUrl = `${url}?page=${options.page}` if (options.status) { fullUrl = `${fullUrl}&status=${options.status.toUpperCase()}` } - configureClient(this.client, fullUrl) - const response = await this.client.get(fullUrl, 'List messages for webhook') + configureClient(client, fullUrl) + const response = await client.get(fullUrl, 'List messages for webhook') return response.data } @@ -92,9 +87,8 @@ async function createWebhook (serviceId, gatewayAccountId, isLive, options = {}) } const baseUrl = options.baseUrl ? options.baseUrl : defaultRequestOptions.baseUrl const url = urlJoin(baseUrl, '/v1/webhook') - this.client = new Client(defaultRequestOptions.service) - configureClient(this.client, url) - const response = await this.client.post(url, body, 'Create a Webhook') + configureClient(client, url) + const response = await client.post(url, body, 'Create a Webhook') return response.data } @@ -108,19 +102,17 @@ async function updateWebhook (id, serviceId, gatewayAccountId, options = {}) { }) const baseUrl = options.baseUrl ? options.baseUrl : defaultRequestOptions.baseUrl const url = urlJoin(baseUrl, '/v1/webhook', id) - this.client = new Client(defaultRequestOptions.service) const fullUrl = `${url}?service_id=${serviceId}&gateway_account_id=${gatewayAccountId}` - configureClient(this.client, fullUrl) - const response = await this.client.patch(fullUrl, body, 'Create a Webhook') + configureClient(client, fullUrl) + const response = await client.patch(fullUrl, body, 'Create a Webhook') return response.data } async function resendWebhookMessage (webhookId, messageId, options = {}) { const baseUrl = options.baseUrl ? options.baseUrl : defaultRequestOptions.baseUrl const url = urlJoin(baseUrl, '/v1/webhook', webhookId, 'message', messageId, 'resend') - this.client = new Client(defaultRequestOptions.service) - configureClient(this.client, url) - const response = await this.client.post(url, {}, 'Schedule resending a message') + configureClient(client, url) + const response = await client.post(url, {}, 'Schedule resending a message') return response.data }