From 851391a2cc7abaa3c5e57002d293acd6afa13728 Mon Sep 17 00:00:00 2001 From: Iqbal Ahmed Date: Tue, 7 May 2024 14:00:18 +0100 Subject: [PATCH] PP-11682 Update `admin users client` to use axios base client - Update to use the `axios base client` from `pay-js-commons`. - Ensured all existing tests are working. --- app/services/clients/adminusers.client.js | 64 +++++++++++------------ 1 file changed, 31 insertions(+), 33 deletions(-) diff --git a/app/services/clients/adminusers.client.js b/app/services/clients/adminusers.client.js index d43c816b8..e5ac46fba 100644 --- a/app/services/clients/adminusers.client.js +++ b/app/services/clients/adminusers.client.js @@ -1,7 +1,8 @@ 'use strict' // Local dependencies -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 logger = require('../../utils/logger')(__filename) const requestLogger = require('../../utils/request-logger') const Service = require('../../models/Service.class') @@ -13,10 +14,9 @@ const METRICS_PREFIX = 'internal-rest-call.adminusers' const SUCCESS_CODES = [200, 201, 202, 204, 206] let baseUrl -let correlationId /** @private */ -function _getAdminUsers (url, description, findOptions, loggingFields = {}, callingFunctionName) { +async function _getAdminUsers (url, description, findOptions, loggingFields = {}, callingFunctionName) { const startTime = new Date() const context = { url: url, @@ -26,36 +26,35 @@ function _getAdminUsers (url, description, findOptions, loggingFields = {}, call service: SERVICE_NAME } requestLogger.logRequestStart(context, loggingFields) - const params = { - correlationId: correlationId, - qs: { - gatewayAccountId: findOptions.gatewayAccountId + + const client = new Client(SERVICE_NAME) + configureClient(client, url) + + try { + const fullUrl = `${url}?gatewayAccountId=${findOptions.gatewayAccountId}` + const response = await client.get(fullUrl, description) + + requestLogger.logRequestEnd(context, response.status, loggingFields) + incrementStatusCodeCounter(callingFunctionName, response.status) + if (SUCCESS_CODES.includes(response.status)) { + return new Service(response.data) + } else { + if (response.status > 499 && response.status < 600) { + logger.error(`Error communicating with ${url}`, { + ...loggingFields, + service: 'adminusers', + method: 'GET', + status_code: response.status, + url: fullUrl + }) + } + return response.data } + } catch (err) { + requestLogger.logRequestError(context, err, loggingFields) + incrementStatusCodeCounter(callingFunctionName, 'error') + throw err } - return baseClient - .get(url, params, null) - .then(response => { - requestLogger.logRequestEnd(context, response.statusCode, loggingFields) - incrementStatusCodeCounter(callingFunctionName, response.statusCode) - if (SUCCESS_CODES.includes(response.statusCode)) { - return new Service(response.body) - } else { - if (response.statusCode > 499 && response.statusCode < 600) { - logger.error(`Error communicating with ${url}`, { - ...loggingFields, - service: 'adminusers', - method: 'GET', - status_code: response.statusCode, - url: url - }) - } - return response.body - } - }).catch(err => { - requestLogger.logRequestError(context, err, loggingFields) - incrementStatusCodeCounter(callingFunctionName, 'error') - throw err - }) } const incrementStatusCodeCounter = (callingFunctionName, statusCode) => { @@ -69,8 +68,7 @@ const findServiceBy = function findServiceBy (findOptions, loggingFields = {}) { module.exports = function (clientOptions = {}) { baseUrl = clientOptions.baseUrl || process.env.ADMINUSERS_URL - correlationId = clientOptions.correlationId || '' return { - findServiceBy: findServiceBy + findServiceBy } }