Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow optional request headers #2

Merged
merged 2 commits into from
Feb 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions generator/src/main/kotlin/com/collectiveidea/twirp/Generator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ class Generator : ServiceGenerator {

interfaceMethods += """
@Throws(ServiceException::class, CancellationException::class)
suspend fun ${kotlinServiceMethodName}(request: $reqType): $respType
suspend fun ${kotlinServiceMethodName}(request: $reqType, requestHeaders: Map<String, String>? = null): $respType
""".trimIndent()

implementationMethods += """
override suspend fun ${kotlinServiceMethodName}(request: $reqType): $respType {
override suspend fun ${kotlinServiceMethodName}(request: $reqType, requestHeaders: Map<String, String>?): $respType {
val response: HttpResponse = httpClient.post("${service.file.packageName}.${service.name}/${method.name}") {
requestHeaders?.forEach { headers.append(it.key, it.value) }
setBody(request.encodeToByteArray())
}
return $respType.decodeFromByteArray(response.body())
Expand All @@ -38,7 +39,6 @@ class Generator : ServiceGenerator {
return listOf(
interfaceKt(service.file.kotlinPackageName, service.name, filePath, interfaceMethods),
implementationKt(
service.file.packageName,
service.file.kotlinPackageName,
service.name,
filePath,
Expand All @@ -64,14 +64,14 @@ class Generator : ServiceGenerator {
}

private fun interfaceKt(
packageName: String?,
kotlinPackageName: String?,
serviceName: String,
filePath: Path,
interfaceMethods: MutableList<String>
) = ServiceGenerator.Result(
otherFilePath = filePath.resolveSibling("${serviceName}.kt").toString(),
code = """
package $packageName
package $kotlinPackageName

import com.collectiveidea.twirp.ServiceException
import kotlin.coroutines.cancellation.CancellationException
Expand All @@ -83,7 +83,6 @@ class Generator : ServiceGenerator {
)

private fun implementationKt(
packageName: String?,
kotlinPackageName: String?,
serviceName: String,
filePath: Path,
Expand Down