Skip to content

Commit

Permalink
Merge pull request #121 from rybalkinsd/realease/0.10.0
Browse files Browse the repository at this point in the history
Release/0.10.0
  • Loading branch information
rybalkinsd authored Jul 1, 2019
2 parents b7502e1 + 9252e96 commit 988d735
Show file tree
Hide file tree
Showing 67 changed files with 1,419 additions and 304 deletions.
4 changes: 4 additions & 0 deletions .codecov.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
status:
project:
default:
target: 90
40 changes: 40 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [0.10.0] - 2019-06-20
### Added
- Async Post DSL by [@Evgeny](https://github.com/DeviantBadge) [issue #86](https://github.com/rybalkinsd/kohttp/issues/86).
- Async Head DSL by [@Evgeny](https://github.com/DeviantBadge).
- Async Put DSL by [@Evgeny](https://github.com/DeviantBadge).
- Async Patch DSL by [@Evgeny](https://github.com/DeviantBadge).
- Async Delete DSL by [@Evgeny](https://github.com/DeviantBadge).
- Async Upload DSL by [@Evgeny](https://github.com/DeviantBadge) [issue #87](https://github.com/rybalkinsd/kohttp/issues/87).
- Async Upload File extensions by [@Evgeny](https://github.com/DeviantBadge).
- Async Upload Url extensions by [@Evgeny](https://github.com/DeviantBadge).
- Default `Dispatcher` configuration in `okhttp.yaml`
- CHANGELOG.md

### Changed
- Migrated to `kotlin` 1.3.40
- Migrated to `okhttp` 3.12.2 [issue #81](https://github.com/rybalkinsd/kohttp/issues/81)
- Migrated to `kotlinx-coroutines-core` 1.2.1
- `Boolean` and Nullable types support in [Json builder](https://github.com/rybalkinsd/kohttp/blob/master/src/main/kotlin/io/github/rybalkinsd/kohttp/util/json.kt)
[issue #113](https://github.com/rybalkinsd/kohttp/issues/113)
- `url()` and `param { }` joint usage by [@dtropanets](https://github.com/dtropanets) [issue #94](https://github.com/rybalkinsd/kohttp/issues/94)
- Allowed nullable types in request `param { }` builder [issue #118](https://github.com/rybalkinsd/kohttp/issues/118),
[PR](https://github.com/rybalkinsd/kohttp/pull/117)
- Changed `maxRequests` and `maxRequestsPerHost` for default http client
- Async methods naming (Deprecated `asyncHttpGet`)
- Relaxed coverage [issue #56](https://github.com/rybalkinsd/kohttp/issues/56)


### Removed
- pass

### Big thanks
@deviantBadge, @dtropanets, @gokulchandra for your contribution
58 changes: 45 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ Kotlin DSL http client

gradle kotlin DSL:
```kotlin
implementation(group = "io.github.rybalkinsd", name = "kohttp", version = "0.9.0")
implementation(group = "io.github.rybalkinsd", name = "kohttp", version = "0.10.0")
```

gradle groovy DSL:
```groovy
implementation 'io.github.rybalkinsd:kohttp:0.9.0'
implementation 'io.github.rybalkinsd:kohttp:0.10.0'
```

maven:
```xml
<dependency>
<groupId>io.github.rybalkinsd</groupId>
<artifactId>kohttp</artifactId>
<version>0.9.0</version>
<version>0.10.0</version>
</dependency>
```

Expand Down Expand Up @@ -248,21 +248,53 @@ val response = fileUri.upload( string or url )

#### async GET

##### `String.asyncHttpGet()` extension function
This function starts a new coroutine with *Unconfined* dispatcher.
##### `String.httpGetAsync()` extension function
This function starts a new coroutine with *Unconfined* dispatcher.

```kotlin
val response: Deferred<Response> = "https://google.com/search?q=iphone".asyncHttpGet()
val response: Deferred<Response> = "https://google.com/search?q=iphone".httpGetAsync()
```

##### `asyncHttpGet` call
##### `httpGetAsync` call

You can use same syntax as in [GET](#get)
```kotlin
val response: Deferred<Response> = asyncHttpGet {
host = "google.com"
path = "/search"
header { ... }
param { ... }
}
val response: Deferred<Response> = httpGetAsync { }
```

#### async POST

You can use same syntax as in [POST](#post)
```kotlin
val response: Deferred<Response> = httpPostAsync { }
```

#### async HEAD

You can use same syntax as in [GET](#get)
```kotlin
val response: Deferred<Response> = httpHeadAsync { }
```

#### async PUT

You can use same syntax as in [POST](#post)
```kotlin
val response: Deferred<Response> = httpPutAsync { }
```

#### async PATCH

You can use same syntax as in [POST](#post)
```kotlin
val response: Deferred<Response> = httpPatchAsync { }
```

#### async DELETE

You can use same syntax as in [POST](#post)
```kotlin
val response: Deferred<Response> = httpDeleteAsync { }
```

### Response usage
Expand Down
13 changes: 7 additions & 6 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version "1.3.20"
kotlin("jvm") version "1.3.40"
jacoco

id("org.jetbrains.dokka") version "0.9.16"
Expand All @@ -11,7 +11,7 @@ plugins {
}

group = "io.github.rybalkinsd"
version = "0.9.0-SNAPSHOT"
version = "0.10.0"

repositories {
mavenCentral()
Expand All @@ -20,18 +20,19 @@ repositories {
dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation(kotlin("reflect"))
implementation("org.jetbrains.kotlinx", "kotlinx-coroutines-core", "1.0.1")
implementation("org.jetbrains.kotlinx", "kotlinx-coroutines-core", "1.2.1")

val jacksonVersion = "2.9.7"
val jacksonVersion = "2.9.9"
implementation(jackson("core"), "jackson-databind", jacksonVersion)
implementation(jackson("dataformat"), "jackson-dataformat-yaml", jacksonVersion)
implementation(jackson("module"), "jackson-module-kotlin", jacksonVersion)
api("com.squareup.okhttp3", "okhttp", "3.12.0")
api("com.squareup.okhttp3", "okhttp", "3.14.2")

testImplementation(kotlin("test-junit"))
testImplementation("io.mockk:mockk:1.9.3")
testImplementation("org.mock-server:mockserver-netty:5.5.4")
testImplementation( "org.mock-server:mockserver-client-java:5.5.4")
testImplementation("org.mock-server:mockserver-client-java:5.5.4")
testImplementation("org.assertj:assertj-core:3.12.2")
}

configure<JavaPluginConvention> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ interface ClientBuilder : ForkClientBuilder {

interface ForkClientBuilder {
@get:Deprecated(level = DeprecationLevel.ERROR, message = "Write only field")
var interceptors: List<Interceptor>
var interceptors: List<Interceptor>

@get:Deprecated(level = DeprecationLevel.ERROR, message = "Write only field")
var networkInterceptors: List<Interceptor>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package io.github.rybalkinsd.kohttp.client

import io.github.rybalkinsd.kohttp.configuration.ConnectionPoolConfig
import io.github.rybalkinsd.kohttp.configuration.DispatcherConfig
import io.github.rybalkinsd.kohttp.configuration.config
import okhttp3.ConnectionPool
import okhttp3.Dispatcher
import okhttp3.OkHttpClient
import java.util.concurrent.TimeUnit

Expand Down Expand Up @@ -37,7 +39,15 @@ val defaultHttpClient: OkHttpClient = config.client.let {
writeTimeout = it.writeTimeout
followRedirects = it.followRedirects
followSslRedirects = it.followSslRedirects
dispatcher = it.dispatcher.create()
}
}

private fun ConnectionPoolConfig.create() = ConnectionPool(maxIdleConnections, keepAliveDuration, TimeUnit.MILLISECONDS)
private fun DispatcherConfig.create() =
Dispatcher().apply {
this.maxRequests = this@create.maxRequests
this.maxRequestsPerHost = this@create.maxRequestsPerHost
}

private fun ConnectionPoolConfig.create() =
ConnectionPool(maxIdleConnections, keepAliveDuration, TimeUnit.MILLISECONDS)
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ import okhttp3.OkHttpClient
* @author sergey
*/
fun OkHttpClient.fork(block: ForkClientBuilder.() -> Unit): OkHttpClient =
ClientBuilderImpl(this).apply(block).build()
ClientBuilderImpl(this).apply(block).build()
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import okhttp3.Interceptor
/**
* Provides a DSL class to define Interceptors for HTTP Client
*
* Usage example using the default `InterceptorsDsl`:
* Usage example with default `InterceptorsDsl`:
*
* <pre>
* val forkedClient = defaultHttpClient.fork {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.fasterxml.jackson.module.kotlin.readValue
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import java.util.concurrent.TimeUnit

const val DEFAULT_REQUEST_AMOUNT: Int = 256

internal val config = Config::class.java.getResource("/kohttp.yaml")?.let {
val mapper = ObjectMapper(YAMLFactory()).registerKotlinModule()
Expand All @@ -15,14 +16,37 @@ internal val config = Config::class.java.getResource("/kohttp.yaml")?.let {

internal data class Config(val client: ClientConfig = ClientConfig())

internal data class ClientConfig (
val connectTimeout: Long = TimeUnit.SECONDS.toMillis(10),
val readTimeout: Long = TimeUnit.SECONDS.toMillis(10),
val writeTimeout: Long = TimeUnit.SECONDS.toMillis(10),
@JsonProperty("connectionPool")
val connectionPoolConfig: ConnectionPoolConfig = ConnectionPoolConfig(),
val followRedirects: Boolean = true,
val followSslRedirects: Boolean = true
internal data class ClientConfig(
val connectTimeout: Long = TimeUnit.SECONDS.toMillis(10),
val readTimeout: Long = TimeUnit.SECONDS.toMillis(10),
val writeTimeout: Long = TimeUnit.SECONDS.toMillis(10),
@JsonProperty("connectionPool")
val connectionPoolConfig: ConnectionPoolConfig = ConnectionPoolConfig(),
val followRedirects: Boolean = true,
val followSslRedirects: Boolean = true,
val dispatcher: DispatcherConfig = DispatcherConfig()
)

/**
* okhttp {@link Dispatcher} configuration
*
* @since 0.10.0
* @author evgeny
*/
internal data class DispatcherConfig(
/**
* Set the maximum number of requests to execute concurrently.
*
* @see okhttp3.Dispatcher.setMaxRequests
*/
val maxRequests: Int = DEFAULT_REQUEST_AMOUNT,

/**
* Set the maximum number of requests for each host to execute concurrently.
*
* @see okhttp3.Dispatcher.setMaxRequestsPerHost
*/
val maxRequestsPerHost: Int = DEFAULT_REQUEST_AMOUNT
)

internal data class ConnectionPoolConfig(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import okhttp3.Response
*
* @return a `Response` instance.
*
* Usage example using the default `defaultHttpClient`:
* Usage example with default `defaultHttpClient`:
*
* <pre>
* val response: Response = httpDelete {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import okhttp3.Response
*
* @return a `Response` instance.
*
* Usage example using the default `defaultHttpClient`:
* Usage example with default `defaultHttpClient`:
*
* <pre>
* val response: Response = httpGet {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import okhttp3.Response
*
* @return a `Response` instance.
*
* Usage example using the default `defaultHttpClient`:
* Usage example with default `defaultHttpClient`:
*
* <pre>
* val response: Response = httpHead {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import okhttp3.Response
*
* @return a `Response` instance.
*
* Usage example using the default `defaultHttpClient`:
* Usage example with default `defaultHttpClient`:
*
* <pre>
* val response: Response = httpPatch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import okhttp3.Response
*
* @return a `Response` instance.
*
* Usage example using the default `defaultHttpClient`:
* Usage example with default `defaultHttpClient`:
*
* <pre>
* val response: Response = httpPost {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import okhttp3.Response
*
* @return a `Response` instance.
*
* Usage example using the default `defaultHttpClient`:
* Usage example with default `defaultHttpClient`:
*
* <pre>
* val response: Response = httpPut {
Expand Down
26 changes: 26 additions & 0 deletions src/main/kotlin/io/github/rybalkinsd/kohttp/dsl/UploadDsl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,32 @@ import okhttp3.Call
import okhttp3.Response

/**
* Method provides a synchronous DSL call of HTTP POST to UPLOAD file
*
* @return a `Response` instance.
*
* Usage example with default `defaultHttpClient`:
*
* <pre>
* val response: Response = upload {
* url("http://postman-echo.com/post")
* val fileUri = this.javaClass.getResource("/cat.gif").toURI()
* file(fileUri)
* }
* response.use { ... }
* </pre>
*
* @param client allow to use your own implementation of HttpClient.
* `defaultHttpClient` is used by default.
*
* <pre>
* val response: Response = upload(customHttpClient) {
* ...
* }
* </pre>
*
* @see Response
* @see HttpDeleteContext
*
* @since 0.8.0
* @author sergey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ package io.github.rybalkinsd.kohttp.dsl.async

import io.github.rybalkinsd.kohttp.client.defaultHttpClient
import io.github.rybalkinsd.kohttp.dsl.context.HttpGetContext
import io.github.rybalkinsd.kohttp.ext.suspendCall
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers.Unconfined
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import okhttp3.Call
import okhttp3.Response

Expand All @@ -16,7 +12,13 @@ import okhttp3.Response
* @since 0.4.0
* @author sergey
*/
@Suppress("DeferredIsResult")
@Deprecated(
message = "Use httpGetAsync instead. This function was renamed according to Kotlin Style Guide." +
"This function will be removed in version 0.12.0",
replaceWith = ReplaceWith(
"httpGetAsync(client, init)",
"io.github.rybalkinsd.kohttp.dsl.async.httpGetAsync")
)
fun asyncHttpGet(client: Call.Factory = defaultHttpClient, init: HttpGetContext.() -> Unit): Deferred<Response> =
GlobalScope.async(context = Unconfined) {
client.suspendCall(HttpGetContext().apply(init).makeRequest())
}
httpGetAsync(client, init)
Loading

0 comments on commit 988d735

Please sign in to comment.