Skip to content

Commit c072451

Browse files
committed
Extracted a HttpClient interface
1 parent 813ba06 commit c072451

File tree

4 files changed

+84
-39
lines changed

4 files changed

+84
-39
lines changed

kotlin-http-client/kotlin-http-client-api/src/main/kotlin/com/linkedplanet/kotlinhttpclient/api/http/BaseHttpClient.kt

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,35 +30,9 @@ import java.nio.charset.StandardCharsets
3030

3131
val GSON: Gson = GsonBuilder().create()
3232

33-
abstract class BaseHttpClient {
33+
abstract class BaseHttpClient : HttpClient {
3434

35-
abstract suspend fun executeRestCall(
36-
method: String,
37-
path: String,
38-
params: Map<String, String>,
39-
body: String?,
40-
contentType: String?,
41-
headers: Map<String, String> = emptyMap()
42-
): Either<HttpDomainError, HttpResponse<String>>
43-
44-
abstract suspend fun executeDownload(
45-
method: String,
46-
path: String,
47-
params: Map<String, String>,
48-
body: String?,
49-
contentType: String?
50-
): Either<DomainError, HttpResponse<ByteArray>>
51-
52-
abstract suspend fun executeUpload(
53-
method: String,
54-
url: String,
55-
params: Map<String, String>,
56-
mimeType: String,
57-
filename: String,
58-
byteArray: ByteArray
59-
): Either<HttpDomainError, HttpResponse<ByteArray>>
60-
61-
suspend fun <T> executeRest(
35+
override suspend fun <T> executeRest(
6236
method: String,
6337
path: String,
6438
params: Map<String, String>,
@@ -73,7 +47,7 @@ abstract class BaseHttpClient {
7347
)
7448
}
7549

76-
suspend fun <T> executeRestList(
50+
override suspend fun <T> executeRestList(
7751
method: String,
7852
path: String,
7953
params: Map<String, String>,
@@ -88,7 +62,7 @@ abstract class BaseHttpClient {
8862
)
8963
}
9064

91-
suspend fun <T> executeGet(
65+
override suspend fun <T> executeGet(
9266
path: String,
9367
params: Map<String, String>,
9468
returnType: Type
@@ -100,7 +74,7 @@ abstract class BaseHttpClient {
10074
)
10175
}
10276

103-
suspend fun <T> executeGetReturnList(
77+
override suspend fun <T> executeGetReturnList(
10478
path: String,
10579
params: Map<String, String>,
10680
returnType: Type
@@ -112,13 +86,13 @@ abstract class BaseHttpClient {
11286
)
11387
}
11488

115-
suspend fun executeGetCall(
89+
override suspend fun executeGetCall(
11690
path: String,
11791
params: Map<String, String>
11892
): Either<HttpDomainError, HttpResponse<String>> =
11993
executeRestCall("GET", path, params, null, null)
12094

121-
fun encodeParams(map: Map<String, String>): String {
95+
override fun encodeParams(map: Map<String, String>): String {
12296
return map.map { it.key + "=" + doEncoding(it.value) }.joinToString("&")
12397
}
12498

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.linkedplanet.kotlinhttpclient.api.http
2+
3+
import arrow.core.Either
4+
import com.linkedplanet.kotlinhttpclient.error.DomainError
5+
import com.linkedplanet.kotlinhttpclient.error.HttpDomainError
6+
import java.lang.reflect.Type
7+
8+
interface HttpClient {
9+
suspend fun executeRestCall(
10+
method: String,
11+
path: String,
12+
params: Map<String, String>,
13+
body: String?,
14+
contentType: String?,
15+
headers: Map<String, String> = emptyMap()
16+
): Either<HttpDomainError, HttpResponse<String>>
17+
18+
suspend fun executeDownload(
19+
method: String,
20+
path: String,
21+
params: Map<String, String>,
22+
body: String?,
23+
contentType: String?
24+
): Either<DomainError, HttpResponse<ByteArray>>
25+
26+
suspend fun executeUpload(
27+
method: String,
28+
url: String,
29+
params: Map<String, String>,
30+
mimeType: String,
31+
filename: String,
32+
byteArray: ByteArray
33+
): Either<HttpDomainError, HttpResponse<ByteArray>>
34+
35+
suspend fun <T> executeRest(
36+
method: String,
37+
path: String,
38+
params: Map<String, String>,
39+
body: String?,
40+
contentType: String?,
41+
returnType: Type
42+
): Either<HttpDomainError, HttpResponse<T?>>
43+
44+
suspend fun <T> executeRestList(
45+
method: String,
46+
path: String,
47+
params: Map<String, String>,
48+
body: String?,
49+
contentType: String?,
50+
returnType: Type
51+
): Either<HttpDomainError, HttpResponse<List<T>>>
52+
53+
suspend fun <T> executeGet(
54+
path: String,
55+
params: Map<String, String>,
56+
returnType: Type
57+
): Either<HttpDomainError, HttpResponse<T?>>
58+
59+
suspend fun <T> executeGetReturnList(
60+
path: String,
61+
params: Map<String, String>,
62+
returnType: Type
63+
): Either<HttpDomainError, HttpResponse<List<T>?>>
64+
65+
suspend fun executeGetCall(
66+
path: String,
67+
params: Map<String, String>
68+
): Either<HttpDomainError, HttpResponse<String>>
69+
70+
fun encodeParams(map: Map<String, String>): String
71+
}

kotlin-insight-client/kotlin-insight-client-http/src/main/kotlin/com/linkedplanet/kotlininsightclient/http/HttpInsightClientContext.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
*/
2020
package com.linkedplanet.kotlininsightclient.http
2121

22-
import com.linkedplanet.kotlinhttpclient.api.http.BaseHttpClient
22+
import com.linkedplanet.kotlinhttpclient.api.http.HttpClient
2323

24-
class HttpInsightClientContext(val baseUrl: String, val httpClient: BaseHttpClient)
24+
class HttpInsightClientContext(val baseUrl: String, val httpClient: HttpClient)

kotlin-jira-client/kotlin-jira-client-http/src/main/kotlin/com/linkedplanet/kotlinjiraclient/http/HttpJiraClientContext.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* Licensed under the Apache License, Version 2.0 (the "License");
88
* you may not use this file except in compliance with the License.
99
* You may obtain a copy of the License at
10-
*
10+
*
1111
* http://www.apache.org/licenses/LICENSE-2.0
12-
*
12+
*
1313
* Unless required by applicable law or agreed to in writing, software
1414
* distributed under the License is distributed on an "AS IS" BASIS,
1515
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -19,6 +19,6 @@
1919
*/
2020
package com.linkedplanet.kotlinjiraclient.http
2121

22-
import com.linkedplanet.kotlinhttpclient.api.http.BaseHttpClient
22+
import com.linkedplanet.kotlinhttpclient.api.http.HttpClient
2323

24-
class HttpJiraClientContext(val baseUrl: String, val httpClient: BaseHttpClient)
24+
class HttpJiraClientContext(val baseUrl: String, val httpClient: HttpClient)

0 commit comments

Comments
 (0)