Skip to content

Commit

Permalink
Upgrade OkHttp
Browse files Browse the repository at this point in the history
  • Loading branch information
kleewho authored May 23, 2022
1 parent 698b79f commit 769d2a0
Show file tree
Hide file tree
Showing 20 changed files with 100 additions and 150 deletions.
17 changes: 11 additions & 6 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: kotlin
version: 7.0.1
version: 7.1.0
schema: 1
scm: github.com/pubnub/kotlin
files:
- build/libs/pubnub-kotlin-7.0.1-all.jar
- build/libs/pubnub-kotlin-7.1.0-all.jar
sdks:
-
type: library
Expand All @@ -23,8 +23,8 @@ sdks:
-
distribution-type: library
distribution-repository: GitHub
package-name: pubnub-kotlin-7.0.1
location: https://github.com/pubnub/kotlin/releases/download/v7.0.1/pubnub-kotlin-7.0.1-all.jar
package-name: pubnub-kotlin-7.1.0
location: https://github.com/pubnub/kotlin/releases/download/v7.1.0/pubnub-kotlin-7.1.0-all.jar
supported-platforms:
supported-operating-systems:
Android:
Expand Down Expand Up @@ -135,8 +135,8 @@ sdks:
-
distribution-type: library
distribution-repository: maven
package-name: pubnub-kotlin-7.0.1
location: https://repo.maven.apache.org/maven2/com/pubnub/pubnub-kotlin/7.0.1/pubnub-kotlin-7.0.1.jar
package-name: pubnub-kotlin-7.1.0
location: https://repo.maven.apache.org/maven2/com/pubnub/pubnub-kotlin/7.1.0/pubnub-kotlin-7.1.0.jar
supported-platforms:
supported-operating-systems:
Android:
Expand Down Expand Up @@ -233,6 +233,11 @@ sdks:
license-url: https://github.com/stleary/JSON-java/blob/20210307/LICENSE
is-required: Required
changelog:
- date: 2022-05-23
version: v7.1.0
changes:
- type: feature
text: "Upgrade okhttp library ."
- date: 2022-04-19
version: v7.0.1
changes:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v7.1.0
May 23 2022

#### Added
- Upgrade okhttp library .

## v7.0.1
April 19 2022

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ You will need the publish and subscribe keys to authenticate your app. Get your
<dependency>
<groupId>com.pubnub</groupId>
<artifactId>pubnub-gson</artifactId>
<version>7.0.1</version>
<version>7.1.0</version>
</dependency>
```

* for Gradle, add the following dependency in your `gradle.build`:
```groovy
implementation 'com.pubnub:pubnub-kotlin:7.0.1'
implementation 'com.pubnub:pubnub-kotlin:7.1.0'
```

2. Configure your keys:
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ plugins {
}

group = 'com.pubnub'
version = '7.0.1'
version = '7.1.0'

repositories {
mavenCentral()
Expand Down Expand Up @@ -49,7 +49,7 @@ dependencies {

implementation "com.squareup.retrofit2:retrofit:2.9.0"

api "com.squareup.okhttp3:logging-interceptor:3.14.9"
api 'com.squareup.okhttp3:logging-interceptor:4.9.3'

api 'com.google.code.gson:gson:2.8.6'
implementation "com.squareup.retrofit2:converter-gson:2.9.0"
Expand Down
12 changes: 6 additions & 6 deletions src/main/kotlin/com/pubnub/api/Endpoint.kt
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ abstract class Endpoint<Input, Output> protected constructor(protected val pubnu
private fun storeRequestLatency(response: Response<Input>) {
pubnub.telemetryManager.storeLatency(
latency = with(response.raw()) {
receivedResponseAtMillis() - sentRequestAtMillis()
receivedResponseAtMillis - sentRequestAtMillis
},
type = operationType()
)
Expand Down Expand Up @@ -275,11 +275,11 @@ abstract class Endpoint<Input, Output> protected constructor(protected val pubnu

with(pnStatus) {
statusCode = it.code()
tlsEnabled = it.raw().request().url().isHttps
origin = it.raw().request().url().host()
uuid = it.raw().request().url().queryParameter("uuid")
authKey = it.raw().request().url().queryParameter("auth")
clientRequest = it.raw().request()
tlsEnabled = it.raw().request.url.isHttps
origin = it.raw().request.url.host
uuid = it.raw().request.url.queryParameter("uuid")
authKey = it.raw().request.url.queryParameter("auth")
clientRequest = it.raw().request
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/pubnub/api/PubNub.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class PubNub(val configuration: PNConfiguration) {

companion object {
private const val TIMESTAMP_DIVIDER = 1000
private const val SDK_VERSION = "7.0.1"
private const val SDK_VERSION = "7.1.0"
private const val MAX_SEQUENCE = 65535

/**
Expand Down
14 changes: 7 additions & 7 deletions src/main/kotlin/com/pubnub/api/PubNubUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ internal object PubNubUtil {
return originalRequest
}
val signature = generateSignature(pnConfiguration, originalRequest, timestamp)
val rebuiltUrl = originalRequest.url().newBuilder()
val rebuiltUrl = originalRequest.url.newBuilder()
.addQueryParameter("timestamp", timestamp.toString())
.addQueryParameter("signature", signature)
.build()
Expand Down Expand Up @@ -149,17 +149,17 @@ internal object PubNubUtil {
timestamp: Int
): String? {
val queryParams: MutableMap<String, String> = mutableMapOf()
for (queryKey: String in request.url().queryParameterNames()) {
val value = request.url().queryParameter(queryKey)
for (queryKey: String in request.url.queryParameterNames) {
val value = request.url.queryParameter(queryKey)
if (value != null) {
queryParams[queryKey] = value
}
}
return generateSignature(
configuration,
request.url().encodedPath(),
request.url.encodedPath,
queryParams,
request.method(),
request.method,
requestBodyToString(request),
timestamp
)
Expand All @@ -174,12 +174,12 @@ internal object PubNubUtil {
}

internal fun requestBodyToString(request: Request): String? {
if (request.body() == null) {
if (request.body == null) {
return ""
}
try {
val buffer = Buffer()
request.body()!!.writeTo(buffer)
request.body!!.writeTo(buffer)
return buffer.readUtf8()
} catch (e: IOException) {
e.printStackTrace()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class GetFileUrl(
pubnub.configuration,
pubnub.timestamp()
)
PNFileUrlResult(signedRequest.url().toString())
PNFileUrlResult(signedRequest.url.toString())
} catch (e: Exception) {
throw PubNubException(errorMessage = e.message)
}
Expand Down
13 changes: 7 additions & 6 deletions src/main/kotlin/com/pubnub/api/endpoints/files/UploadFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import com.pubnub.api.models.server.files.FormField
import com.pubnub.api.services.S3Service
import com.pubnub.api.vendor.FileEncryptionUtil
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import org.slf4j.LoggerFactory
import retrofit2.Call
import retrofit2.Callback
Expand Down Expand Up @@ -44,7 +45,7 @@ internal class UploadFile(
val mediaType = getMediaType(formParams.findContentType())

val bytes = prepareBytes(content, cipherKey)
builder.addFormDataPart(FILE_PART_MULTIPART, fileName, RequestBody.create(mediaType, bytes))
builder.addFormDataPart(FILE_PART_MULTIPART, fileName, bytes.toRequestBody(mediaType, 0, content.size))
return s3Service.upload(baseUrl, builder.build())
}

Expand All @@ -65,7 +66,7 @@ internal class UploadFile(
return if (contentType == null) {
APPLICATION_OCTET_STREAM
} else try {
MediaType.get(contentType)
contentType.toMediaType()
} catch (t: Throwable) {
log.warn("Content-Type: $contentType was not recognized by MediaType.get", t)
APPLICATION_OCTET_STREAM
Expand Down Expand Up @@ -199,8 +200,8 @@ internal class UploadFile(
category = category,
operation = operationType(),
statusCode = response?.code(),
tlsEnabled = response?.raw()?.request()?.url()?.isHttps,
origin = response?.raw()?.request()?.url()?.host(),
tlsEnabled = response?.raw()?.request?.url?.isHttps,
origin = response?.raw()?.request?.url?.host,
error = response == null || throwable != null
).apply { executedEndpoint = this@UploadFile }
}
Expand All @@ -225,7 +226,7 @@ internal class UploadFile(
}

companion object {
private val APPLICATION_OCTET_STREAM = MediaType.get("application/octet-stream")
private val APPLICATION_OCTET_STREAM = "application/octet-stream".toMediaType()
private const val CONTENT_TYPE_HEADER = "Content-Type"
private const val FILE_PART_MULTIPART = "file"
private val log = LoggerFactory.getLogger(UploadFile::class.java)
Expand Down
13 changes: 6 additions & 7 deletions src/main/kotlin/com/pubnub/api/managers/RetrofitManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import com.pubnub.api.services.S3Service
import com.pubnub.api.services.SignalService
import com.pubnub.api.services.SubscribeService
import com.pubnub.api.services.TimeService
import com.pubnub.okhttp3.PNCallFactory
import okhttp3.Call
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
Expand Down Expand Up @@ -59,7 +58,7 @@ class RetrofitManager(val pubnub: PubNub) {
}

val transactionInstance = createRetrofit(transactionClientInstance)
val subscriptionInstance = createRetrofit(PNCallFactory(subscriptionClientInstance))
val subscriptionInstance = createRetrofit(subscriptionClientInstance)
val noSignatureInstance = createRetrofit(noSignatureClientInstance)

timeService = transactionInstance.create(TimeService::class.java)
Expand All @@ -79,7 +78,7 @@ class RetrofitManager(val pubnub: PubNub) {
}

fun getTransactionClientExecutorService(): ExecutorService {
return transactionClientInstance.dispatcher().executorService()
return transactionClientInstance.dispatcher.executorService
}

private fun createOkHttpClient(readTimeout: Int, withSignature: Boolean = true): OkHttpClient {
Expand Down Expand Up @@ -121,7 +120,7 @@ class RetrofitManager(val pubnub: PubNub) {

val okHttpClient = okHttpBuilder.build()

pubnub.configuration.maximumConnections?.let { okHttpClient.dispatcher().maxRequestsPerHost = it }
pubnub.configuration.maximumConnections?.let { okHttpClient.dispatcher.maxRequestsPerHost = it }

return okHttpClient
}
Expand All @@ -142,10 +141,10 @@ class RetrofitManager(val pubnub: PubNub) {
}

private fun closeExecutor(client: OkHttpClient, force: Boolean) {
client.dispatcher().cancelAll()
client.dispatcher.cancelAll()
if (force) {
client.connectionPool().evictAll()
val executorService = client.dispatcher().executorService()
client.connectionPool.evictAll()
val executorService = client.dispatcher.executorService
executorService.shutdown()
}
}
Expand Down
56 changes: 0 additions & 56 deletions src/main/kotlin/com/pubnub/okhttp3/PNCall.kt

This file was deleted.

10 changes: 0 additions & 10 deletions src/main/kotlin/com/pubnub/okhttp3/PNCallFactory.kt

This file was deleted.

4 changes: 2 additions & 2 deletions src/test/kotlin/com/pubnub/api/legacy/BaseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.pubnub.api.CommonUtils.DEFAULT_LISTEN_DURATION
import com.pubnub.api.PNConfiguration
import com.pubnub.api.PubNub
import com.pubnub.api.enums.PNLogVerbosity
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import org.junit.After
import org.junit.Assert.assertTrue
import org.junit.Before
Expand Down Expand Up @@ -56,7 +56,7 @@ abstract class BaseTest {
subscribeKey = "mySubscribeKey"
publishKey = "myPublishKey"
uuid = "myUUID"
origin = HttpUrl.parse(wireMockServer.baseUrl())!!.run { "${host()}:${port()}" }
origin = wireMockServer.baseUrl().toHttpUrlOrNull()!!.run { "$host:$port" }
secure = false
logVerbosity = PNLogVerbosity.BODY
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/kotlin/com/pubnub/api/legacy/PubNubTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class PubNubTest : BaseTest() {
fun getVersionAndTimeStamp() {
val version = pubnub.version
val timeStamp = pubnub.timestamp()
assertEquals("7.0.1", version)
assertEquals("7.1.0", version)
assertTrue(timeStamp > 0)
}
}
Loading

0 comments on commit 769d2a0

Please sign in to comment.