Skip to content

Commit

Permalink
Merge pull request #4185 from alphagov/PP-11681_replace_request_with_…
Browse files Browse the repository at this point in the history
…axios_webhooks_client

PP-11681 Replace ‘base-client’ with ‘axios-base-client' for webhooks client
  • Loading branch information
JFSGDS authored Mar 27, 2024
2 parents 60338b5 + 60636fd commit 4843cd6
Show file tree
Hide file tree
Showing 4 changed files with 342 additions and 123 deletions.
202 changes: 79 additions & 123 deletions app/services/clients/webhooks.client.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const baseClient = require('./base-client/base.client')
const { Client } = require('@govuk-pay/pay-js-commons/lib/utils/axios-base-client/axios-base-client')
const { configureClient } = require('./base/config')
const urlJoin = require('url-join')

const defaultRequestOptions = {
Expand All @@ -9,155 +10,110 @@ const defaultRequestOptions = {
service: 'webhooks'
}

function webhook (id, serviceId, gatewayAccountId, options = {}) {
const url = urlJoin('/v1/webhook', id)
const request = {
url,
qs: {
service_id: serviceId,
gateway_account_id: gatewayAccountId
},
description: 'Get one webhook',
...defaultRequestOptions,
...options
}
return baseClient.get(request)
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)
const fullUrl = `${url}?service_id=${serviceId}&gateway_account_id=${gatewayAccountId}`
configureClient(client, fullUrl)
const response = await client.get(fullUrl, 'Get one webhook')
return response.data
}

