Skip to content

Commit

Permalink
Collapse the different functions for both generator types
Browse files Browse the repository at this point in the history
  • Loading branch information
erewok committed Feb 20, 2017
1 parent ae43862 commit 88684c9
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 19 deletions.
3 changes: 3 additions & 0 deletions src/Servant/PY.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ import Servant.PY.Requests
python :: HasForeign NoTypes NoContent api => Proxy api -> Foreign NoContent api
python p = foreignFor (Proxy :: Proxy NoTypes) (Proxy :: Proxy NoContent) p defReq

-- | Generate the data necessary to generate Python code
-- for all the endpoints of an API, but try to get as much type-information
-- into Python docstrings, in order to aid discoverability of client functions.
pythonTyped :: HasForeign Python Text api => Proxy api -> Foreign Text api
pythonTyped p = foreignFor (Proxy :: Proxy Python) (Proxy :: Proxy Text) p defReq

Expand Down
19 changes: 1 addition & 18 deletions src/Servant/PY/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ module Servant.PY.Internal
, captures
, withFormattedCaptures
, buildDocString
, buildDocStringWithTypes
, buildHeaderDict
, functionArguments
, formatBuilder
Expand Down Expand Up @@ -286,7 +285,7 @@ capturesToFormatArgs segments = map getSegment $ filter isCapture segments
getSegment _ = ""
getCapture s = s ^. argName . _PathSegment

buildDocString :: PyRequest -> CommonGeneratorOptions -> T.Text
buildDocString :: forall f. Req f -> CommonGeneratorOptions -> T.Text
buildDocString req opts = T.toUpper method <> " \"" <> url <> "\n"
<> includeArgs <> "\n\n"
<> indent' <> "Returns: " <> "\n"
Expand All @@ -301,19 +300,3 @@ buildDocString req opts = T.toUpper method <> " \"" <> url <> "\n"
returnVal = case returnMode opts of
DangerMode -> "JSON response from the endpoint"
RawResponse -> "response (requests.Response) from issuing the request"

buildDocStringWithTypes :: PyTypedRequest -> CommonGeneratorOptions -> T.Text
buildDocStringWithTypes req opts = T.toUpper method <> " \"" <> url <> "\n"
<> includeArgs <> "\n\n"
<> indent' <> "Returns: " <> "\n"
<> indent' <> indent' <> returnVal
where args = capturesToFormatArgs $ req ^.. reqUrl.path.traverse
method = decodeUtf8 $ req ^. reqMethod
url = makePyUrl' $ req ^.. reqUrl.path.traverse
includeArgs = if null args then "" else argDocs
argDocs = indent' <> "Args: " <> "\n"
<> indent' <> indent' <> T.intercalate ("\n" <> indent' <> indent') args
indent' = indentation opts indent
returnVal = case returnMode opts of
DangerMode -> "JSON response from the endpoint"
RawResponse -> "response (requests.Response) from issuing the request"
2 changes: 1 addition & 1 deletion src/Servant/PY/Requests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ generatePyTypedRequestWith :: CommonGeneratorOptions -> PyTypedRequest -> Text
generatePyTypedRequestWith opts req = "\n" <>
"def " <> fname <> "(" <> argsStr <> "):\n"
<> indent' <> docStringMarker
<> indent' <> buildDocStringWithTypes req opts <> "\n"
<> indent' <> buildDocString req opts <> "\n"
<> indent' <> docStringMarker
<> indent' <> "url = " <> makePyUrl opts req (indent' <> indent') <> "\n\n"
<> headerDef
Expand Down

0 comments on commit 88684c9

Please sign in to comment.