-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OpenAPI (#251): put HTTP code in separate module
- Loading branch information
1 parent
ad73494
commit c412449
Showing
4 changed files
with
25 additions
and
17 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,21 @@ | ||
module Pinafore.WebAPI.HTTP | ||
( callHTTP | ||
) where | ||
|
||
import Data.Aeson as Aeson | ||
import Network.HTTP.Simple | ||
import Network.HTTP.Types (statusCode) | ||
import Shapes | ||
|
||
callHTTP :: Text -> Text -> Value -> IO Value | ||
callHTTP op uri obj = do | ||
plainRequest <- parseRequest $ unpack uri | ||
let | ||
request :: Request | ||
request = setRequestBodyJSON obj $ setRequestMethod (encodeUtf8 op) plainRequest | ||
response <- httpJSON request | ||
case statusCode $ getResponseStatus response of | ||
200 -> return $ getResponseBody response | ||
201 -> return $ getResponseBody response | ||
204 -> return Null | ||
i -> fail $ "failed with HTTP status " <> show i |
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
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
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