Skip to content

Commit

Permalink
add getResponseHeaders, getResponseStatus, getResponseContent (#327)
Browse files Browse the repository at this point in the history
* add getResponseHeaders, getResponseStatus, getResponseContent

* upd changelog

---------

Co-authored-by: Marco Zocca <[email protected]>
  • Loading branch information
ocramz and Marco Zocca authored Oct 4, 2023
1 parent bb50e65 commit 99a602b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
24 changes: 21 additions & 3 deletions Web/Scotty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ module Web.Scotty
-- | Note: only one of these should be present in any given route
-- definition, as they completely replace the current 'Response' body.
, text, html, file, json, stream, raw
-- ** Accessing the fields of the Response
, getResponseHeaders, getResponseStatus, getResponseContent
-- ** Exceptions
, raise, raiseStatus, throw, rescue, next, finish, defaultHandler, liftAndCatchIO
, StatusError(..)
-- * Parsing Parameters
, Param, Trans.Parsable(..), Trans.readEither
-- * Types
, ScottyM, ActionM, RoutePattern, File, Kilobytes, Handler(..)
, ScottyM, ActionM, RoutePattern, File, Content(..), Kilobytes, Handler(..)
, ScottyState, defaultScottyState
) where

Expand All @@ -47,12 +49,12 @@ import qualified Data.ByteString as BS
import Data.ByteString.Lazy.Char8 (ByteString)
import Data.Text.Lazy (Text)

import Network.HTTP.Types (Status, StdMethod)
import Network.HTTP.Types (Status, StdMethod, ResponseHeaders)
import Network.Socket (Socket)
import Network.Wai (Application, Middleware, Request, StreamingBody)
import Network.Wai.Handler.Warp (Port)

import Web.Scotty.Internal.Types (ScottyT, ActionT, ErrorHandler, Param, RoutePattern, Options, defaultOptions, File, Kilobytes, ScottyState, defaultScottyState, StatusError(..))
import Web.Scotty.Internal.Types (ScottyT, ActionT, ErrorHandler, Param, RoutePattern, Options, defaultOptions, File, Kilobytes, ScottyState, defaultScottyState, StatusError(..), Content(..))
import Web.Scotty.Exceptions (Handler(..))

type ScottyM = ScottyT IO
Expand Down Expand Up @@ -305,6 +307,18 @@ stream = Trans.stream
raw :: ByteString -> ActionM ()
raw = Trans.raw


-- | Access the HTTP 'Status' of the Response
getResponseStatus :: ActionM Status
getResponseStatus = Trans.getResponseStatus
-- | Access the HTTP headers of the Response
getResponseHeaders :: ActionM ResponseHeaders
getResponseHeaders = Trans.getResponseHeaders
-- | Access the content of the Response
getResponseContent :: ActionM Content
getResponseContent = Trans.getResponseContent


-- | get = 'addroute' 'GET'
get :: RoutePattern -> ActionM () -> ScottyM ()
get = Trans.get
Expand Down Expand Up @@ -403,3 +417,7 @@ function = Trans.function
-- | Build a route that requires the requested path match exactly, without captures.
literal :: String -> RoutePattern
literal = Trans.literal




17 changes: 17 additions & 0 deletions Web/Scotty/Action.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ module Web.Scotty.Action
, status
, stream
, text
, getResponseStatus
, getResponseHeaders
, getResponseContent
, Param
, Parsable(..)
-- private to Scotty
Expand Down Expand Up @@ -350,6 +353,20 @@ paramsWith f = ActionT (f <$> ask)
getParams :: ActionEnv -> [Param]
getParams e = envCaptureParams e <> envFormParams e <> envQueryParams e


-- === access the fields of the Response being constructed

-- | Access the HTTP 'Status' of the Response
getResponseStatus :: (MonadIO m) => ActionT m Status
getResponseStatus = srStatus <$> getResponseAction
-- | Access the HTTP headers of the Response
getResponseHeaders :: (MonadIO m) => ActionT m ResponseHeaders
getResponseHeaders = srHeaders <$> getResponseAction
-- | Access the content of the Response
getResponseContent :: (MonadIO m) => ActionT m Content
getResponseContent = srContent <$> getResponseAction


-- | Minimum implemention: 'parseParam'
class Parsable a where
-- | Take a 'T.Text' value and parse it as 'a', or fail with a message.
Expand Down
5 changes: 5 additions & 0 deletions Web/Scotty/Internal/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ data ActionEnv = Env { envReq :: Request
getResponse :: MonadIO m => ActionEnv -> m ScottyResponse
getResponse ae = liftIO $ readTVarIO (envResponse ae)

getResponseAction :: (MonadIO m) => ActionT m ScottyResponse
getResponseAction = do
ae <- ask
getResponse ae

modifyResponse :: (MonadIO m) => (ScottyResponse -> ScottyResponse) -> ActionT m ()
modifyResponse f = do
tv <- asks envResponse
Expand Down
6 changes: 4 additions & 2 deletions Web/Scotty/Trans.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ module Web.Scotty.Trans
-- | Note: only one of these should be present in any given route
-- definition, as they completely replace the current 'Response' body.
, text, html, file, json, stream, raw, nested
-- ** Accessing the fields of the Response
, getResponseHeaders, getResponseStatus, getResponseContent
-- ** Exceptions
, raise, raiseStatus, throw, rescue, next, finish, defaultHandler, liftAndCatchIO
, StatusError(..)
-- * Parsing Parameters
, Param, Parsable(..), readEither
-- * Types
, RoutePattern, File, Kilobytes, ErrorHandler, Handler(..)
, RoutePattern, File, Content(..), Kilobytes, ErrorHandler, Handler(..)
-- * Monad Transformers
, ScottyT, ActionT
, ScottyState, defaultScottyState
Expand All @@ -60,7 +62,7 @@ import Network.Wai.Handler.Warp (Port, runSettings, runSettingsSocket, setPort,

import Web.Scotty.Action
import Web.Scotty.Route
import Web.Scotty.Internal.Types (ActionT(..), ScottyT(..), defaultScottyState, Application, RoutePattern, Options(..), defaultOptions, RouteOptions(..), defaultRouteOptions, ErrorHandler, Kilobytes, File, addMiddleware, setHandler, updateMaxRequestBodySize, routes, middlewares, ScottyException(..), ScottyState, defaultScottyState, StatusError(..))
import Web.Scotty.Internal.Types (ActionT(..), ScottyT(..), defaultScottyState, Application, RoutePattern, Options(..), defaultOptions, RouteOptions(..), defaultRouteOptions, ErrorHandler, Kilobytes, File, addMiddleware, setHandler, updateMaxRequestBodySize, routes, middlewares, ScottyException(..), ScottyState, defaultScottyState, StatusError(..), Content(..))
import Web.Scotty.Util (socketDescription)
import Web.Scotty.Body (newBodyInfo)
import Web.Scotty.Exceptions (Handler(..), catches)
Expand Down
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## next [????.??.??]

* add getResponseHeaders, getResponseStatus, getResponseContent (#214)

## 0.20.1 [2023.10.03]

* remove dependencies on 'base-compat' and 'base-compat-batteries' (#318)
Expand Down

0 comments on commit 99a602b

Please sign in to comment.