Skip to content

Commit

Permalink
quick wins with code smell
Browse files Browse the repository at this point in the history
  • Loading branch information
augustinas committed Sep 29, 2021
1 parent 2c4367b commit 194e081
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 38 deletions.
3 changes: 3 additions & 0 deletions src/main/scala/io/dataswift/hat/api/HatClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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))

Expand All @@ -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()

Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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})")

Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -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("")
Expand All @@ -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)))
Expand Down Expand Up @@ -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 =
Expand Down
16 changes: 8 additions & 8 deletions src/main/scala/io/dataswift/hat/api/services/HatDataDebits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()

Expand All @@ -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()

Expand Down Expand Up @@ -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))

Expand Down
12 changes: 6 additions & 6 deletions src/main/scala/io/dataswift/hat/api/services/HatRichData.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -102,7 +102,7 @@ trait HatRichData extends HatWsClient {
}

def getData(
access_token: String,
accessToken: String,
namespace: String,
endpoint: String,
recordId: Option[UUID] = None,
Expand All @@ -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()
Expand Down
10 changes: 5 additions & 5 deletions src/main/scala/io/dataswift/hat/api/services/HatSystem.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand All @@ -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 =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 194e081

Please sign in to comment.