refactor: split preference parsing from userApiRequest
This allows obtaining the preferences header before doing the full parse on userApiRequest. Which is needed by #3507.
This commit is contained in:
committed by
Steve Chavez
parent
bf79766a9b
commit
b58253833b
@@ -13,6 +13,7 @@ module PostgREST.ApiRequest
|
|||||||
, DbAction(..)
|
, DbAction(..)
|
||||||
, Payload(..)
|
, Payload(..)
|
||||||
, userApiRequest
|
, userApiRequest
|
||||||
|
, userPreferences
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import qualified Data.Aeson as JSON
|
import qualified Data.Aeson as JSON
|
||||||
@@ -127,10 +128,8 @@ data ApiRequest = ApiRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
-- | Examines HTTP request and translates it into user intent.
|
-- | Examines HTTP request and translates it into user intent.
|
||||||
--
|
userApiRequest :: AppConfig -> Preferences.Preferences -> Request -> RequestBody -> Either ApiRequestError ApiRequest
|
||||||
-- TimezoneNames are used by Prefer: timezone
|
userApiRequest conf prefs req reqBody = do
|
||||||
userApiRequest :: AppConfig -> Request -> RequestBody -> TimezoneNames -> Either ApiRequestError ApiRequest
|
|
||||||
userApiRequest conf req reqBody timezones = do
|
|
||||||
resource <- getResource conf $ pathInfo req
|
resource <- getResource conf $ pathInfo req
|
||||||
(schema, negotiatedByProfile) <- getSchema conf hdrs method
|
(schema, negotiatedByProfile) <- getSchema conf hdrs method
|
||||||
act <- getAction resource schema method
|
act <- getAction resource schema method
|
||||||
@@ -142,7 +141,7 @@ userApiRequest conf req reqBody timezones = do
|
|||||||
, iRange = ranges
|
, iRange = ranges
|
||||||
, iTopLevelRange = topLevelRange
|
, iTopLevelRange = topLevelRange
|
||||||
, iPayload = payload
|
, iPayload = payload
|
||||||
, iPreferences = Preferences.fromHeaders (configDbTxAllowOverride conf) timezones hdrs
|
, iPreferences = prefs
|
||||||
, iQueryParams = qPrms
|
, iQueryParams = qPrms
|
||||||
, iColumns = columns
|
, iColumns = columns
|
||||||
, iHeaders = iHdrs
|
, iHeaders = iHdrs
|
||||||
@@ -163,6 +162,10 @@ userApiRequest conf req reqBody timezones = do
|
|||||||
contentMediaType = maybe MTApplicationJSON MediaType.decodeMediaType $ lookupHeader "content-type"
|
contentMediaType = maybe MTApplicationJSON MediaType.decodeMediaType $ lookupHeader "content-type"
|
||||||
actIsInvokeSafe x = case x of {ActDb (ActRoutine _ (InvRead _)) -> True; _ -> False}
|
actIsInvokeSafe x = case x of {ActDb (ActRoutine _ (InvRead _)) -> True; _ -> False}
|
||||||
|
|
||||||
|
-- | Parses the Prefer header
|
||||||
|
userPreferences :: AppConfig -> Request -> TimezoneNames -> Preferences.Preferences
|
||||||
|
userPreferences conf req timezones = Preferences.fromHeaders (configDbTxAllowOverride conf) timezones $ requestHeaders req
|
||||||
|
|
||||||
getResource :: AppConfig -> [Text] -> Either ApiRequestError Resource
|
getResource :: AppConfig -> [Text] -> Either ApiRequestError Resource
|
||||||
getResource AppConfig{configOpenApiMode, configDbRootSpec} = \case
|
getResource AppConfig{configOpenApiMode, configDbRootSpec} = \case
|
||||||
[] ->
|
[] ->
|
||||||
|
|||||||
@@ -145,8 +145,9 @@ postgrestResponse appState conf@AppConfig{..} maybeSchemaCache pgVer authResult@
|
|||||||
|
|
||||||
let jwtTime = if configServerTimingEnabled then Auth.getJwtDur req else Nothing
|
let jwtTime = if configServerTimingEnabled then Auth.getJwtDur req else Nothing
|
||||||
timezones = dbTimezones sCache
|
timezones = dbTimezones sCache
|
||||||
|
prefs = ApiRequest.userPreferences conf req timezones
|
||||||
|
|
||||||
(parseTime, apiReq@ApiRequest{..}) <- withTiming $ liftEither . mapLeft Error.ApiRequestError $ ApiRequest.userApiRequest conf req body timezones
|
(parseTime, apiReq@ApiRequest{..}) <- withTiming $ liftEither . mapLeft Error.ApiRequestError $ ApiRequest.userApiRequest conf prefs req body
|
||||||
(planTime, plan) <- withTiming $ liftEither $ Plan.actionPlan iAction conf apiReq sCache
|
(planTime, plan) <- withTiming $ liftEither $ Plan.actionPlan iAction conf apiReq sCache
|
||||||
|
|
||||||
let query = Query.query conf authResult apiReq plan sCache pgVer
|
let query = Query.query conf authResult apiReq plan sCache pgVer
|
||||||
|
|||||||
Reference in New Issue
Block a user