Skip to content

Commit

Permalink
Merge pull request #3826 from alphagov/pp-11682-update-admin-client-t…
Browse files Browse the repository at this point in the history
…o-use-axios-base-client

PP-11682 Update `admin users client` to use axios base client
  • Loading branch information
iqbalgds authored May 9, 2024
2 parents 18861f7 + 851391a commit 025d66d
Showing 1 changed file with 31 additions and 33 deletions.
64 changes: 31 additions & 33 deletions app/services/clients/adminusers.client.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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,
Expand All @@ -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) => {
Expand All @@ -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
}
}

0 comments on commit 025d66d

Please sign in to comment.