Skip to content

Commit 75b21a5

Browse files
committed
* Jdk11 client is a default one; removed the one based on Unirest
* Code for publication to MavenCentral * Cleanup
1 parent a003f52 commit 75b21a5

3 files changed

Lines changed: 64 additions & 139 deletions

File tree

build.gradle.kts

Lines changed: 34 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
11
val kotlinVersion = "2.1.21"
22

33
plugins {
4+
idea
45
kotlin("jvm") version "2.1.21"
56
id("com.adarshr.test-logger") version "4.0.0"
6-
`maven-publish`
7-
signing
8-
idea
7+
id("com.vanniktech.maven.publish") version "0.33.0"
98
}
109

1110
group = "net.igsoft"
12-
version = "0.5.0"
11+
version = "0.6.0"
1312

1413
repositories {
1514
mavenCentral()
1615
}
1716

18-
java {
19-
toolchain {
20-
languageVersion.set(JavaLanguageVersion.of(17))
21-
}
22-
23-
withJavadocJar()
24-
withSourcesJar()
17+
kotlin {
18+
jvmToolchain(17)
2519
}
2620

2721
testlogger {
@@ -33,77 +27,52 @@ tasks.test {
3327
useJUnitPlatform()
3428
}
3529

36-
val licencesSpec = Action<MavenPomLicenseSpec> {
37-
license {
38-
name.set("MIT License")
39-
url.set("https://opensource.org/licenses/MIT")
40-
}
41-
}
42-
43-
val developersSpec = Action<MavenPomDeveloperSpec> {
44-
developer {
45-
id.set("aartiPl")
46-
name.set("Marcin Kuszczak")
47-
email.set("aarti@interia.pl")
48-
}
49-
}
50-
51-
val scmSpec = Action<MavenPomScm> {
52-
connection.set("scm:git:git://https://github.com/aartiPl/funclient.git")
53-
developerConnection.set("scm:git:ssh:https://github.com/aartiPl/funclient.git")
54-
url.set("https://github.com/aartiPl/funclient/tree/master")
55-
}
56-
57-
publishing {
58-
publications {
59-
create<MavenPublication>("funclient") {
60-
artifactId = "funclient"
61-
from(components["java"])
62-
63-
pom {
64-
name.set("funclient")
65-
description.set("FunClient - HTTP client - functional way")
66-
url.set("https://github.com/aartiPl/funclient")
30+
mavenPublishing {
31+
pom {
32+
name.set("funclient")
33+
description.set("FunClient - HTTP client - functional way")
34+
url.set("https://github.com/aartiPl/funclient")
6735

68-
licenses(licencesSpec)
69-
developers(developersSpec)
70-
scm(scmSpec)
36+
licenses {
37+
license {
38+
name.set("MIT License")
39+
url.set("https://opensource.org/licenses/MIT")
7140
}
7241
}
73-
}
74-
75-
repositories {
76-
maven {
77-
val releasesRepoUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
78-
val snapshotsRepoUrl = "https://oss.sonatype.org/content/repositories/snapshots/"
79-
url = uri(if (project.version.toString().endsWith("SNAPSHOT")) snapshotsRepoUrl else releasesRepoUrl)
80-
81-
credentials {
82-
username = project.findProperty("sonatype.user") as String? ?: System.getenv("SONATYPE_USER")
83-
password = project.findProperty("sonatype.password") as String? ?: System.getenv("SONATYPE_PASSWORD")
42+
developers {
43+
developer {
44+
id.set("aartiPl")
45+
name.set("Marcin Kuszczak")
46+
email.set("aarti@interia.pl")
8447
}
8548
}
49+
scm {
50+
connection.set("scm:git:git://https://github.com/aartiPl/funclient.git")
51+
developerConnection.set("scm:git:ssh:https://github.com/aartiPl/funclient.git")
52+
url.set("https://github.com/aartiPl/funclient/tree/master")
53+
}
8654
}
87-
}
8855

89-
signing {
90-
sign(publishing.publications["funclient"])
56+
publishToMavenCentral()
57+
signAllPublications()
9158
}
9259

9360
dependencies {
9461
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion")
9562
implementation("org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion")
63+
9664
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2")
9765

9866
implementation("org.apache.commons:commons-lang3:3.17.0")
99-
implementation("com.konghq:unirest-java:3.14.5")
10067
implementation("com.google.code.gson:gson:2.13.1")
10168

102-
testImplementation("org.junit.platform:junit-platform-suite-engine:1.9.0")
103-
testImplementation("org.junit.platform:junit-platform-suite-api:1.9.0")
104-
testImplementation("org.junit.platform:junit-platform-suite-commons:1.9.0")
105-
testImplementation("org.junit.jupiter:junit-jupiter-engine:5.9.0")
106-
testImplementation("org.junit.jupiter:junit-jupiter-params:5.9.0")
69+
val junitVersion = "6.0.0-M1"
70+
testImplementation("org.junit.platform:junit-platform-suite-engine:$junitVersion")
71+
testImplementation("org.junit.platform:junit-platform-suite-api:$junitVersion")
72+
testImplementation("org.junit.platform:junit-platform-suite-commons:$junitVersion")
73+
testImplementation("org.junit.jupiter:junit-jupiter-engine:$junitVersion")
74+
testImplementation("org.junit.jupiter:junit-jupiter-params:$junitVersion")
75+
10776
testImplementation("com.willowtreeapps.assertk:assertk-jvm:0.28.1")
10877
testImplementation("io.mockk:mockk:1.14.4")
10978
}
Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,48 @@
11
package net.igsoft.funclient
22

33
import com.google.gson.Gson
4-
import kong.unirest.Unirest
5-
import net.igsoft.funclient.handler.ResponseHandler
64
import net.igsoft.funclient.model.*
5+
import java.net.http.HttpClient
6+
import java.net.http.HttpRequest
7+
import java.net.http.HttpResponse
8+
import kotlin.io.path.Path
79

8-
//TODO: create common content types
10+
//NOTE: Doesn't work with JDK8
11+
@Suppress("unused")
912
class FunClient(val gson: Gson) {
10-
init {
11-
//socket timeout - time to receive the data
12-
// 300_000 == 5m
13-
Unirest.config().reset().socketTimeout(300_000).connectTimeout(2_000)
14-
}
15-
16-
fun <T> execute(request: Request, responseHandler: ResponseHandler<T>): T {
17-
return responseHandler.handle(request, execute(request), gson)
18-
}
13+
private val client = HttpClient.newHttpClient()
1914

2015
fun execute(request: Request): Response {
21-
val uniRequest = Unirest.request(request.method.name, request.url.toString())
16+
val httpRequestBuilder = HttpRequest.newBuilder()
17+
18+
when (request.method) {
19+
Method.GET -> httpRequestBuilder.GET()
20+
Method.POST -> httpRequestBuilder.POST(resolveBody(request))
21+
Method.PUT -> httpRequestBuilder.PUT(resolveBody(request))
22+
Method.DELETE -> httpRequestBuilder.DELETE()
23+
Method.PATCH -> httpRequestBuilder.method("PATCH", resolveBody(request))
24+
Method.HEAD -> httpRequestBuilder.method("HEAD", resolveBody(request))
25+
Method.OPTIONS -> httpRequestBuilder.method("OPTIONS", resolveBody(request))
26+
}
27+
28+
httpRequestBuilder.uri(request.url.toURI())
2229

2330
for (header in request.headers.all.entries) {
2431
for (value in header.value) {
25-
uniRequest.header(header.key, value)
32+
httpRequestBuilder.header(header.key, value)
2633
}
2734
}
2835

29-
val finalRequest = when (request.body) {
30-
is StringBody -> uniRequest.body(request.body.string)
31-
is FileBody -> uniRequest.body(request.body.file.readBytes())
32-
is ObjectBody<*> -> uniRequest.body(gson.toJson(request.body.value))
33-
is EmptyBody -> uniRequest
34-
else -> throw IllegalStateException("Unknown body type: ${request.body::class.simpleName}")
35-
}
36-
37-
val response = finalRequest.asString()
36+
val response = client.send(httpRequestBuilder.build(), HttpResponse.BodyHandlers.ofString())
3837

39-
val headersBuilder = Headers.builder()
40-
response.headers.all().forEach { headersBuilder.append(it.name, it.value) }
38+
return RawResponse(response.statusCode(), Headers(response.headers().map()), StringBody(response.body()))
39+
}
4140

42-
return RawResponse(response.status, headersBuilder.build(), StringBody(response.body))
41+
private fun resolveBody(request: Request): HttpRequest.BodyPublisher = when (request.body) {
42+
is StringBody -> HttpRequest.BodyPublishers.ofString(request.body.string)
43+
is EmptyBody -> HttpRequest.BodyPublishers.noBody()
44+
is FileBody -> HttpRequest.BodyPublishers.ofFile(Path(request.body.file.path))
45+
is ObjectBody<*> -> HttpRequest.BodyPublishers.ofString(gson.toJson(request.body.value))
46+
else -> throw IllegalStateException("Unknown body type: ${request.body::class.simpleName}")
4347
}
4448
}

src/main/kotlin/net/igsoft/funclient/FunClientJdk11.kt

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)