2021-10-20 17:37:42 +02:00
|
|
|
package eu.dnetlib.dhp.datacite
|
2021-01-28 16:34:46 +01:00
|
|
|
|
|
|
|
import org.apache.commons.io.IOUtils
|
2021-07-28 10:30:49 +02:00
|
|
|
import org.apache.http.client.config.RequestConfig
|
2021-10-20 17:37:42 +02:00
|
|
|
import org.apache.http.client.methods.{HttpGet, HttpPost, HttpUriRequest}
|
2021-01-28 16:34:46 +01:00
|
|
|
import org.apache.http.entity.StringEntity
|
2021-10-20 17:37:42 +02:00
|
|
|
import org.apache.http.impl.client.HttpClientBuilder
|
2021-01-28 16:34:46 +01:00
|
|
|
|
2021-08-30 09:32:21 +02:00
|
|
|
abstract class AbstractRestClient extends Iterator[String] {
|
2021-01-28 16:34:46 +01:00
|
|
|
|
|
|
|
var buffer: List[String] = List()
|
2021-08-30 09:32:21 +02:00
|
|
|
var current_index: Int = 0
|
2021-01-28 16:34:46 +01:00
|
|
|
|
|
|
|
var scroll_value: Option[String] = None
|
|
|
|
|
2021-08-30 09:32:21 +02:00
|
|
|
var complete: Boolean = false
|
2021-01-28 16:34:46 +01:00
|
|
|
|
|
|
|
def extractInfo(input: String): Unit
|
|
|
|
|
|
|
|
protected def getBufferData(): Unit
|
|
|
|
|
2021-08-30 09:32:21 +02:00
|
|
|
def doHTTPGETRequest(url: String): String = {
|
2021-01-28 16:34:46 +01:00
|
|
|
val httpGet = new HttpGet(url)
|
|
|
|
doHTTPRequest(httpGet)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-08-30 09:32:21 +02:00
|
|
|
def doHTTPPOSTRequest(url: String, json: String): String = {
|
2021-01-28 16:34:46 +01:00
|
|
|
val httpPost = new HttpPost(url)
|
|
|
|
if (json != null) {
|
|
|
|
val entity = new StringEntity(json)
|
|
|
|
httpPost.setEntity(entity)
|
|
|
|
httpPost.setHeader("Accept", "application/json")
|
|
|
|
httpPost.setHeader("Content-type", "application/json")
|
|
|
|
}
|
|
|
|
doHTTPRequest(httpPost)
|
|
|
|
}
|
|
|
|
|
|
|
|
def hasNext: Boolean = {
|
|
|
|
buffer.nonEmpty && current_index < buffer.size
|
|
|
|
}
|
|
|
|
|
|
|
|
override def next(): String = {
|
2021-08-30 09:32:21 +02:00
|
|
|
val next_item: String = buffer(current_index)
|
2021-01-28 16:34:46 +01:00
|
|
|
current_index = current_index + 1
|
|
|
|
if (current_index == buffer.size)
|
|
|
|
getBufferData()
|
|
|
|
next_item
|
|
|
|
}
|
|
|
|
|
2021-08-30 09:32:21 +02:00
|
|
|
private def doHTTPRequest[A <: HttpUriRequest](r: A): String = {
|
2022-02-18 11:11:17 +01:00
|
|
|
val timeout = 600; // seconds
|
2022-01-11 16:57:48 +01:00
|
|
|
val config = RequestConfig
|
|
|
|
.custom()
|
2021-07-28 10:30:49 +02:00
|
|
|
.setConnectTimeout(timeout * 1000)
|
|
|
|
.setConnectionRequestTimeout(timeout * 1000)
|
2022-01-11 16:57:48 +01:00
|
|
|
.setSocketTimeout(timeout * 1000)
|
|
|
|
.build()
|
2021-08-30 09:32:21 +02:00
|
|
|
val client = HttpClientBuilder.create().setDefaultRequestConfig(config).build()
|
|
|
|
try {
|
|
|
|
var tries = 4
|
|
|
|
while (tries > 0) {
|
2021-03-26 13:56:29 +01:00
|
|
|
println(s"requesting ${r.getURI}")
|
2021-07-28 16:15:15 +02:00
|
|
|
try {
|
|
|
|
val response = client.execute(r)
|
|
|
|
println(s"get response with status${response.getStatusLine.getStatusCode}")
|
|
|
|
if (response.getStatusLine.getStatusCode > 400) {
|
|
|
|
tries -= 1
|
2022-01-11 16:57:48 +01:00
|
|
|
} else
|
2021-07-28 16:15:15 +02:00
|
|
|
return IOUtils.toString(response.getEntity.getContent)
|
|
|
|
} catch {
|
|
|
|
case e: Throwable =>
|
|
|
|
println(s"Error on requesting ${r.getURI}")
|
|
|
|
e.printStackTrace()
|
2021-08-30 09:32:21 +02:00
|
|
|
tries -= 1
|
2021-02-12 11:48:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
""
|
2021-08-30 09:32:21 +02:00
|
|
|
} finally {
|
|
|
|
if (client != null)
|
|
|
|
client.close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-28 16:34:46 +01:00
|
|
|
getBufferData()
|
2022-01-11 16:57:48 +01:00
|
|
|
}
|