-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit of servant-py library. Usable probably for beta testing
- Loading branch information
0 parents
commit 0c4f3db
Showing
11 changed files
with
830 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
*~ | ||
.stack-work/ | ||
build/ | ||
examples/api.py | ||
__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
Copyright Erik Aker (c) 2017 | ||
|
||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
|
||
* Redistributions in binary form must reproduce the above | ||
copyright notice, this list of conditions and the following | ||
disclaimer in the documentation and/or other materials provided | ||
with the distribution. | ||
|
||
* Neither the name of Author name here nor the names of other | ||
contributors may be used to endorse or promote products derived | ||
from this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
# servant-py | ||
|
||
This library lets you derive automatically Python functions that let you query each endpoint of a *servant* webservice. | ||
|
||
Currently, the only supported method for generating requests is via the `requests` library, which is the recommended way to generate HTTP requests in the Python world (even among Python core devs). | ||
|
||
## Inspiration | ||
|
||
This library is largely inspired by [servant-js](https://github.com/haskell-servant/servant-js) and by the fantastic work of the Servant team in general. Any good ideas you find in here are from their work (any mistakes are almost entirely mine, however). | ||
|
||
## Example | ||
|
||
There are two different styles of function-return supported here: `DangerMode` and `RawResponse`. | ||
|
||
The latter returns the raw response from issuing the request and the former calls `raise_for_status` and then attempts to return `resp.json()`. You can switch which style you'd like to use by creating a proper `CommonGeneratorOptions` object. | ||
|
||
The default options just chucks it all to the wind and goes for `DangerMode` (because, seriously, we're using Haskell to generate Python here...): | ||
|
||
|
||
``` haskell | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE DeriveGeneric #-} | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
{-# LANGUAGE TypeOperators #-} | ||
|
||
module Main where | ||
|
||
import Data.Aeson | ||
import qualified Data.ByteString.Char8 as B | ||
import Data.Proxy | ||
import qualified Data.Text as T | ||
import GHC.Generics | ||
import Servant | ||
import System.FilePath | ||
|
||
import Servant.PY | ||
|
||
-- * A simple Counter data type | ||
newtype Counter = Counter { value :: Int } | ||
deriving (Generic, Show, Num) | ||
instance ToJSON Counter | ||
|
||
data LoginForm = LoginForm | ||
{ username :: !T.Text | ||
, password :: !T.Text | ||
, otherMissing :: Maybe T.Text | ||
} deriving (Eq, Show, Generic) | ||
instance ToJSON LoginForm | ||
|
||
-- * Our API type | ||
type TestApi = "counter-req-header" :> Post '[JSON] Counter | ||
:<|> "counter-queryparam" | ||
:> QueryParam "sortby" T.Text | ||
:> Header "Some-Header" T.Text :> Get '[JSON] Counter | ||
:<|> "login-queryflag" :> QueryFlag "published" :> Get '[JSON] LoginForm | ||
:<|> "login-params-authors-with-reqBody" | ||
:> QueryParams "authors" T.Text | ||
:> ReqBody '[JSON] LoginForm :> Post '[JSON] LoginForm | ||
:<|> "login-with-path-var-and-header" | ||
:> Capture "id" Int | ||
:> Capture "Name" T.Text | ||
:> Capture "hungrig" Bool | ||
:> ReqBody '[JSON] LoginForm | ||
:> Post '[JSON] (Headers '[Header "test-head" B.ByteString] LoginForm) | ||
|
||
testApi :: Proxy TestApi | ||
testApi = Proxy | ||
|
||
-- where our static files reside | ||
result :: FilePath | ||
result = "examples" | ||
|
||
main :: IO () | ||
main = writePythonForAPI testApi requests (result </> "api.py") | ||
``` | ||
|
||
If you build this and run it, you will get some output that looks like the following: | ||
|
||
```python | ||
|
||
from urllib import parse | ||
|
||
import requests | ||
|
||
def post_counterreqheader(): | ||
""" | ||
POST "counter-req-header" | ||
""" | ||
url = "/counter-req-header" | ||
|
||
resp = requests.post(url) | ||
resp.raise_for_status() | ||
return resp.json() | ||
|
||
|
||
def get_counterqueryparam(sortby, header_SomeHeader): | ||
""" | ||
GET "counter-queryparam" | ||
""" | ||
url = "/counter-queryparam" | ||
|
||
headers = {"Some-Header": headerSomeHeader} | ||
params = {"sortby": sortby} | ||
resp = requests.get(url | ||
headers=headers, | ||
params=params) | ||
|
||
resp.raise_for_status() | ||
return resp.json() | ||
|
||
|
||
def get_loginqueryflag(published): | ||
""" | ||
GET "login-queryflag" | ||
""" | ||
url = "/login-queryflag" | ||
|
||
params = {"published": published} | ||
resp = requests.get(url | ||
params=params) | ||
|
||
resp.raise_for_status() | ||
return resp.json() | ||
|
||
|
||
def post_loginparamsauthorswithreqBody(authors, data): | ||
""" | ||
POST "login-params-authors-with-reqBody" | ||
""" | ||
url = "/login-params-authors-with-reqBody" | ||
|
||
params = {"authors": authors} | ||
resp = requests.post(url | ||
params=params, | ||
json=data) | ||
|
||
resp.raise_for_status() | ||
return resp.json() | ||
|
||
|
||
def post_loginwithpathvarandheader_by_id_by_Name_by_hungrig(id, Name, hungrig, data): | ||
""" | ||
POST "login-with-path-var-and-header/{id}/{Name}/{hungrig}" | ||
Args: | ||
id | ||
Name | ||
hungrig | ||
""" | ||
url = "/login-with-path-var-and-header/{id}/{Name}/{hungrig}".format( | ||
id=parse.quote(id), | ||
Name=parse.quote(Name), | ||
hungrig=parse.quote(hungrig)) | ||
|
||
resp = requests.post(url | ||
json=data) | ||
|
||
resp.raise_for_status() | ||
return resp.json() | ||
``` | ||
|
||
If you would like to compile and run this example yourself, you can do that like so: | ||
|
||
``` | ||
$ stack build --flag servant-py:example | ||
$ stack exec servant-py-exe | ||
$ cat examples/api.py | ||
``` | ||
|
||
## TODO | ||
|
||
1. Add Tests Pronto! | ||
2. Fix `urllib.parse.quote` on non-string args. Need to know more about what's getting passed or convert all to strings. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import Distribution.Simple | ||
main = defaultMain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE DeriveGeneric #-} | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
{-# LANGUAGE TypeOperators #-} | ||
|
||
module Main where | ||
|
||
import Data.Aeson | ||
import qualified Data.ByteString.Char8 as B | ||
import Data.Proxy | ||
import qualified Data.Text as T | ||
import GHC.Generics | ||
import Servant | ||
import System.FilePath | ||
|
||
import Servant.PY | ||
|
||
-- * A simple Counter data type | ||
newtype Counter = Counter { value :: Int } | ||
deriving (Generic, Show, Num) | ||
instance ToJSON Counter | ||
|
||
data LoginForm = LoginForm | ||
{ username :: !T.Text | ||
, password :: !T.Text | ||
, otherMissing :: Maybe T.Text | ||
} deriving (Eq, Show, Generic) | ||
instance ToJSON LoginForm | ||
|
||
-- * Our API type | ||
type TestApi = "counter-req-header" :> Post '[JSON] Counter | ||
:<|> "counter-queryparam" | ||
:> QueryParam "sortby" T.Text | ||
:> Header "Some-Header" T.Text :> Get '[JSON] Counter | ||
:<|> "login-queryflag" :> QueryFlag "published" :> Get '[JSON] LoginForm | ||
:<|> "login-params-authors-with-reqBody" | ||
:> QueryParams "authors" T.Text | ||
:> ReqBody '[JSON] LoginForm :> Post '[JSON] LoginForm | ||
:<|> "login-with-path-var-and-header" | ||
:> Capture "id" Int | ||
:> Capture "Name" T.Text | ||
:> Capture "hungrig" Bool | ||
:> ReqBody '[JSON] LoginForm | ||
:> Post '[JSON] (Headers '[Header "test-head" B.ByteString] LoginForm) | ||
|
||
testApi :: Proxy TestApi | ||
testApi = Proxy | ||
|
||
-- where our static files reside | ||
result :: FilePath | ||
result = "examples" | ||
|
||
main :: IO () | ||
main = writePythonForAPI testApi requests (result </> "api.py") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
name: servant-py | ||
version: 0.1.0.0 | ||
synopsis: Automatically derive python functions to query servant webservices. | ||
description: | ||
Automatically derive python functions to query servant webservices. | ||
. | ||
Supports deriving functions using Python's requests library. | ||
homepage: https://github.com/pellagic-puffbomb/servant-py#readme | ||
license: BSD3 | ||
license-file: LICENSE | ||
author: Erik Aker | ||
maintainer: [email protected] | ||
copyright: 2017 Erik Aker | ||
category: Web | ||
build-type: Simple | ||
extra-source-files: README.md | ||
cabal-version: >=1.10 | ||
|
||
flag example | ||
description: Build the example too | ||
manual: True | ||
default: False | ||
|
||
library | ||
hs-source-dirs: src | ||
exposed-modules: Servant.PY | ||
, Servant.PY.Requests | ||
, Servant.PY.Internal | ||
build-depends: base >= 4.7 && < 5 | ||
, aeson | ||
, charset | ||
, lens | ||
, servant-foreign | ||
, servant | ||
, text | ||
default-language: Haskell2010 | ||
default-extensions: OverloadedStrings | ||
ghc-options: -Wall | ||
|
||
executable servant-py-exe | ||
ghc-options: -Wall | ||
hs-source-dirs: examples | ||
|
||
if flag(example) | ||
buildable: True | ||
else | ||
buildable: False | ||
hs-source-dirs: examples | ||
main-is: Main.hs | ||
build-depends: base | ||
, blaze-html | ||
, bytestring | ||
, servant-py | ||
, stm | ||
, aeson | ||
, wai | ||
, servant | ||
, servant-server | ||
, servant-blaze | ||
, text | ||
, filepath | ||
, warp | ||
default-language: Haskell2010 | ||
|
||
test-suite servant-py-test | ||
type: exitcode-stdio-1.0 | ||
hs-source-dirs: test | ||
main-is: Spec.hs | ||
build-depends: base | ||
, servant-py | ||
ghc-options: -threaded -rtsopts -with-rtsopts=-N | ||
default-language: Haskell2010 | ||
|
||
source-repository head | ||
type: git | ||
location: https://github.com/pellagic-puffbomb/servant-py |
Oops, something went wrong.