From 9a07a66f3cedb3f8d60094bc1ecc277365ac663a Mon Sep 17 00:00:00 2001 From: Erik Aker Date: Thu, 9 Mar 2017 11:13:42 -0800 Subject: [PATCH] Move functions to internal module for sharing with others --- src/Servant/PY/Internal.hs | 14 ++++++++++++++ src/Servant/PY/Requests.hs | 15 --------------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/Servant/PY/Internal.hs b/src/Servant/PY/Internal.hs index 4734b71..099b8f8 100644 --- a/src/Servant/PY/Internal.hs +++ b/src/Servant/PY/Internal.hs @@ -36,6 +36,7 @@ module Servant.PY.Internal , buildHeaderDict , functionArguments , formatBuilder + , remainingReqCall -- re-exports , (:<|>)(..) , (:>) @@ -354,3 +355,16 @@ getMethod (UnTypedPythonRequest req) = decodeUtf8 $ req ^. reqMethod hasBody :: PythonRequest -> Bool hasBody (TypedPythonRequest req) = isJust (req ^. reqBody) hasBody (UnTypedPythonRequest req) = isJust (req ^. reqBody) + +remainingReqCall :: PyRequestArgs -> Int -> Text +remainingReqCall reqArgs width + | null argsAsList = ")" + | length argsAsList == 1 = ",\n" <> offset <> head argsAsList <> ")\n" + | otherwise = ",\n" <> offset <> T.intercalate (",\n" <> offset) argsAsList <> ")\n" + where argsAsList = requestArgsToList reqArgs + offset = mconcat $ replicate width " " + +requestArgsToList :: PyRequestArgs -> [Text] +requestArgsToList reqArgs = map snd . filter fst $ zip bools strings + where bools = [hasHeaders reqArgs, hasParams reqArgs, hasData reqArgs] + strings = ["headers=headers", "params=params", "json=data"] diff --git a/src/Servant/PY/Requests.hs b/src/Servant/PY/Requests.hs index 9d0c6f6..82f1679 100644 --- a/src/Servant/PY/Requests.hs +++ b/src/Servant/PY/Requests.hs @@ -67,21 +67,6 @@ generatePyRequestWith opts req = "\n" <> DangerMode -> "JSON response from the endpoint" RawResponse -> "response (requests.Response) from issuing the request" - -remainingReqCall :: PyRequestArgs -> Int -> Text -remainingReqCall reqArgs width - | null argsAsList = ")" - | length argsAsList == 1 = ",\n" <> offset <> head argsAsList <> ")\n" - | otherwise = ",\n" <> offset <> T.intercalate (",\n" <> offset) argsAsList <> ")\n" - where argsAsList = requestArgsToList reqArgs - offset = mconcat $ replicate width " " - -requestArgsToList :: PyRequestArgs -> [Text] -requestArgsToList reqArgs = map snd . filter fst $ zip bools strings - where bools = [hasHeaders reqArgs, hasParams reqArgs, hasData reqArgs] - strings = ["headers=headers", "params=params", "json=data"] - - functionReturn :: ReturnStyle -> (Proxy Indent -> T.Text) -> T.Text functionReturn DangerMode pyindenter = indent' <> "resp.raise_for_status()\n" <> indent' <> "return resp.json()"