in app/services/GithubAPI.scala [97:134]
private def downloadArtifactsZip(providedUrl:Uri):Future[ByteString] = {
val maxIterationDepth = config.getOptional[Int]("github.max-redirects").getOrElse(10)
def recursiveFind(request:HttpRequest, iterations:Int=0):Future[HttpResponse] = {
getHttp
.singleRequest(request)
.flatMap(response => {
(response.status: @switch) match {
case StatusCodes.Found =>
response.header[Location] match {
case Some(realUri)=>
logger.info(s"Reading ${request.uri} redirected to $realUri")
if(iterations<maxIterationDepth) {
recursiveFind(HttpRequest(uri = realUri.uri), iterations + 1)
} else {
logger.error(s"Received too many layers of redirection, current limit is $maxIterationDepth.")
Future.failed(new RuntimeException("Too many redirects"))
}
case None=>
Future.failed(new RuntimeException("Received a 302 redirect without a location field, this is an error with the remote service"))
}
case StatusCodes.OK =>
Future(response)
case StatusCodes.NotFound=>
logger.error(s"No artifacts available for artifact id ${providedUrl.toString()}")
Future.failed(new RuntimeException("No artifacts available"))
case _=>
consumeResponseContent(response).flatMap(content=> {
logger.error(s"Could not get artifacts zip, server returned ${response.status} ${content.utf8String}")
Future.failed(new RuntimeException("External server error"))
})
}
})
}
val actualHeaders = Seq(Some(defaultGithubHeaders), githubToken.map(t=>Authorization(GenericHttpCredentials("token", t)))).collect({case Some(h)=>h})
recursiveFind(HttpRequest(uri=providedUrl, headers=actualHeaders)).flatMap(consumeResponseContent)
}