function signingSecret (webhookId, serviceId, gatewayAccountId, options = {}) {
const url = urlJoin('/v1/webhook/', webhookId, '/signing-key')
const request = {
url,
qs: {
service_id: serviceId,
gateway_account_id: gatewayAccountId
},
description: 'Get a Webhook signing secret',
...defaultRequestOptions,
...options
}
return baseClient.get(request)
async function signingSecret (webhookId, serviceId, gatewayAccountId, options = {}) {
const baseUrl = options.baseUrl ? options.baseUrl : defaultRequestOptions.baseUrl
const url = urlJoin(baseUrl, '/v1/webhook/', webhookId, '/signing-key')
const fullUrl = `${url}?service_id=${serviceId}&gateway_account_id=${gatewayAccountId}`
configureClient(client, fullUrl)
const response = await client.get(fullUrl, 'Get a Webhook signing secret')
return response.data
}

function resetSigningSecret (webhookId, serviceId, gatewayAccountId, options = {}) {
const url = urlJoin('/v1/webhook/', webhookId, '/signing-key')
const request = {
url,
qs: {
service_id: serviceId,
gateway_account_id: gatewayAccountId
},
description: 'Reset a Webhook signing secret',
...defaultRequestOptions,
...options
}
return baseClient.post(request)
async function resetSigningSecret (webhookId, serviceId, gatewayAccountId, options = {}) {
const baseUrl = options.baseUrl ? options.baseUrl : defaultRequestOptions.baseUrl
const url = urlJoin(baseUrl, '/v1/webhook/', webhookId, '/signing-key')
const fullUrl = `${url}?service_id=${serviceId}&gateway_account_id=${gatewayAccountId}`
configureClient(client, fullUrl)
const response = await client.post(fullUrl, 'Reset a Webhook signing secret')
return response.data
}

function webhooks (serviceId, gatewayAccountId, isLive, options = {}) {
const url = '/v1/webhook'
const request = {
url,
qs: {
service_id: serviceId,
gateway_account_id: gatewayAccountId,
live: isLive
},
description: 'List webhooks for service',
...defaultRequestOptions,
...options
}
return baseClient.get(request)
async function webhooks (serviceId, gatewayAccountId, isLive, options = {}) {
const baseUrl = options.baseUrl ? options.baseUrl : defaultRequestOptions.baseUrl
const url = urlJoin(baseUrl, '/v1/webhook')
const fullUrl = `${url}?service_id=${serviceId}&gateway_account_id=${gatewayAccountId}&live=${isLive}`
configureClient(client, fullUrl)
const response = await client.get(fullUrl, 'List webhooks for service')
return response.data
}

function message (id, webhookId, options = {}) {
const url = urlJoin('/v1/webhook', webhookId, 'message', id)
const request = {
url,
description: 'Get webhook message',
...defaultRequestOptions,
...options
}
return baseClient.get(request)
async function message (id, webhookId, options = {}) {
const baseUrl = options.baseUrl ? options.baseUrl : defaultRequestOptions.baseUrl
const url = urlJoin(baseUrl, '/v1/webhook', webhookId, 'message', id)
configureClient(client, url)
const response = await client.get(url, 'Get webhook message')
return response.data
}

function attempts (messageId, webhookId, options = {}) {
const url = urlJoin('/v1/webhook', webhookId, 'message', messageId, 'attempt')
const request = {
url,
description: 'Get webhook message delivery attempts',
...defaultRequestOptions,
...options
}
return baseClient.get(request)
async function attempts (messageId, webhookId, options = {}) {
const baseUrl = options.baseUrl ? options.baseUrl : defaultRequestOptions.baseUrl
const url = urlJoin(baseUrl, '/v1/webhook', webhookId, 'message', messageId, 'attempt')
configureClient(client, url)
const response = await client.get(url, 'Get webhook message delivery attempts')
return response.data
}

function messages (id, options = {}) {
const url = urlJoin('/v1/webhook', id, 'message')
const request = {
url,
qs: {
page: options.page,
...options.status && { status: options.status.toUpperCase() }
},
description: 'List messages for webhook',
...defaultRequestOptions,
...options
async function messages (id, options = {}) {
const baseUrl = options.baseUrl ? options.baseUrl : defaultRequestOptions.baseUrl
const url = urlJoin(baseUrl, '/v1/webhook', id, 'message')
let fullUrl = `${url}?page=${options.page}`
if (options.status) {
fullUrl = `${fullUrl}&status=${options.status.toUpperCase()}`
}
return baseClient.get(request)
configureClient(client, fullUrl)
const response = await client.get(fullUrl, 'List messages for webhook')
return response.data
}

function createWebhook (serviceId, gatewayAccountId, isLive, options = {}) {
const url = '/v1/webhook'
const request = {
url,
body: {
service_id: serviceId,
gateway_account_id: gatewayAccountId,
live: isLive,
callback_url: options.callback_url,
subscriptions: options.subscriptions,
description: options.description
},
...defaultRequestOptions,
...options,
description: 'Create a Webhook'
async function createWebhook (serviceId, gatewayAccountId, isLive, options = {}) {
const body = {
service_id: serviceId,
gateway_account_id: gatewayAccountId,
live: isLive,
callback_url: options.callback_url,
subscriptions: options.subscriptions,
description: options.description
}
return baseClient.post(request)
const baseUrl = options.baseUrl ? options.baseUrl : defaultRequestOptions.baseUrl
const url = urlJoin(baseUrl, '/v1/webhook')
configureClient(client, url)
const response = await client.post(url, body, 'Create a Webhook')
return response.data
}

function updateWebhook (id, serviceId, gatewayAccountId, options = {}) {
const url = urlJoin('/v1/webhook', id)
async function updateWebhook (id, serviceId, gatewayAccountId, options = {}) {
const paths = [ 'callback_url', 'subscriptions', 'description', 'status' ]
const body = []
paths.forEach((path) => {
if (options[path]) {
body.push({ op: 'replace', path, value: options[path] })
}
})
const request = {
url,
qs: {
service_id: serviceId,
gateway_account_id: gatewayAccountId
},
body,
...defaultRequestOptions,
...options,
description: 'Update a Webhook'
}
return baseClient.patch(request)
const baseUrl = options.baseUrl ? options.baseUrl : defaultRequestOptions.baseUrl
const url = urlJoin(baseUrl, '/v1/webhook', id)
const fullUrl = `${url}?service_id=${serviceId}&gateway_account_id=${gatewayAccountId}`
configureClient(client, fullUrl)
const response = await client.patch(fullUrl, body, 'Create a Webhook')
return response.data
}

function resendWebhookMessage (webhookId, messageId, options = {}) {
const url = urlJoin('/v1/webhook', webhookId, 'message', messageId, 'resend')
const request = {
url,
...defaultRequestOptions,
...options,
description: 'Schedule resending a message'
}
return baseClient.post(request)
async function resendWebhookMessage (webhookId, messageId, options = {}) {
const baseUrl = options.baseUrl ? options.baseUrl : defaultRequestOptions.baseUrl
const url = urlJoin(baseUrl, '/v1/webhook', webhookId, 'message', messageId, 'resend')
configureClient(client, url)
const response = await client.post(url, {}, 'Schedule resending a message')
return response.data
}

module.exports = {
Expand Down
Loading

0 comments on commit 4843cd6

Please sign in to comment.