Created
April 16, 2018 10:49
-
-
Save timrobbins1/66b5913358774768b92927e33ed919c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private def issueSignedRequest(path: String, params: Map[String, String] = Map(), | |
post: Boolean = false): Future[JsonObject] = { | |
val sortedParams = params.map(p => p._1 + "=" + p._2).toList.sorted | |
val queryString = sortedParams.mkString("&") | |
val nonce = Instant.now().toEpochMilli.toString | |
val strForSign = path + "/" + nonce + "/" + queryString | |
val strForSign64 = Base64.getEncoder.encodeToString(strForSign.getBytes(charset)) | |
val mac = Mac.getInstance(hmacAlgo) | |
mac.init(new SecretKeySpec(secret.getBytes(charset), hmacAlgo)) | |
mac.update(strForSign64.getBytes(charset)) | |
val macBytes = mac.doFinal() | |
val macString = hexBytes(macBytes) | |
Marshal(emptyJsonObject).to[MessageEntity].flatMap { reqEntity => | |
val req = HttpRequest(method = if (post) HttpMethods.POST else HttpMethods.GET, | |
uri = Uri(path).resolvedAgainst(baseUrl).withRawQueryString(queryString), | |
headers = List(RawHeader("KC-API-KEY", apiKey), | |
RawHeader("KC-API-SIGNATURE", macString), | |
RawHeader("KC-API-NONCE", nonce)), | |
entity = if (post) reqEntity else HttpEntity.Empty) | |
Http().singleRequest(req).flatMap { | |
case HttpResponse(StatusCodes.OK, _, respEntity, _) => | |
Unmarshal(respEntity).to[JsonObject] | |
case HttpResponse(_, _, respEntity, _) => | |
Unmarshal(respEntity).to[JsonObject].map(o => | |
throw new RuntimeException(o.getString("msg"))) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment