Skip to content

Commit

Permalink
Add custom body encoder to the sample app
Browse files Browse the repository at this point in the history
  • Loading branch information
MiSikora committed Feb 10, 2021
1 parent 733178f commit f0b44c6
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ buildscript {
gsonVersion = '2.8.6'
okhttpVersion = '4.9.1'
retrofitVersion = '2.9.0'
wireVersion = '3.6.0'

// Debug and quality control
binaryCompatibilityValidator = '0.2.4'
Expand Down Expand Up @@ -51,6 +52,7 @@ buildscript {
classpath "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:$detektVersion"
classpath "org.jlleitschuh.gradle:ktlint-gradle:$ktLintGradleVersion"
classpath "org.jetbrains.kotlinx:binary-compatibility-validator:$binaryCompatibilityValidator"
classpath "com.squareup.wire:wire-gradle-plugin:$wireVersion"
}
}
apply plugin: 'binary-compatibility-validator'
Expand Down
2 changes: 1 addition & 1 deletion gradle/kotlin-static-analysis.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ktlint {
include fileTree("scripts/")
}
filter {
exclude("**/generated/**")
exclude { element -> element.file.path.contains("generated/") }
include("**/kotlin/**")
}
}
Expand Down
9 changes: 9 additions & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'com.squareup.wire'

wire {
kotlin {}
}

android {
sourceSets {
getByName("main").java.srcDirs += "$buildDir/generated/source/wire/"
}

compileSdkVersion rootProject.compileSdkVersion

defaultConfig {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.chuckerteam.chucker.sample

import android.content.Context
import com.chuckerteam.chucker.api.BodyDecoder
import com.chuckerteam.chucker.api.ChuckerCollector
import com.chuckerteam.chucker.api.ChuckerInterceptor
import com.chuckerteam.chucker.api.RetentionManager
Expand All @@ -13,6 +14,7 @@ import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.logging.HttpLoggingInterceptor
import okio.Buffer
import okio.BufferedSink
import okio.ByteString
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
Expand All @@ -36,13 +38,14 @@ class HttpBinClient(
private val chuckerInterceptor = ChuckerInterceptor.Builder(context)
.collector(collector)
.maxContentLength(250000L)
.addBodyDecoder(PokemonProtoBodyDecoder())
.redactHeaders(emptySet())
.build()

private val httpClient =
OkHttpClient.Builder()
// Add a ChuckerInterceptor instance to your OkHttp client
.addInterceptor(chuckerInterceptor)
.addNetworkInterceptor(chuckerInterceptor)
.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
.build()

Expand Down Expand Up @@ -102,6 +105,7 @@ class HttpBinClient(
downloadSampleImage(colorHex = "fff")
downloadSampleImage(colorHex = "000")
getResponsePartially()
getProtoResponse()
}

private fun oneShotRequestBody() = object : RequestBody() {
Expand Down Expand Up @@ -145,4 +149,32 @@ class HttpBinClient(
}
)
}

private fun getProtoResponse() {
val pokemon = Pokemon("Pikachu", level = 99)
val body = pokemon.encodeByteString().toRequestBody("application/protobuf".toMediaType())
val request = Request.Builder()
.url("https://postman-echo.com/post")
.post(body)
.build()
httpClient.newCall(request).enqueue(
object : okhttp3.Callback {
override fun onFailure(call: okhttp3.Call, e: IOException) = Unit

override fun onResponse(call: okhttp3.Call, response: okhttp3.Response) {
response.body?.source()?.use { it.readByteString() }
}
}
)
}

private class PokemonProtoBodyDecoder : BodyDecoder {
override fun decodeRequest(request: Request, body: ByteString): String? {
return if (request.url.host.contains("postman", ignoreCase = true)) {
Pokemon.ADAPTER.decode(body).toString()
} else null
}

override fun decodeResponse(response: okhttp3.Response, body: ByteString): String? = null
}
}
10 changes: 10 additions & 0 deletions sample/src/main/proto/com/chuckerteam/chucker/sample/pokemon.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
syntax = "proto3";

package com.chuckerteam.chucker.sample;

option java_package = "com.chuckerteam.chucker.sample";

message Pokemon {
string name = 1;
int32 level = 2;
}

0 comments on commit f0b44c6

Please sign in to comment.