From 4819520e3ad8a14da121d0ead5c1f3279dfddd5d Mon Sep 17 00:00:00 2001 From: Taimoor Zaeem Date: Sun, 9 Mar 2025 23:38:39 +0500 Subject: [PATCH] refactor: decouple module SchemaCache and ApiRequest --- src/PostgREST/ApiRequest.hs | 11 ++++++----- src/PostgREST/App.hs | 3 ++- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/PostgREST/ApiRequest.hs b/src/PostgREST/ApiRequest.hs index ee2d8eccd..ae9a3e79e 100644 --- a/src/PostgREST/ApiRequest.hs +++ b/src/PostgREST/ApiRequest.hs @@ -4,7 +4,6 @@ Description : PostgREST functions to translate HTTP request to a domain type cal -} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE NamedFieldPuns #-} --- TODO: This module shouldn't depend on SchemaCache module PostgREST.ApiRequest ( ApiRequest(..) , InvokeMethod(..) @@ -46,6 +45,7 @@ import Web.Cookie (parseCookies) import PostgREST.ApiRequest.QueryParams (QueryParams (..)) import PostgREST.Config (AppConfig (..), OpenAPIMode (..)) +import PostgREST.Config.Database (TimezoneNames) import PostgREST.Error (ApiRequestError (..), RangeError (..)) import PostgREST.MediaType (MediaType (..)) @@ -53,7 +53,6 @@ import PostgREST.RangeQuery (NonnegRange, allRange, convertToLimitZeroRange, hasLimitZero, rangeRequested) -import PostgREST.SchemaCache (SchemaCache (..)) import PostgREST.SchemaCache.Identifiers (FieldName, QualifiedIdentifier (..), Schema) @@ -128,8 +127,10 @@ data ApiRequest = ApiRequest { } -- | Examines HTTP request and translates it into user intent. -userApiRequest :: AppConfig -> Request -> RequestBody -> SchemaCache -> Either ApiRequestError ApiRequest -userApiRequest conf req reqBody sCache = do +-- +-- TimezoneNames are used by Prefer: timezone +userApiRequest :: AppConfig -> Request -> RequestBody -> TimezoneNames -> Either ApiRequestError ApiRequest +userApiRequest conf req reqBody timezones = do resource <- getResource conf $ pathInfo req (schema, negotiatedByProfile) <- getSchema conf hdrs method act <- getAction resource schema method @@ -141,7 +142,7 @@ userApiRequest conf req reqBody sCache = do , iRange = ranges , iTopLevelRange = topLevelRange , iPayload = payload - , iPreferences = Preferences.fromHeaders (configDbTxAllowOverride conf) (dbTimezones sCache) hdrs + , iPreferences = Preferences.fromHeaders (configDbTxAllowOverride conf) timezones hdrs , iQueryParams = qPrms , iColumns = columns , iHeaders = iHdrs diff --git a/src/PostgREST/App.hs b/src/PostgREST/App.hs index 254860afa..75c0d091c 100644 --- a/src/PostgREST/App.hs +++ b/src/PostgREST/App.hs @@ -144,8 +144,9 @@ postgrestResponse appState conf@AppConfig{..} maybeSchemaCache pgVer authResult@ body <- lift $ Wai.strictRequestBody req let jwtTime = if configServerTimingEnabled then Auth.getJwtDur req else Nothing + timezones = dbTimezones sCache - (parseTime, apiReq@ApiRequest{..}) <- withTiming $ liftEither . mapLeft Error.ApiRequestError $ ApiRequest.userApiRequest conf req body sCache + (parseTime, apiReq@ApiRequest{..}) <- withTiming $ liftEither . mapLeft Error.ApiRequestError $ ApiRequest.userApiRequest conf req body timezones (planTime, plan) <- withTiming $ liftEither $ Plan.actionPlan iAction conf apiReq sCache let query = Query.query conf authResult apiReq plan sCache pgVer