-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor HttpClient to its own class (#29)
* Refactor HttpClient to its own class Which makes its headers fully configurable by the Inngest client. It should also be able to pool network connections correctly given that it's the same instance. According to: https://square.github.io/okhttp/contribute/concurrency/#connection-pool * Add the framework header to client * Use Inngest client headers in controller * Move framework option from Inngest's client API It's now set to the `CommHandler` similarly to the other SDKs. * Make `HttpClient` internal
- Loading branch information
Showing
12 changed files
with
143 additions
and
88 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.inngest | ||
|
||
object Environment { | ||
fun inngestHeaders(framework: SupportedFrameworkName? = null): RequestHeaders { | ||
val sdk = "inngest-kt:${Version.getVersion()}" | ||
return mapOf( | ||
InngestHeaderKey.ContentType.value to "application/json", | ||
InngestHeaderKey.Sdk.value to sdk, | ||
InngestHeaderKey.UserAgent.value to sdk, | ||
InngestHeaderKey.Framework.value to (framework?.value), | ||
).filterValues { (it is String) }.entries.associate { (k, v) -> k to v!! } | ||
} | ||
} |
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,45 @@ | ||
package com.inngest | ||
|
||
import com.beust.klaxon.Klaxon | ||
import okhttp3.Headers | ||
import okhttp3.MediaType.Companion.toMediaType | ||
import okhttp3.OkHttpClient | ||
import okhttp3.RequestBody.Companion.toRequestBody | ||
import okhttp3.Response | ||
|
||
typealias RequestHeaders = Map<String, String> | ||
|
||
data class RequestConfig(val headers: RequestHeaders? = null) | ||
|
||
val jsonMediaType = "application/json".toMediaType() | ||
|
||
internal class HttpClient(private val clientConfig: RequestConfig) { | ||
private val client = OkHttpClient() | ||
|
||
fun <T> send( | ||
request: okhttp3.Request, | ||
handler: (Response) -> T, | ||
) = this.client.newCall(request).execute().use(handler) | ||
|
||
fun build( | ||
url: String, | ||
payload: Any, | ||
config: RequestConfig? = null, | ||
): okhttp3.Request { | ||
val jsonRequestBody = Klaxon().toJsonString(payload) | ||
val body = jsonRequestBody.toRequestBody(jsonMediaType) | ||
|
||
return okhttp3.Request.Builder() | ||
.url(url) | ||
.post(body) | ||
.headers(toOkHttpHeaders(clientConfig.headers)) | ||
.apply { config?.headers?.forEach { (k, v) -> addHeader(k, v) } } | ||
.build() | ||
} | ||
} | ||
|
||
fun toOkHttpHeaders(requestHeaders: RequestHeaders?): Headers { | ||
val builder = Headers.Builder() | ||
requestHeaders?.forEach { (k, v) -> builder.add(k, v) } | ||
return builder.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 |
---|---|---|
@@ -1,18 +1,32 @@ | ||
package com.inngest | ||
|
||
import com.beust.klaxon.Klaxon | ||
import java.io.IOException | ||
|
||
class Inngest(val appId: String) { | ||
// TODO - Fetch INNGEST_EVENT_KEY env variable on instantiation | ||
|
||
// fun send(event: Event): EventAPIResponse { | ||
// val requestBody = Klaxon().toJsonString(event) | ||
// val client = HttpClient.newBuilder().build() | ||
// val request = | ||
// HttpRequest.newBuilder() | ||
// .uri(URI.create("http://localhost:8288/e/")) | ||
// .POST(HttpRequest.BodyPublishers.ofString(requestBody)) | ||
// .build() | ||
// val response = client.send(request, HttpResponse.BodyHandlers.ofString()) | ||
// val body = Klaxon().parse<EventAPIResponse>(response) | ||
// return body; | ||
// } | ||
val headers: RequestHeaders = Environment.inngestHeaders() | ||
internal val httpClient = HttpClient(RequestConfig(headers)) | ||
|
||
internal inline fun <reified T> send( | ||
url: String, | ||
payload: Any, | ||
): T? { | ||
val request = httpClient.build(url, payload) | ||
|
||
return httpClient.send(request) lambda@{ response -> | ||
// TODO: Handle error case | ||
if (!response.isSuccessful) throw IOException("Unexpected code $response") | ||
if (Unit::class.java.isAssignableFrom(T::class.java)) { | ||
return@lambda Unit as T | ||
} | ||
return@lambda Klaxon().parse<T>(response.body!!.charStream()) | ||
} | ||
} | ||
|
||
internal inline fun <reified T> sendEvent(payload: Any): T? { | ||
val eventKey = "test" | ||
return send("http://localhost:8288/e/$eventKey", payload) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
inngest-core/src/main/kotlin/com/inngest/InngestHeaderKey.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,16 @@ | ||
package com.inngest | ||
|
||
enum class InngestHeaderKey(val value: String) { | ||
ContentType("content-type"), | ||
UserAgent("user-agent"), | ||
Sdk("x-inngest-sdk"), | ||
Framework("x-inngest-framework"), | ||
Environment("x-inngest-env"), | ||
Platform("x-inngest-platform"), | ||
NoRetry("x-inngest-no-retry"), | ||
RequestVersion("x-inngest-req-version"), | ||
RetryAfter("retry-after"), | ||
ServerKind("x-inngest-server-kind"), | ||
ExpectedServerKind("x-inngest-expected-server-kind"), | ||
Signature("x-inngest-signature"), | ||
} |
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
6 changes: 6 additions & 0 deletions
6
inngest-core/src/main/kotlin/com/inngest/SupportedFrameworkName.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,6 @@ | ||
package com.inngest | ||
|
||
enum class SupportedFrameworkName(val value: String) { | ||
SpringBoot("springboot"), | ||
Ktor("ktor"), | ||
} |
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
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