Implemented using HttpLoggingInterceptor instead of manual logging. Closes #3.
This commit is contained in:
parent
411e63a0ba
commit
505eee98f0
5 changed files with 29 additions and 14 deletions
|
@ -35,6 +35,7 @@ package net.thauvin.erik.pinboard
|
|||
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.logging.HttpLoggingInterceptor
|
||||
import org.xml.sax.InputSource
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
|
@ -123,7 +124,15 @@ open class PinboardPoster() {
|
|||
@Suppress("MemberVisibilityCanBePrivate")
|
||||
val logger: Logger by lazy { Logger.getLogger(PinboardPoster::class.java.simpleName) }
|
||||
|
||||
private val client by lazy { OkHttpClient() }
|
||||
private val client by lazy {
|
||||
OkHttpClient.Builder().apply {
|
||||
if (logger.isLoggable(Level.FINE)) {
|
||||
addInterceptor(HttpLoggingInterceptor().apply {
|
||||
level = HttpLoggingInterceptor.Level.BODY
|
||||
})
|
||||
}
|
||||
}.build()
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a bookmark to Pinboard.
|
||||
|
@ -244,13 +253,9 @@ open class PinboardPoster() {
|
|||
|
||||
val request = Request.Builder().url(httpUrl).build()
|
||||
val result = client.newCall(request).execute()
|
||||
|
||||
logHttp(method, "HTTP Result: ${result.code}")
|
||||
|
||||
val response = result.body?.string()
|
||||
|
||||
if (response != null) {
|
||||
logHttp(method, "HTTP Response:\n$response")
|
||||
if (response.contains("done")) {
|
||||
return true
|
||||
} else {
|
||||
|
@ -268,10 +273,6 @@ open class PinboardPoster() {
|
|||
return false
|
||||
}
|
||||
|
||||
private fun logHttp(method: String, msg: String) {
|
||||
logger.logp(Level.FINE, PinboardPoster::class.java.name, "executeMethod($method)", msg)
|
||||
}
|
||||
|
||||
private fun validate(): Boolean {
|
||||
var isValid = true
|
||||
if (!apiToken.contains(':')) {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/*
|
||||
* PinboardPosterTest.kt
|
||||
*
|
||||
* Copyright (c) 2017-2019, Erik C. Thauvin (erik@thauvin.net)
|
||||
* Copyright (c) 2017-2020, Erik C. Thauvin (erik@thauvin.net)
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -40,6 +40,7 @@ import java.io.IOException
|
|||
import java.nio.file.Files
|
||||
import java.nio.file.Paths
|
||||
import java.util.Properties
|
||||
import java.util.logging.Level
|
||||
|
||||
class PinboardPosterTest {
|
||||
private val url = "http://www.foo.com/"
|
||||
|
@ -49,6 +50,7 @@ class PinboardPosterTest {
|
|||
@Test
|
||||
fun testAddPin() {
|
||||
var poster = PinboardPoster("")
|
||||
poster.logger.level = Level.FINE
|
||||
|
||||
assertFalse(poster.addPin(url, desc), "apiToken: <blank>")
|
||||
|
||||
|
@ -59,6 +61,7 @@ class PinboardPosterTest {
|
|||
// assertFalse(poster.addPin(url, desc), "apiToken: ${poster.apiToken}")
|
||||
|
||||
poster = PinboardPoster(localProps)
|
||||
poster.logger.level = Level.FINE
|
||||
assertTrue(poster.addPin(url, desc), "apiToken: ${Constants.ENV_API_TOKEN}")
|
||||
}
|
||||
|
||||
|
@ -75,11 +78,13 @@ class PinboardPosterTest {
|
|||
}
|
||||
|
||||
var poster = PinboardPoster(props)
|
||||
poster.logger.level = Level.FINE
|
||||
|
||||
poster.apiEndPoint = ""
|
||||
assertFalse(poster.deletePin(url), "apiEndPoint: <blank>")
|
||||
|
||||
poster = PinboardPoster(localProps, Constants.ENV_API_TOKEN)
|
||||
poster.logger.level = Level.FINE
|
||||
|
||||
poster.apiEndPoint = Constants.API_ENDPOINT
|
||||
assertTrue(poster.deletePin(url), "apiEndPoint: ${Constants.API_ENDPOINT}")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue