From 68505aad4a22686594965bc910e60c8d2b6a95d4 Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Sun, 19 May 2019 15:15:24 +0800 Subject: Bump ammonite to 1.6.7 (#610) * bump ammonite to 1.6.7 * upgrade all the things * add scalaj-http shims for bootstrapping * wip * tweak-error-message * tweak coursier * . --- scalalib/src/publish/SonatypeHttpApi.scala | 116 +++++++++++++++-------------- 1 file changed, 60 insertions(+), 56 deletions(-) (limited to 'scalalib/src/publish/SonatypeHttpApi.scala') diff --git a/scalalib/src/publish/SonatypeHttpApi.scala b/scalalib/src/publish/SonatypeHttpApi.scala index 12defa93..217d556e 100644 --- a/scalalib/src/publish/SonatypeHttpApi.scala +++ b/scalalib/src/publish/SonatypeHttpApi.scala @@ -5,18 +5,10 @@ import java.util.Base64 import scala.concurrent.duration._ -import scalaj.http.{BaseHttp, HttpOptions, HttpRequest, HttpResponse} - -object PatientHttp - extends BaseHttp( - options = Seq( - HttpOptions.connTimeout(5.seconds.toMillis.toInt), - HttpOptions.readTimeout(1.minute.toMillis.toInt), - HttpOptions.followRedirects(false) - ) - ) + class SonatypeHttpApi(uri: String, credentials: String) { + val http = requests.Session(connectTimeout = 5000, readTimeout = 1000, maxRedirects = 0) private val base64Creds = base64(credentials) @@ -29,12 +21,19 @@ class SonatypeHttpApi(uri: String, credentials: String) { // https://oss.sonatype.org/nexus-staging-plugin/default/docs/path__staging_profiles.html def getStagingProfileUri(groupId: String): String = { val response = withRetry( - PatientHttp(s"$uri/staging/profiles").headers(commonHeaders)) - .throwError + http.get( + s"$uri/staging/profiles", + headers = commonHeaders + ) + ) + + if (!response.is2xx) { + throw new Exception(s"$uri/staging/profiles returned ${response.statusCode}") + } val resourceUri = ujson - .read(response.body)("data") + .read(response.data.text)("data") .arr .find(profile => groupId.split('.').startsWith(profile("name").str.split('.'))) @@ -47,79 +46,84 @@ class SonatypeHttpApi(uri: String, credentials: String) { } def getStagingRepoState(stagingRepoId: String): String = { - val response = PatientHttp(s"${uri}/staging/repository/${stagingRepoId}") - .option(HttpOptions.readTimeout(60000)) - .headers(commonHeaders) - .asString - .throwError - - ujson.read(response.body)("type").str.toString + val response = http.get( + s"${uri}/staging/repository/${stagingRepoId}", + readTimeout = 60000, + headers = commonHeaders + ) + ujson.read(response.data.text)("type").str.toString } // https://oss.sonatype.org/nexus-staging-plugin/default/docs/path__staging_profiles_-profileIdKey-_start.html def createStagingRepo(profileUri: String, groupId: String): String = { - val response = withRetry(PatientHttp(s"${profileUri}/start") - .headers(commonHeaders) - .postData( - s"""{"data": {"description": "fresh staging profile for ${groupId}"}}""")) - .throwError + val response = http.post( + s"${profileUri}/start", + headers = commonHeaders, + data = s"""{"data": {"description": "fresh staging profile for ${groupId}"}}""" + ) - ujson.read(response.body)("data")("stagedRepositoryId").str.toString + if (!response.is2xx) { + throw new Exception(s"$uri/staging/profiles returned ${response.statusCode}") + } + + ujson.read(response.data.text)("data")("stagedRepositoryId").str.toString } // https://oss.sonatype.org/nexus-staging-plugin/default/docs/path__staging_profiles_-profileIdKey-_finish.html def closeStagingRepo(profileUri: String, repositoryId: String): Boolean = { val response = withRetry( - PatientHttp(s"${profileUri}/finish") - .headers(commonHeaders) - .postData( - s"""{"data": {"stagedRepositoryId": "${repositoryId}", "description": "closing staging repository"}}""" - )) + http.post( + s"${profileUri}/finish", + headers = commonHeaders, + data = s"""{"data": {"stagedRepositoryId": "${repositoryId}", "description": "closing staging repository"}}""" + ) + ) - response.code == 201 + response.statusCode == 201 } // https://oss.sonatype.org/nexus-staging-plugin/default/docs/path__staging_profiles_-profileIdKey-_promote.html def promoteStagingRepo(profileUri: String, repositoryId: String): Boolean = { val response = withRetry( - PatientHttp(s"${profileUri}/promote") - .headers(commonHeaders) - .postData( - s"""{"data": {"stagedRepositoryId": "${repositoryId}", "description": "promote staging repository"}}""" - )) + http.post( + s"${profileUri}/promote", + headers = commonHeaders, + data = s"""{"data": {"stagedRepositoryId": "${repositoryId}", "description": "promote staging repository"}}""" + ) + ) - response.code == 201 + response.statusCode == 201 } // https://oss.sonatype.org/nexus-staging-plugin/default/docs/path__staging_profiles_-profileIdKey-_drop.html def dropStagingRepo(profileUri: String, repositoryId: String): Boolean = { val response = withRetry( - PatientHttp(s"${profileUri}/drop") - .headers(commonHeaders) - .postData( - s"""{"data": {"stagedRepositoryId": "${repositoryId}", "description": "drop staging repository"}}""" - )) - - response.code == 201 + http.post( + s"${profileUri}/drop", + headers = commonHeaders, + data = s"""{"data": {"stagedRepositoryId": "${repositoryId}", "description": "drop staging repository"}}""" + ) + ) + response.statusCode == 201 } private val uploadTimeout = 5.minutes.toMillis.toInt - def upload(uri: String, data: Array[Byte]): HttpResponse[String] = { - PatientHttp(uri) - .option(HttpOptions.readTimeout(uploadTimeout)) - .method("PUT") - .headers( + def upload(uri: String, data: Array[Byte]): requests.Response = { + http.put( + uri, + readTimeout = uploadTimeout, + headers = Seq( "Content-Type" -> "application/binary", "Authorization" -> s"Basic ${base64Creds}" - ) - .put(data) - .asString + ), + data = data + ) } - private def withRetry(request: HttpRequest, - retries: Int = 10): HttpResponse[String] = { - val resp = request.asString + private def withRetry(request: => requests.Response, + retries: Int = 10): requests.Response = { + val resp = request if (resp.is5xx && retries > 0) { Thread.sleep(500) withRetry(request, retries - 1) -- cgit v1.2.3