From 194e0816e62e852fb2b8125d039500d26fc4b8fd Mon Sep 17 00:00:00 2001 From: Augustinas Markevicius Date: Wed, 29 Sep 2021 11:53:31 +0300 Subject: [PATCH] quick wins with code smell --- .../io/dataswift/hat/api/HatClient.scala | 3 +++ .../hat/api/services/HatApplications.scala | 16 +++++++-------- .../hat/api/services/HatAuthentication.scala | 20 +++++++++---------- .../hat/api/services/HatDataDebits.scala | 16 +++++++-------- .../hat/api/services/HatRichData.scala | 12 +++++------ .../hat/api/services/HatSystem.scala | 10 +++++----- .../hat/api/services/MockHatServer.scala | 2 +- 7 files changed, 41 insertions(+), 38 deletions(-) diff --git a/src/main/scala/io/dataswift/hat/api/HatClient.scala b/src/main/scala/io/dataswift/hat/api/HatClient.scala index 4120ae3..40f5466 100644 --- a/src/main/scala/io/dataswift/hat/api/HatClient.scala +++ b/src/main/scala/io/dataswift/hat/api/HatClient.scala @@ -19,6 +19,9 @@ trait HatWsClient extends Logging { protected val ws: WSClient protected val baseUrl: String protected val baseUrlWithPath: String + + protected val customAuthHeader: String = "X-Auth-Token" + protected val jsonHeader: (String, String) = "Accept" -> "application/json" } class HatClient( diff --git a/src/main/scala/io/dataswift/hat/api/services/HatApplications.scala b/src/main/scala/io/dataswift/hat/api/services/HatApplications.scala index 8f2c376..9ba4b37 100644 --- a/src/main/scala/io/dataswift/hat/api/services/HatApplications.scala +++ b/src/main/scala/io/dataswift/hat/api/services/HatApplications.scala @@ -24,11 +24,11 @@ trait HatApplications extends HatWsClient { import io.dataswift.models.hat.json.ApplicationJsonProtocol._ @Deprecated - def getApplications(access_token: String)(implicit ec: ExecutionContext): Future[Seq[HatService]] = { + def getApplications(accessToken: String)(implicit ec: ExecutionContext): Future[Seq[HatService]] = { val request: WSRequest = ws .url(s"$baseUrl/api/v2/application") - .withHttpHeaders("Accept" -> "application/json", "X-Auth-Token" -> access_token) + .withHttpHeaders(jsonHeader, customAuthHeader -> accessToken) val futureResponse: Future[WSResponse] = request.get() @@ -52,12 +52,12 @@ trait HatApplications extends HatWsClient { @Deprecated def saveApplication( - access_token: String, + accessToken: String, application: HatService )(implicit ec: ExecutionContext): Future[HatService] = { val request: WSRequest = ws .url(s"$baseUrl/api/v2/application") - .withHttpHeaders("Accept" -> "application/json", "X-Auth-Token" -> access_token) + .withHttpHeaders(jsonHeader, customAuthHeader -> accessToken) val futureResponse: Future[WSResponse] = request.post(Json.toJson(application)) @@ -82,7 +82,7 @@ trait HatApplications extends HatWsClient { def getAllApplications(accessToken: String)(implicit ec: ExecutionContext): Future[Seq[HatApplication]] = { val request: WSRequest = ws .url(s"$baseUrlWithPath/applications") - .withHttpHeaders("Accept" -> "application/json", "X-Auth-Token" -> accessToken) + .withHttpHeaders(jsonHeader, customAuthHeader -> accessToken) val eventualResponse: Future[WSResponse] = request.get() @@ -110,7 +110,7 @@ trait HatApplications extends HatWsClient { )(implicit ec: ExecutionContext): Future[Boolean] = { implicit val request: WSRequest = ws .url(s"$baseUrlWithPath/applications/$applicationId/setup") - .withHttpHeaders("Accept" -> "application/json", "X-Auth-Token" -> accessToken) + .withHttpHeaders(jsonHeader, customAuthHeader -> accessToken) transitionApplication } @@ -121,7 +121,7 @@ trait HatApplications extends HatWsClient { )(implicit ec: ExecutionContext): Future[Boolean] = { implicit val request: WSRequest = ws .url(s"$baseUrlWithPath/applications/$applicationId/disable") - .withHttpHeaders("Accept" -> "application/json", "X-Auth-Token" -> accessToken) + .withHttpHeaders(jsonHeader, customAuthHeader -> accessToken) transitionApplication } @@ -132,7 +132,7 @@ trait HatApplications extends HatWsClient { )(implicit ec: ExecutionContext): Future[String] = { val request: WSRequest = ws .url(s"$baseUrlWithPath/applications/$applicationId/access-token") - .withHttpHeaders("Accept" -> "application/json", "X-Auth-Token" -> accessToken) + .withHttpHeaders(jsonHeader, customAuthHeader -> accessToken) val eventualResponse: Future[WSResponse] = request.get() diff --git a/src/main/scala/io/dataswift/hat/api/services/HatAuthentication.scala b/src/main/scala/io/dataswift/hat/api/services/HatAuthentication.scala index e445cb8..17a0c77 100644 --- a/src/main/scala/io/dataswift/hat/api/services/HatAuthentication.scala +++ b/src/main/scala/io/dataswift/hat/api/services/HatAuthentication.scala @@ -44,8 +44,8 @@ trait HatAuthentication extends HatWsClient { password: String )(implicit ec: ExecutionContext): Future[String] = { val request: WSRequest = ws - .url(s"$baseUrl/users/access_token") - .withHttpHeaders("Accept" -> "application/json", "username" -> username, "password" -> password) + .url(s"$baseUrl/users/accessToken") + .withHttpHeaders(jsonHeader, "username" -> username, "password" -> password) logger.debug(s"Authenticate for token with HAT at ${request.method} ${request.url} (headers: ${request.headers})") @@ -72,12 +72,12 @@ trait HatAuthentication extends HatWsClient { } def createAccount( - access_token: String, + accessToken: String, hatUser: User )(implicit ec: ExecutionContext): Future[UUID] = { val request: WSRequest = ws .url(s"$baseUrl/users/user") - .withHttpHeaders("Accept" -> "application/json", "X-Auth-Token" -> access_token) + .withHttpHeaders(jsonHeader, customAuthHeader -> accessToken) logger.debug(s"Create account request ${request.uri}") val futureResponse: Future[WSResponse] = request.post(Json.toJson(hatUser)) @@ -94,12 +94,12 @@ trait HatAuthentication extends HatWsClient { } def updateAccount( - access_token: String, + accessToken: String, hatUser: User )(implicit ec: ExecutionContext): Future[UUID] = { val request: WSRequest = ws .url(s"$baseUrl/users/user/${hatUser.userId}/update") - .withHttpHeaders("Accept" -> "application/json", "X-Auth-Token" -> access_token) + .withHttpHeaders(jsonHeader, customAuthHeader -> accessToken) logger.debug(s"Update account request ${request.uri}") val futureResponse: Future[WSResponse] = request.put(Json.toJson(hatUser)) @@ -116,12 +116,12 @@ trait HatAuthentication extends HatWsClient { } def enableAccount( - access_token: String, + accessToken: String, userId: UUID )(implicit ec: ExecutionContext): Future[Boolean] = { val request: WSRequest = ws .url(s"$baseUrl/users/user/$userId/enable") - .withHttpHeaders("Accept" -> "application/json", "X-Auth-Token" -> access_token) + .withHttpHeaders(jsonHeader, customAuthHeader -> accessToken) logger.debug(s"Enable account $userId on $baseUrl") val futureResponse: Future[WSResponse] = request.put("") @@ -146,7 +146,7 @@ trait HatAuthentication extends HatWsClient { val request: WSRequest = ws .url(s"$baseUrl/control/v2/auth/request-verification") .withQueryStringParameters("lang" -> lang.language) - .withHttpHeaders("Accept" -> "application/json") + .withHttpHeaders(jsonHeader) logger.debug(s"Trigger HAT claim process on $baseUrl") val eventualResponse = request.post(Json.toJson(PdaEmailVerificationRequest(email, applicationId, redirectUri))) @@ -176,7 +176,7 @@ trait HatAuthentication extends HatWsClient { val request: WSRequest = ws .url(s"$baseUrl/control/v2/auth/claim") .withQueryStringParameters("lang" -> lang) - .withHttpHeaders("Accept" -> "application/json") + .withHttpHeaders(jsonHeader) logger.debug(s"Trigger HAT claim process on $baseUrl") val eventualResponse = diff --git a/src/main/scala/io/dataswift/hat/api/services/HatDataDebits.scala b/src/main/scala/io/dataswift/hat/api/services/HatDataDebits.scala index 640c1a8..f1c1a3a 100644 --- a/src/main/scala/io/dataswift/hat/api/services/HatDataDebits.scala +++ b/src/main/scala/io/dataswift/hat/api/services/HatDataDebits.scala @@ -13,13 +13,13 @@ trait HatDataDebits extends HatWsClient { import io.dataswift.models.hat.json.RichDataJsonFormats._ def getDataDebit( - access_token: String, + accessToken: String, dataDebitId: String )(implicit ec: ExecutionContext): Future[DataDebit] = { val request: WSRequest = ws .url(s"$baseUrlWithPath/data-debit/$dataDebitId") - .withHttpHeaders("Accept" -> "application/json", "X-Auth-Token" -> access_token) + .withHttpHeaders(jsonHeader, customAuthHeader -> accessToken) val futureResponse: Future[WSResponse] = request.get() @@ -46,11 +46,11 @@ trait HatDataDebits extends HatWsClient { } } - def listDataDebits(access_token: String)(implicit ec: ExecutionContext): Future[Seq[DataDebit]] = { + def listDataDebits(accessToken: String)(implicit ec: ExecutionContext): Future[Seq[DataDebit]] = { val request: WSRequest = ws .url(s"$baseUrlWithPath/data-debit") - .withHttpHeaders("Accept" -> "application/json", "X-Auth-Token" -> access_token) + .withHttpHeaders(jsonHeader, customAuthHeader -> accessToken) val futureResponse: Future[WSResponse] = request.get() @@ -74,13 +74,13 @@ trait HatDataDebits extends HatWsClient { } def getDataDebitValues( - access_token: String, + accessToken: String, dataDebitId: String )(implicit ec: ExecutionContext): Future[DataDebitData] = { val request: WSRequest = ws .url(s"$baseUrlWithPath/data-debit/$dataDebitId/values") - .withHttpHeaders("Accept" -> "application/json", "X-Auth-Token" -> access_token) + .withHttpHeaders(jsonHeader, customAuthHeader -> accessToken) val futureResponse: Future[WSResponse] = request.get() @@ -108,13 +108,13 @@ trait HatDataDebits extends HatWsClient { } def registerDataDebit( - access_token: String, + accessToken: String, dataDebitId: String, dataDebit: DataDebitSetupRequest )(implicit ec: ExecutionContext): Future[DataDebit] = { val request: WSRequest = ws .url(s"$baseUrlWithPath/data-debit/$dataDebitId") - .withHttpHeaders("Accept" -> "application/json", "X-Auth-Token" -> access_token) + .withHttpHeaders(jsonHeader, customAuthHeader -> accessToken) val futureResponse: Future[WSResponse] = request.post(Json.toJson(dataDebit)) diff --git a/src/main/scala/io/dataswift/hat/api/services/HatRichData.scala b/src/main/scala/io/dataswift/hat/api/services/HatRichData.scala index f1e4b0a..eef2deb 100644 --- a/src/main/scala/io/dataswift/hat/api/services/HatRichData.scala +++ b/src/main/scala/io/dataswift/hat/api/services/HatRichData.scala @@ -23,7 +23,7 @@ trait HatRichData extends HatWsClient { import io.dataswift.models.hat.json.RichDataJsonFormats._ def saveData( - access_token: String, + accessToken: String, namespace: String, endpoint: String, data: JsArray, @@ -32,7 +32,7 @@ trait HatRichData extends HatWsClient { val request: WSRequest = ws .url(s"$baseUrlWithPath/data/$namespace/$endpoint") - .withHttpHeaders("Accept" -> "application/json", "X-Auth-Token" -> access_token) + .withHttpHeaders(jsonHeader, customAuthHeader -> accessToken) .withQueryStringParameters("skipErrors" -> skipErrors.toString) val futureResponse: Future[WSResponse] = request.post(data) @@ -67,12 +67,12 @@ trait HatRichData extends HatWsClient { } def saveData( - access_token: String, + accessToken: String, data: Seq[EndpointData] )(implicit ec: ExecutionContext): Future[Seq[EndpointData]] = { val request: WSRequest = ws .url(s"$baseUrlWithPath/data-batch") - .withHttpHeaders("Accept" -> "application/json", "X-Auth-Token" -> access_token) + .withHttpHeaders(jsonHeader, customAuthHeader -> accessToken) val futureResponse: Future[WSResponse] = request.post(Json.toJson(data)) @@ -102,7 +102,7 @@ trait HatRichData extends HatWsClient { } def getData( - access_token: String, + accessToken: String, namespace: String, endpoint: String, recordId: Option[UUID] = None, @@ -122,7 +122,7 @@ trait HatRichData extends HatWsClient { val request: WSRequest = ws .url(s"$baseUrlWithPath/data/$namespace/$endpoint") - .withHttpHeaders("Accept" -> "application/json", "X-Auth-Token" -> access_token) + .withHttpHeaders(jsonHeader, customAuthHeader -> accessToken) .withQueryStringParameters(queryParameter: _*) val futureResponse: Future[WSResponse] = request.get() diff --git a/src/main/scala/io/dataswift/hat/api/services/HatSystem.scala b/src/main/scala/io/dataswift/hat/api/services/HatSystem.scala index 609fac1..5768afe 100644 --- a/src/main/scala/io/dataswift/hat/api/services/HatSystem.scala +++ b/src/main/scala/io/dataswift/hat/api/services/HatSystem.scala @@ -16,12 +16,12 @@ import play.api.libs.ws._ import scala.concurrent.{ ExecutionContext, Future } trait HatSystem extends HatWsClient { - def update(access_token: String)(implicit ec: ExecutionContext): Future[Unit] = { + def update(accessToken: String)(implicit ec: ExecutionContext): Future[Unit] = { logger.debug(s"Update HAT database") val request: WSRequest = ws .url(s"$baseUrl/system/update") - .withHttpHeaders("Accept" -> "application/json", "X-Auth-Token" -> access_token) + .withHttpHeaders(jsonHeader, customAuthHeader -> accessToken) val futureResponse: Future[WSResponse] = request.get() futureResponse.flatMap { response => @@ -35,16 +35,16 @@ trait HatSystem extends HatWsClient { } /** - * @param access_token - Expect Milliner Shared Secret + * @param accessToken - Expect Milliner Shared Secret * @param ec * @return */ - def destroyCache(access_token: String)(implicit ec: ExecutionContext): Future[Unit] = { + def destroyCache(accessToken: String)(implicit ec: ExecutionContext): Future[Unit] = { logger.debug(s"Destroying HAT Cache") val request: WSRequest = ws .url(s"$baseUrlWithPath/system/destroy-cache") - .withHttpHeaders("Accept" -> "application/json", "X-Auth-Token" -> access_token) + .withHttpHeaders(jsonHeader, customAuthHeader -> accessToken) val futureResponse: Future[WSResponse] = request.delete() futureResponse.flatMap { response => diff --git a/src/main/scala/io/dataswift/hat/api/services/MockHatServer.scala b/src/main/scala/io/dataswift/hat/api/services/MockHatServer.scala index 3f0f220..ad8f5a2 100644 --- a/src/main/scala/io/dataswift/hat/api/services/MockHatServer.scala +++ b/src/main/scala/io/dataswift/hat/api/services/MockHatServer.scala @@ -44,7 +44,7 @@ object MockHatServer { Action { Results.Ok.sendResource("hat-test-messages/testPublicKey.pem") } - case GET(p"/users/access_token") => + case GET(p"/users/accessToken") => Action { request => // Logger.info("Responding to access token request") val requestHeaders = request.headers.toSimpleMap