-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #439 from armanbilge/topic/api-gateway-proxy-handl…
…er-refactor `ApiGatewayProxyHandler` -> `ApiGatewayProxyHandlerV2`
- Loading branch information
Showing
9 changed files
with
144 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
98 changes: 98 additions & 0 deletions
98
lambda-http4s/src/main/scala/feral/lambda/http4s/ApiGatewayProxyHandlerV2.scala
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,98 @@ | ||
/* | ||
* Copyright 2021 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package feral.lambda | ||
package http4s | ||
|
||
import cats.effect.kernel.Concurrent | ||
import cats.syntax.all._ | ||
import feral.lambda.events.ApiGatewayProxyEventV2 | ||
import feral.lambda.events.ApiGatewayProxyStructuredResultV2 | ||
import fs2.Stream | ||
import org.http4s.Charset | ||
import org.http4s.Header | ||
import org.http4s.Headers | ||
import org.http4s.HttpApp | ||
import org.http4s.HttpRoutes | ||
import org.http4s.Method | ||
import org.http4s.Request | ||
import org.http4s.Uri | ||
import org.http4s.headers.Cookie | ||
import org.http4s.headers.`Set-Cookie` | ||
|
||
object ApiGatewayProxyHandlerV2 { | ||
|
||
@deprecated("Use apply(routes.orNotFound)", "0.3.0") | ||
def httpRoutes[F[_]: Concurrent: ApiGatewayProxyInvocationV2]( | ||
routes: HttpRoutes[F]): F[Option[ApiGatewayProxyStructuredResultV2]] = apply( | ||
routes.orNotFound) | ||
|
||
def apply[F[_]: Concurrent: ApiGatewayProxyInvocationV2]( | ||
app: HttpApp[F]): F[Option[ApiGatewayProxyStructuredResultV2]] = | ||
for { | ||
event <- Invocation.event | ||
request <- decodeEvent(event) | ||
response <- app(request) | ||
isBase64Encoded = !response.charset.contains(Charset.`UTF-8`) | ||
responseBody <- response | ||
.body | ||
.through( | ||
if (isBase64Encoded) fs2.text.base64.encode else fs2.text.utf8.decode | ||
) | ||
.compile | ||
.string | ||
} yield { | ||
val headers = response.headers.headers.groupMap(_.name)(_.value) | ||
Some( | ||
ApiGatewayProxyStructuredResultV2( | ||
response.status.code, | ||
(headers - `Set-Cookie`.name).map { | ||
case (name, values) => | ||
name -> values.mkString(",") | ||
}, | ||
responseBody, | ||
isBase64Encoded, | ||
headers.getOrElse(`Set-Cookie`.name, Nil) | ||
) | ||
) | ||
} | ||
|
||
private[http4s] def decodeEvent[F[_]: Concurrent]( | ||
event: ApiGatewayProxyEventV2): F[Request[F]] = for { | ||
method <- Method.fromString(event.requestContext.http.method).liftTo[F] | ||
uri <- Uri.fromString(event.rawPath + "?" + event.rawQueryString).liftTo[F] | ||
headers = { | ||
val builder = List.newBuilder[Header.Raw] | ||
|
||
event.headers.foreachEntry(builder += Header.Raw(_, _)) | ||
event.cookies.filter(_.nonEmpty).foreach { cs => | ||
builder += Header.Raw(Cookie.name, cs.mkString("; ")) | ||
} | ||
|
||
Headers(builder.result()) | ||
} | ||
readBody = | ||
if (event.isBase64Encoded) | ||
fs2.text.base64.decode[F] | ||
else | ||
fs2.text.utf8.encode[F] | ||
} yield Request( | ||
method, | ||
uri, | ||
headers = headers, | ||
body = Stream.fromOption[F](event.body).through(readBody) | ||
) | ||
} |
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
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