From 60e231d47c1d2be088fd3cbd9a2ecece9ba4b9e9 Mon Sep 17 00:00:00 2001 From: Michael Gilliland Date: Mon, 24 Jun 2024 11:23:11 -0400 Subject: [PATCH] Add `Freckle.App.Http (setRequestMethod)` (#176) **Why?** As far as I know there are currently 2 ways to use a non-`GET` request method: 1. including method in the request string (e.g. `parseRequest_ $ "POST " <> url`) 2. importing lower-level HTTP module and setting the field directly (e.g. `req { HTTP.method = "POST"}`) **2** breaks the centralization we're providing with this module by requiring consumers to import an internal thing. Also both of these are stringly typed, producing a runtime error if a mistake is made, exs. forgotten space, or casing: > Note that the request method must be provided as all capital letters. ^ from `http-conduit`'s docs. **Note** We could just re-export `Network.HTTP.Simple (setRequestMethod)`, however, still has the stringly-typed problem (it takes a `ByteString`). `StdMethod` gives us nice type-safety and covers all our current (and I'd bet future) use-cases. --- library/Freckle/App/Http.hs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/library/Freckle/App/Http.hs b/library/Freckle/App/Http.hs index 1cfb1b1..62f8f23 100644 --- a/library/Freckle/App/Http.hs +++ b/library/Freckle/App/Http.hs @@ -32,6 +32,7 @@ module Freckle.App.Http , setRequestBodyJSON , setRequestBodyURLEncoded , setRequestCheckStatus + , setRequestMethod , setRequestPath , disableRequestDecompress @@ -71,6 +72,7 @@ module Freckle.App.Http , statusIsRedirection , statusIsClientError , statusIsServerError + , StdMethod (..) ) where import Freckle.App.Prelude @@ -91,8 +93,9 @@ import Freckle.App.Http.Paginate import Freckle.App.Http.Retry import Network.HTTP.Client qualified as HTTP (Request (..)) import Network.HTTP.Conduit (HttpExceptionContent (..)) -import Network.HTTP.Simple hiding (httpLbs, httpNoBody) +import Network.HTTP.Simple hiding (httpLbs, httpNoBody, setRequestMethod) import Network.HTTP.Simple qualified as HTTP +import Network.HTTP.Types (StdMethod (..), renderStdMethod) import Network.HTTP.Types.Header (hAccept, hAuthorization) import Network.HTTP.Types.Status ( Status @@ -251,6 +254,9 @@ addAcceptHeader = addRequestHeader hAccept addBearerAuthorizationHeader :: BS.ByteString -> Request -> Request addBearerAuthorizationHeader = addRequestHeader hAuthorization . ("Bearer " <>) +setRequestMethod :: StdMethod -> Request -> Request +setRequestMethod method req = req {HTTP.method = renderStdMethod method} + disableRequestDecompress :: Request -> Request disableRequestDecompress req = req