Copyright | (c) Nike Inc. 2019 |
---|---|
License | BSD3 |
Maintainer | nathan.fairhurst@nike.com, fernando.freire@nike.com |
Stability | stable |
Safe Haskell | None |
Language | Haskell2010 |
This module enable exposes the required types for responding to API Gateway Proxy Events. Responses must return a status, body, and optionaly headers. Multiple smart contructors and helpers are provided to help encapsulated details like header case-insensitivity, multiple header copies, correct base64 encoding, and default content type.
Synopsis
- data ProxyResponse = ProxyResponse {}
- response :: Status -> ProxyBody -> ProxyResponse
- addHeader :: Text -> Text -> ProxyResponse -> ProxyResponse
- setHeader :: Text -> Text -> ProxyResponse -> ProxyResponse
- data ProxyBody = ProxyBody {
- contentType :: Text
- serialized :: Text
- isBase64Encoded :: Bool
- textPlain :: Text -> ProxyBody
- applicationJson :: ToJSON a => a -> ProxyBody
- genericBinary :: Text -> ByteString -> ProxyBody
- networkAuthenticationRequired511 :: Status
- status511 :: Status
- httpVersionNotSupported505 :: Status
- status505 :: Status
- gatewayTimeout504 :: Status
- status504 :: Status
- serviceUnavailable503 :: Status
- status503 :: Status
- badGateway502 :: Status
- status502 :: Status
- notImplemented501 :: Status
- status501 :: Status
- internalServerError500 :: Status
- status500 :: Status
- requestHeaderFieldsTooLarge431 :: Status
- status431 :: Status
- tooManyRequests429 :: Status
- status429 :: Status
- preconditionRequired428 :: Status
- status428 :: Status
- upgradeRequired426 :: Status
- status426 :: Status
- unprocessableEntity422 :: Status
- status422 :: Status
- imATeapot418 :: Status
- status418 :: Status
- expectationFailed417 :: Status
- status417 :: Status
- requestedRangeNotSatisfiable416 :: Status
- status416 :: Status
- unsupportedMediaType415 :: Status
- status415 :: Status
- requestURITooLong414 :: Status
- status414 :: Status
- requestEntityTooLarge413 :: Status
- status413 :: Status
- preconditionFailed412 :: Status
- status412 :: Status
- lengthRequired411 :: Status
- status411 :: Status
- gone410 :: Status
- status410 :: Status
- conflict409 :: Status
- status409 :: Status
- requestTimeout408 :: Status
- status408 :: Status
- proxyAuthenticationRequired407 :: Status
- status407 :: Status
- notAcceptable406 :: Status
- status406 :: Status
- methodNotAllowed405 :: Status
- status405 :: Status
- notFound404 :: Status
- status404 :: Status
- forbidden403 :: Status
- status403 :: Status
- paymentRequired402 :: Status
- status402 :: Status
- unauthorized401 :: Status
- status401 :: Status
- badRequest400 :: Status
- status400 :: Status
- permanentRedirect308 :: Status
- status308 :: Status
- temporaryRedirect307 :: Status
- status307 :: Status
- useProxy305 :: Status
- status305 :: Status
- notModified304 :: Status
- status304 :: Status
- seeOther303 :: Status
- status303 :: Status
- found302 :: Status
- status302 :: Status
- movedPermanently301 :: Status
- status301 :: Status
- multipleChoices300 :: Status
- status300 :: Status
- partialContent206 :: Status
- status206 :: Status
- resetContent205 :: Status
- status205 :: Status
- noContent204 :: Status
- status204 :: Status
- nonAuthoritative203 :: Status
- status203 :: Status
- accepted202 :: Status
- status202 :: Status
- created201 :: Status
- status201 :: Status
- ok200 :: Status
- status200 :: Status
- switchingProtocols101 :: Status
- status101 :: Status
- continue100 :: Status
- status100 :: Status
- data Status = Status {}
Documentation
data ProxyResponse Source #
A response returned to an API Gateway when using the HTTP Lambda Proxy integration. ContentType will be set based on the ProxyBody (recommended) if a value is not present in the headers field.
This type can be constructed explicity or via the smart constructor
response
. Headers can then be added incrementally with addHeader
or
setHeader
. The smart constructor pattern is recommended because it avoids
some of the awkwardness of dealing with the multiValueHeaders field's type.
{-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE OverloadedStrings #-} module Main where import AWS.Lambda.Runtime (pureRuntime) import AWS.Lambda.Events.ApiGateway.ProxyRequest (ProxyRequest(..), NoAuthorizer) import AWS.Lambda.Events.ApiGateway.ProxyResponse (ProxyResponse(..), textPlain, forbidden403, ok200, response) myHandler :: ProxyRequest NoAuthorizer -> ProxyResponse myHandler ProxyRequest { httpMethod = "GET", path = "/say_hello" } = -- Smart Constructor and added header (recommended) addHeader "My-Custom-Header" Value $ response ok200 $ textPlain "Hello" myHandler _ = -- Explicit Construction (not recommended) ProxyResponse { status = forbidden403 , body = textPlain "Forbidden" , multiValueHeaders = fromList [(mk "My-Custom-Header", ["Other Value])] } main :: IO () main = pureRuntime myHandler
Instances
response :: Status -> ProxyBody -> ProxyResponse Source #
Smart constructor for creating a ProxyResponse from a status and a body
addHeader :: Text -> Text -> ProxyResponse -> ProxyResponse Source #
Add a header to the ProxyResponse. If there was already a value for this header, this one is added, meaning the response will include multiple copies of this header (valid by the HTTP spec). This does NOT replace any previous headers or their values.
setHeader :: Text -> Text -> ProxyResponse -> ProxyResponse Source #
Set a header to the ProxyResponse. If there were any previous values for this header they are all replaced by this new value.
Type that represents the body returned to an API Gateway when using HTTP
Lambda Proxy integration. It is highly recommended that you do not use this
type directly, and instead use the smart constructors exposed such as
textPlain
, applicationJson
, and genericBinary
. These make sure that
the base64 encodings work transparently.
ProxyBody | |
|
Instances
Eq ProxyBody Source # | |
Show ProxyBody Source # | |
Generic ProxyBody Source # | |
type Rep ProxyBody Source # | |
Defined in AWS.Lambda.Events.ApiGateway.ProxyResponse type Rep ProxyBody = D1 ('MetaData "ProxyBody" "AWS.Lambda.Events.ApiGateway.ProxyResponse" "hal-0.4.8-40gZLn9f3jS82UCkewY0zs" 'False) (C1 ('MetaCons "ProxyBody" 'PrefixI 'True) (S1 ('MetaSel ('Just "contentType") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Text) :*: (S1 ('MetaSel ('Just "serialized") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Text) :*: S1 ('MetaSel ('Just "isBase64Encoded") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Bool)))) |
applicationJson :: ToJSON a => a -> ProxyBody Source #
Smart constructor for creating a simple body of JSON.
genericBinary :: Text -> ByteString -> ProxyBody Source #
Smart constructor for creating a ProxyBody with an arbitrary ByteString of the chosen content type. Use this smart constructor to avoid invalid JSON representations of binary data.
From here it is easy to make more specific body constructors:
imageGif :: ByteString -> ProxyBody imageGif = genericBinary "image/gif" imageJpeg :: ByteString -> ProxyBody imageJpeg = genericBinary "image/jpeg"
networkAuthenticationRequired511 :: Status #
Network Authentication Required 511 (RFC 6585)
httpVersionNotSupported505 :: Status #
HTTP Version Not Supported 505
Gateway Timeout 504
serviceUnavailable503 :: Status #
Service Unavailable 503
badGateway502 :: Status #
Bad Gateway 502
Not Implemented 501
internalServerError500 :: Status #
Internal Server Error 500
requestHeaderFieldsTooLarge431 :: Status #
Request Header Fields Too Large 431 (RFC 6585)
tooManyRequests429 :: Status #
Too Many Requests 429 (RFC 6585)
preconditionRequired428 :: Status #
Precondition Required 428 (RFC 6585)
upgradeRequired426 :: Status #
Upgrade Required 426 (https://tools.ietf.org/html/rfc7231#section-6.5.15)
Upgrade Required 426 (https://tools.ietf.org/html/rfc7231#section-6.5.15)
unprocessableEntity422 :: Status #
Unprocessable Entity 422 (RFC 4918)
imATeapot418 :: Status #
I'm a teapot 418
expectationFailed417 :: Status #
Expectation Failed 417
requestedRangeNotSatisfiable416 :: Status #
Requested Range Not Satisfiable 416
unsupportedMediaType415 :: Status #
Unsupported Media Type 415
requestURITooLong414 :: Status #
Request-URI Too Long 414
requestEntityTooLarge413 :: Status #
Request Entity Too Large 413
preconditionFailed412 :: Status #
Precondition Failed 412
Length Required 411
conflict409 :: Status #
Conflict 409
Request Timeout 408
proxyAuthenticationRequired407 :: Status #
Proxy Authentication Required 407
Not Acceptable 406
methodNotAllowed405 :: Status #
Method Not Allowed 405
notFound404 :: Status #
Not Found 404
forbidden403 :: Status #
Forbidden 403
paymentRequired402 :: Status #
Payment Required 402
Unauthorized 401
badRequest400 :: Status #
Bad Request 400
permanentRedirect308 :: Status #
Permanent Redirect 308
temporaryRedirect307 :: Status #
Temporary Redirect 307
useProxy305 :: Status #
Use Proxy 305
Not Modified 304
seeOther303 :: Status #
See Other 303
movedPermanently301 :: Status #
Moved Permanently 301
multipleChoices300 :: Status #
Multiple Choices 300
Partial Content 206
Reset Content 205
noContent204 :: Status #
No Content 204
nonAuthoritative203 :: Status #
Non-Authoritative Information 203
accepted202 :: Status #
Accepted 202
created201 :: Status #
Created 201
switchingProtocols101 :: Status #
Switching Protocols 101
continue100 :: Status #
Continue 100
HTTP Status.
Only the statusCode
is used for comparisons.
Please use mkStatus
to create status codes from code and message, or the Enum
instance or the
status code constants (like ok200
). There might be additional record members in the future.
Note that the Show instance is only for debugging.