Skip to content

Commit b582538

Browse files
committed
refactor: split preference parsing from userApiRequest
This allows obtaining the preferences header before doing the full parse on userApiRequest. Which is needed by #3507.
1 parent bf79766 commit b582538

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/PostgREST/ApiRequest.hs

+8-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module PostgREST.ApiRequest
1313
, DbAction(..)
1414
, Payload(..)
1515
, userApiRequest
16+
, userPreferences
1617
) where
1718

1819
import qualified Data.Aeson as JSON
@@ -127,10 +128,8 @@ data ApiRequest = ApiRequest {
127128
}
128129

129130
-- | Examines HTTP request and translates it into user intent.
130-
--
131-
-- TimezoneNames are used by Prefer: timezone
132-
userApiRequest :: AppConfig -> Request -> RequestBody -> TimezoneNames -> Either ApiRequestError ApiRequest
133-
userApiRequest conf req reqBody timezones = do
131+
userApiRequest :: AppConfig -> Preferences.Preferences -> Request -> RequestBody -> Either ApiRequestError ApiRequest
132+
userApiRequest conf prefs req reqBody = do
134133
resource <- getResource conf $ pathInfo req
135134
(schema, negotiatedByProfile) <- getSchema conf hdrs method
136135
act <- getAction resource schema method
@@ -142,7 +141,7 @@ userApiRequest conf req reqBody timezones = do
142141
, iRange = ranges
143142
, iTopLevelRange = topLevelRange
144143
, iPayload = payload
145-
, iPreferences = Preferences.fromHeaders (configDbTxAllowOverride conf) timezones hdrs
144+
, iPreferences = prefs
146145
, iQueryParams = qPrms
147146
, iColumns = columns
148147
, iHeaders = iHdrs
@@ -163,6 +162,10 @@ userApiRequest conf req reqBody timezones = do
163162
contentMediaType = maybe MTApplicationJSON MediaType.decodeMediaType $ lookupHeader "content-type"
164163
actIsInvokeSafe x = case x of {ActDb (ActRoutine _ (InvRead _)) -> True; _ -> False}
165164

165+
-- | Parses the Prefer header
166+
userPreferences :: AppConfig -> Request -> TimezoneNames -> Preferences.Preferences
167+
userPreferences conf req timezones = Preferences.fromHeaders (configDbTxAllowOverride conf) timezones $ requestHeaders req
168+
166169
getResource :: AppConfig -> [Text] -> Either ApiRequestError Resource
167170
getResource AppConfig{configOpenApiMode, configDbRootSpec} = \case
168171
[] ->

src/PostgREST/App.hs

+2-1
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,9 @@ postgrestResponse appState conf@AppConfig{..} maybeSchemaCache pgVer authResult@
145145

146146
let jwtTime = if configServerTimingEnabled then Auth.getJwtDur req else Nothing
147147
timezones = dbTimezones sCache
148+
prefs = ApiRequest.userPreferences conf req timezones
148149

149-
(parseTime, apiReq@ApiRequest{..}) <- withTiming $ liftEither . mapLeft Error.ApiRequestError $ ApiRequest.userApiRequest conf req body timezones
150+
(parseTime, apiReq@ApiRequest{..}) <- withTiming $ liftEither . mapLeft Error.ApiRequestError $ ApiRequest.userApiRequest conf prefs req body
150151
(planTime, plan) <- withTiming $ liftEither $ Plan.actionPlan iAction conf apiReq sCache
151152

152153
let query = Query.query conf authResult apiReq plan sCache pgVer

0 commit comments

Comments
 (0)