Skip to content

Commit

Permalink
PP-11681 Refactor 'axios-base-client' call following code review.
Browse files Browse the repository at this point in the history
  • Loading branch information
JFSGDS committed Mar 26, 2024
1 parent 86ddf6a commit 70637f8
Showing 1 changed file with 22 additions and 30 deletions.
52 changes: 22 additions & 30 deletions app/services/clients/webhooks.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand Down

0 comments on commit 70637f8

Please sign in to comment.