-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
248 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
apply plugin: 'kotlin' | ||
apply plugin: 'com.vanniktech.maven.publish' | ||
|
||
dependencies { | ||
api project (':kalev-lib') | ||
|
||
implementation "org.jetbrains.kotlin:kotlin-stdlib:${versions.kotlin}" | ||
implementation "com.squareup.okhttp3:okhttp:3.12.12" | ||
compileOnly "org.json:json:20141113" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
POM_ARTIFACT_ID=kalev-okhttp | ||
POM_NAME=kalev-okhttp | ||
POM_PACKAGING=jar | ||
POM_DESCRIPTION=OkHttp logging interceptor with Kalev |
108 changes: 108 additions & 0 deletions
108
kalev-okhttp/src/main/java/eu/bolt/kalev/okhttp/LoggingInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package eu.bolt.kalev.okhttp | ||
|
||
import eu.bolt.kalev.Kalev | ||
import okhttp3.Interceptor | ||
import okhttp3.Interceptor.Chain | ||
import okhttp3.Request | ||
import okhttp3.Response | ||
import okhttp3.ResponseBody | ||
import okio.Buffer | ||
import org.json.JSONException | ||
import org.json.JSONObject | ||
import java.nio.charset.Charset | ||
|
||
class LoggingInterceptor : Interceptor { | ||
|
||
override fun intercept(chain: Chain): Response { | ||
val request = chain.request() | ||
val response: Response | ||
try { | ||
response = chain.proceed(request) | ||
} catch (e: Exception) { | ||
logError(request ,e) | ||
throw e | ||
} | ||
logResponse(response) | ||
return response | ||
} | ||
|
||
private fun logResponse(response: Response) { | ||
val url = response.request().url() | ||
val query = url.queryParameterNames().map { name -> name to url.queryParameter(name) }.toMap() | ||
|
||
val entry = Kalev.with("method", response.request().method()) | ||
.with("path", url.encodedPath()) | ||
|
||
query.entries.forEach { queryEntry -> | ||
entry.with(queryEntry.key, queryEntry.value ?: "") | ||
} | ||
|
||
formatRequestBody(response.request())?.let { | ||
try { | ||
entry.with("request.body", JSONObject(it)) | ||
} catch (exc: JSONException) { | ||
entry.with("request.body", it) | ||
} | ||
} | ||
|
||
entry.with("response.code", response.code()) | ||
formatResponseBody(response.body())?.let { | ||
try { | ||
entry.with("response.body", JSONObject(it)) | ||
} catch (exc: JSONException) { | ||
entry.with("response.body", it) | ||
} | ||
} | ||
entry.v(NETWORK_MESSAGE) | ||
} | ||
|
||
private fun formatRequestBody(request: Request): String? { | ||
if (request.method() == "GET") { | ||
return null | ||
} | ||
return request.body()?.let { | ||
val buffer = Buffer() | ||
it.writeTo(buffer) | ||
buffer.readString(Charset.forName("UTF-8")) | ||
} | ||
} | ||
|
||
private fun formatResponseBody(body: ResponseBody?): String? { | ||
return body?.let { | ||
val source = it.source() | ||
source.request(Long.MAX_VALUE) | ||
val buffer = source.buffer() | ||
buffer.clone().readString(Charset.forName("UTF-8")) | ||
} | ||
} | ||
|
||
private fun logError(request: Request, error: Throwable) { | ||
val url = request.url() | ||
val query = url.queryParameterNames().map { name -> name to url.queryParameter(name) }.toMap() | ||
|
||
val entry = Kalev.with("method", request.method()) | ||
.with("path", url.encodedPath()) | ||
|
||
query.entries.forEach { queryEntry -> | ||
entry.with(queryEntry.key, queryEntry.value ?: "") | ||
} | ||
|
||
formatRequestBody(request)?.let { | ||
try { | ||
entry.with("request.body", JSONObject(it)) | ||
} catch (exc: JSONException) { | ||
entry.with("request.body", it) | ||
} | ||
} | ||
|
||
entry.with("response.error", error.javaClass.name) | ||
error.message?.let { | ||
entry.with("response.error.message", it) | ||
} | ||
entry.e(NETWORK_MESSAGE) | ||
} | ||
|
||
companion object { | ||
const val NETWORK_MESSAGE = "network" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package eu.bolt.kalev.sample | ||
|
||
import eu.bolt.kalev.Kalev | ||
import eu.bolt.kalev.okhttp.LoggingInterceptor | ||
import okhttp3.Call | ||
import okhttp3.Callback | ||
import okhttp3.OkHttpClient | ||
import okhttp3.Request | ||
import okhttp3.Response | ||
import java.io.IOException | ||
|
||
class NetworkDemo { | ||
|
||
private val okHttpClient = OkHttpClient.Builder() | ||
.addNetworkInterceptor(LoggingInterceptor()) | ||
.build() | ||
|
||
fun doGet() { | ||
val request: Request = Request.Builder() | ||
.url("https://httpbin.org/get?id=42") | ||
.header("Content-Type", "application/json") | ||
.build() | ||
|
||
okHttpClient.newCall(request).enqueue(networkCallback) | ||
} | ||
|
||
fun doNotFound() { | ||
val request: Request = Request.Builder() | ||
.url("https://httpbin.org/unknown") | ||
.header("Content-Type", "application/json") | ||
.build() | ||
|
||
okHttpClient.newCall(request).enqueue(networkCallback) | ||
} | ||
|
||
private val networkCallback = object : Callback { | ||
|
||
override fun onResponse(call: Call, response: Response) { | ||
Kalev.d("Got response") | ||
} | ||
|
||
override fun onFailure(call: Call, e: IOException) { | ||
Kalev.e(e, "Got error") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
include ':kalev-lib', ':kalev-android', ':sample' | ||
include ':kalev-lib', ':kalev-android', ':sample', ':kalev-okhttp' | ||
rootProject.name = 'kalev' |