SHARE
TWEET

Untitled

a guest Aug 21st, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.comarch.iaa.bd.servicescore.rest
  2.  
  3. import java.net.URLEncoder
  4.  
  5. import akka.http.scaladsl.model.{HttpMethods, StatusCodes}
  6. import com.comarch.iaa.bd.servicescore.GenericDecode
  7. import com.comarch.iaa.bd.servicescore.properties.ServicesProperties
  8. import com.comarch.iaa.bd.servicescore.properties.ServicesProperties.{restClientConnTimeoutMs, restClientEncodingSpace, restClientReadTimeoutMs}
  9. import com.comarch.iaa.bd.servicescore.utils.ServiceUtil._
  10. import com.comarch.oss.bd.log.LogWrapper
  11. import io.circe.Decoder
  12. import scalaj.http.{Http, HttpRequest, HttpResponse}
  13.  
  14. import scala.reflect.runtime.universe.TypeTag
  15.  
  16. object RestClient extends RestUtils with LogWrapper {
  17.  
  18.   val getMethod: String = HttpMethods.GET.value
  19.  
  20.   private val space: String = " "
  21.   private val encodedSpace: String = URLEncoder.encode(space, restClientEncodingSpace)
  22.  
  23.   def httpRequestWithToken(name: String, additionalPath: String = "", method: String = getMethod, token: String = AuthToken.createToken(), params: Seq[(String, String)] = Seq.empty): HttpRequest = {
  24.     val path = servicePathWithToken(name, token)
  25.     Http(encodeSpaces(path + additionalPath))
  26.       .header(Authorization, headerToken(token))
  27.       .timeout(restClientConnTimeoutMs, restClientReadTimeoutMs)
  28.       .params(params)
  29.       .method(method)
  30.   }
  31.  
  32.   private def encodeSpaces(url: String): String = {
  33.     url.replaceAll(space, encodedSpace)
  34.   }
  35.  
  36.   def jsonFromRequestWithToken(name: String, additionalPath: String = "", method: String = getMethod, token: String = AuthToken.createToken(), params: Seq[(String, String)] = Seq.empty): String = {
  37.       requestWithToken(name, additionalPath, method, token, params).body
  38.   }
  39.  
  40.   def requestWithToken(name: String, additionalPath: String = "", method: String = getMethod, token: String, params: Seq[(String, String)] = Seq.empty): HttpResponse[String] = {
  41.     val response = httpRequestWithToken(name, additionalPath, method, token, params = params)
  42.       .header(ContentType, ApplicationJson)
  43.       .asString
  44.  
  45.     checkForError(response, name)
  46.     response
  47.   }
  48.  
  49.   @deprecated
  50.   def httpRequestWithBasicAuth(name: String, additionalPath: String = "", method: String = getMethod, headers: Seq[(String, String)] = Seq.empty, credentials: Credentials = OssCredentials, params: Seq[(String, String)] = Seq.empty): HttpRequest = {
  51.     val path = servicePath(name)
  52.     Http(path + additionalPath)
  53.       .timeout(restClientConnTimeoutMs, restClientReadTimeoutMs)
  54.       .auth(credentials.username, credentials.password)
  55.       .headers(headers)
  56.       .params(params)
  57.       .method(method)
  58.   }
  59.  
  60.   @deprecated
  61.   def jsonFromRequestWithBasicAuthFromPath(name: String, method: String = getMethod, params: Seq[(String, String)] = Seq.empty): String = {
  62.     val response = httpRequestWithBasicAuthFromPath(name, method, params = params)
  63.       .header(ContentType, ApplicationJson)
  64.       .asString
  65.  
  66.     checkForError(response, name)
  67.     response.body
  68.   }
  69.  
  70.   def httpRequestWithBasicAuthFromPath(path: String, method: String = getMethod, credentials: Credentials = OssCredentials, params: Seq[(String, String)] = Seq.empty): HttpRequest = {
  71.     Http(path)
  72.       .auth(credentials.username, credentials.password)
  73.       .method(method)
  74.       .params(params)
  75.       .timeout(restClientConnTimeoutMs, restClientReadTimeoutMs)
  76.   }
  77.  
  78.   def httpRequestWithoutAuthFromPath(path: String, method: String = getMethod): HttpRequest = {
  79.     Http(path)
  80.       .method(method)
  81.       .timeout(restClientConnTimeoutMs, restClientReadTimeoutMs)
  82.   }
  83.  
  84.   def httpRequestPutJsonWithToken(name: String, additionalPath: String, toPutJSON: String, method: String = "put", token: String = AuthToken.createToken(), useServiceDiscovery: Boolean = ServicesProperties.bigDataUseServiceDiscovery): HttpResponse[String] = {
  85.     val response = Http(servicePathWithToken(name, token, useServiceDiscovery) + additionalPath)
  86.       .headers(Seq(Authorization -> headerToken(token), ContentType -> ApplicationJson))
  87.       .put(toPutJSON)
  88.       .method(method)
  89.       .timeout(restClientConnTimeoutMs, restClientReadTimeoutMs)
  90.       .asString
  91.  
  92.     checkForError(response, name)
  93.     response
  94.   }
  95.  
  96.  
  97.   def jsonFromPostRequestWithToken(name: String, additionalPath: String, body: String, token: String): String = {
  98.     val response = Http(servicePathWithToken(name, token) + additionalPath)
  99.       .headers(Seq(Authorization -> headerToken(token), ContentType -> ApplicationJson))
  100.       .put(body)
  101.       .method("post")
  102.       .timeout(restClientConnTimeoutMs, restClientReadTimeoutMs)
  103.       .asString
  104.  
  105.     checkForError(response, name)
  106.     response.body
  107.   }
  108.  
  109.   def checkForError(httpResponse: HttpResponse[String], name: String): Unit = {
  110.     if (httpResponse.isError) {
  111.       log.error(s"Error in: $name. Response: ${httpResponse.body}")
  112.     }
  113.   }
  114.  
  115.   def httpRequestWithFilters(name: String, additionalPath: String = "", otherParams: Map[String, String] = Map.empty, method: String = getMethod, token: String = AuthToken.createToken()): HttpRequest = {
  116.     filtersAutomaticExtract(name, additionalPath, otherParams, method, token)
  117.   }
  118.  
  119.   def jsonFromRequestWithFilters(name: String, additionalPath: String = "", otherParams: Map[String, String] = Map.empty, method: String = getMethod, token: String = AuthToken.createToken()): String = {
  120.     val response = httpRequestWithFilters(name, additionalPath, otherParams, method, token)
  121.       .asString
  122.  
  123.     checkForError(response, name)
  124.     response.body
  125.   }
  126.  
  127.   implicit class ToResponse(httpRequest: HttpRequest) {
  128.     def toResponse: HttpResponse[String] = {
  129.       httpRequest.asString
  130.     }
  131.  
  132.     def decode[T: TypeTag](implicit decoder: Decoder[T]): Option[T] = GenericDecode.genericDecode(toResponse.body)
  133.  
  134.     def decodeWithException[T: TypeTag](implicit decoder: Decoder[T]): Option[T] = {
  135.       if (toResponse.code.equals(StatusCodes.NotFound.intValue)) None else {
  136.         Some(GenericDecode.genericDecodeWithException(toResponse.body))
  137.       }
  138.     }
  139.  
  140.     def decodeList[T: TypeTag](implicit decoder: Decoder[T]): List[T] = GenericDecode.genericListDecode(toResponse.body)
  141.   }
  142.  
  143.   implicit class ResponseDecoder(httpResponse: HttpResponse[String]) {
  144.     def decode[T: TypeTag](implicit decoder: Decoder[T]): Option[T] = GenericDecode.genericDecode(httpResponse.body)
  145.  
  146.     def decodeWithException[T: TypeTag](implicit decoder: Decoder[T]): Option[T] = {
  147.       if (httpResponse.code.equals(StatusCodes.NotFound.intValue)) None else {
  148.         Some(GenericDecode.genericDecodeWithException(httpResponse.body))
  149.       }
  150.     }
  151.  
  152.     def decodeList[T: TypeTag](implicit decoder: Decoder[T]): List[T] = GenericDecode.genericListDecode(httpResponse.body)
  153.   }
  154. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top