Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PP-11681 Replace ‘base-client’ with ‘axios-base-client' for webhooks client #4185

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() }
iqbalgds marked this conversation as resolved.
Show resolved Hide resolved
},
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
Loading