Skip to content

Commit

Permalink
Implement Google Functions :serverless adapter :wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jaguililla committed Sep 25, 2024
1 parent 14b2dff commit 89fa2f9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,26 @@ class GoogleServerlessHttpAdapter(private val handler: HttpHandler): HttpFunctio
val qp = request.queryParameters?.map { (k, v) -> QueryParameter(k, v) } ?: emptyList()
val h = request.headers?.map { (k, v) -> Header(k, v) } ?: emptyList()

request.parts.map { (k, v) ->
HttpPart(
name = k,
body = v.inputStream.readAllBytes(),
headers = Headers(),
contentType = v.contentType?.map { parseContentType(it) }?.orElse(null),
size = v.contentLength,
submittedFileName = v.fileName.orElse(null),
if (request.contentType.orElse("").contains("multipart")) {
request.parts.map { (k, v) ->
HttpPart(
name = k,
body = v.inputStream.readAllBytes(),
headers = Headers(),
contentType = v.contentType?.map { parseContentType(it) }?.orElse(null),
size = v.contentLength,
submittedFileName = v.fileName.orElse(null),

/*
name: String,
body: Any,
headers: Headers = Headers(),
contentType: ContentType? = null,
size: Long = -1L,
submittedFileName: String? = null
*/
)
/*
name: String,
body: Any,
headers: Headers = Headers(),
contentType: ContentType? = null,
size: Long = -1L,
submittedFileName: String? = null
*/
)
}
}

return HttpRequest(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
package com.hexagontk.serverless.http.google

import com.google.cloud.functions.HttpFunction
import com.google.cloud.functions.HttpRequest
import com.google.cloud.functions.HttpResponse
import com.google.cloud.functions.invoker.runner.Invoker
import com.hexagontk.core.freePort
import com.hexagontk.core.urlOf
import com.hexagontk.http.client.HttpClient
import com.hexagontk.http.client.HttpClientSettings
import com.hexagontk.http.client.java.JavaClientAdapter
import com.hexagontk.http.handlers.Get
import kotlin.reflect.KClass
import kotlin.test.Test
import kotlin.test.assertEquals

internal class GoogleServerlessHttpAdapterTest {

class TestServerlessHttpAdapter: HttpFunction {
override fun service(request: HttpRequest, response: HttpResponse) {
response.writer.write("Hello World!")
}
}
class TestServerlessHttpAdapter: HttpFunction by GoogleServerlessHttpAdapter(
Get { ok("Hello World!") }
)

@Test fun `Google functions work ok`() {
val client = invoker(TestServerlessHttpAdapter::class)
assertEquals("Hello World!", client.get().bodyString())
}

private fun invoker(function: KClass<*>): HttpClient {
val port = freePort()
val baseUrl = urlOf("http://localhost:${port}")
val client = HttpClient(JavaClientAdapter(), HttpClientSettings(baseUrl))

invoker(port, TestServerlessHttpAdapter::class)
invoker(port, function)
client.start()
assertEquals("Hello World!", client.get().bodyString())
return client
}

private fun invoker(port: Int, function: KClass<*>): Invoker {
Expand Down

0 comments on commit 89fa2f9

Please sign in to comment.