Skip to content

Commit

Permalink
Create dedicated body type for requests
Browse files Browse the repository at this point in the history
Signed-off-by: Sascha Grunert <mail@saschagrunert.de>
  • Loading branch information
saschagrunert committed Jun 16, 2019
1 parent 0c96683 commit d696fe0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
4 changes: 2 additions & 2 deletions config/models.persistentmodels
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Test
benchmarks [Benchmark]
environment Environment
benchmarks [BenchmarkId]
environment EnvironmentId
time UTCTime
deriving Show

Expand Down
1 change: 1 addition & 0 deletions nix/shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pkgs.stdenv.mkDerivation {
hlint
nix-prefetch-git
sass
sqlite
wget
zlib
];
Expand Down
10 changes: 8 additions & 2 deletions src/Handler/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
module Handler.Api ( getApiR, postApiR ) where

import Data.Aeson.Types as T ( Result(Success) )
import Data.Time.Clock ( getCurrentTime )

import Import

Expand All @@ -14,7 +15,12 @@ getApiR = return $ String "Hello world"

postApiR :: Handler ()
postApiR = do
body <- parseCheckJsonBody :: Handler (T.Result Test)
body <- parseCheckJsonBody :: Handler (T.Result ReqBody)
case body of
Success _ -> sendResponseStatus status200 ()
Success (e, b) -> do
eId <- runDB $ insert e
bIds <- runDB $ mapM insert b
time <- liftIO getCurrentTime
_ <- runDB . insertEntity $ Test bIds eId time
sendResponseStatus status200 ()
_ -> sendResponseStatus status400 ()
3 changes: 3 additions & 0 deletions src/Model.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
module Model
( Benchmark(Benchmark)
, BenchmarkId
, ReqBody
, Environment(Environment)
, EnvironmentId
, Test(Test)
Expand Down Expand Up @@ -41,6 +42,8 @@ share [ mkPersist sqlSettings { mpsGenerateLenses = True }
]
$(persistFileWith lowerCaseSettings "config/models.persistentmodels")

type ReqBody = (Environment, [Benchmark])

-- | Drop the "_benchmark" from the Benchmark
deriveJSON defaultOptions { fieldLabelModifier = drop 10 } ''Benchmark

Expand Down
26 changes: 10 additions & 16 deletions src/ParserResult.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ import Control.Exception ( displayException, try )

import Data.Aeson ( encodeFile )
import Data.ByteString.Lazy.Char8 as C ( unpack )
import Data.Time.Clock ( getCurrentTime )

import GoParser ( parse )

import Log ( debug, err, notice, noticeR )

import Model
( Benchmark, Environment, Test(Test) )
import Model ( Benchmark, Environment, ReqBody )

import Network.HTTP.Simple
( HttpException, Request, getResponseBody
Expand Down Expand Up @@ -72,30 +70,26 @@ debugResult :: ParserResult -> IO ()
debugResult r = debug . printf "Current result: %s" $ show r

-- | Store the current result on disk
toDisk :: Test -> IO FilePath
toDisk t = do
toDisk :: ReqBody -> IO FilePath
toDisk b = do
f <- emptySystemTempFile "result-.json"
debug $ printf "Writing to temp file: %s" f
encodeFile f t
encodeFile f b
return f

-- | Convert a parser result and a given environment to a test model
toTest :: ParserResult -> Environment -> IO Test
toTest r e = Test r e <$> getCurrentTime

-- | Sen the provided data to the given url including the environment
send :: ParserStep -> String -> Environment -> IO ()
send (_, r) u e = do
t <- toTest r e
p <- toDisk t
let body = (e, r)
p <- toDisk body
request <- buildRequest u p
debug . printf "Doing HTTP request:\n%s" $ show request
doRequest request t p
doRequest request body p

-- | Do the provided request
doRequest :: Request -> Test -> FilePath -> IO ()
doRequest r t p = do
response <- try . httpLBS $ setRequestBodyJSON t r
doRequest :: Request -> ReqBody -> FilePath -> IO ()
doRequest r b p = do
response <- try . httpLBS $ setRequestBodyJSON b r
case response of
Right res -> case getResponseStatusCode res of
200 -> do
Expand Down

0 comments on commit d696fe0

Please sign in to comment.