-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sut): add common http client for dumblog
- Loading branch information
1 parent
5f11e49
commit 11cccee
Showing
3 changed files
with
83 additions
and
1 deletion.
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
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,80 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Dumblog.Common.HttpClient where | ||
|
||
import Control.Monad (when) | ||
import qualified Data.ByteString.Char8 as BSChar8 | ||
import Data.ByteString.Lazy.Char8 (ByteString) | ||
import qualified Data.ByteString.Lazy.Char8 as LBSChar8 | ||
import Network.HTTP.Client | ||
( Manager | ||
, Request | ||
, RequestBody(RequestBodyLBS) | ||
, defaultManagerSettings | ||
, httpLbs | ||
, httpNoBody | ||
, method | ||
, newManager | ||
, parseRequest | ||
, path | ||
, requestBody | ||
, responseBody | ||
, responseStatus | ||
) | ||
import Network.HTTP.Types.Status (ok200) | ||
import Network.Wai.Handler.Warp (Port) | ||
|
||
------------------------------------------------------------------------ | ||
|
||
data HttpClient = HttpClient | ||
{ hcManager :: Manager -- | NOTE: If possible, you should share a single | ||
-- `Manager` between multiple threads and requests. | ||
, hcWriteReq :: ByteString -> Request | ||
, hcReadReq :: Int -> Request | ||
-- , hcErrors :: AtomicCounter | ||
} | ||
|
||
newHttpClient :: String -> Port -> IO HttpClient | ||
newHttpClient host port = do | ||
mgr <- newManager defaultManagerSettings | ||
initReq <- parseRequest ("http://" ++ host ++ ":" ++ show port) | ||
|
||
let writeReq :: ByteString -> Request | ||
writeReq bs = initReq { method = "POST" | ||
, requestBody = RequestBodyLBS bs | ||
} | ||
|
||
readReq :: Int -> Request | ||
readReq ix = initReq { method = "GET" | ||
, path = path initReq <> BSChar8.pack (show ix) | ||
} | ||
|
||
return (HttpClient mgr writeReq readReq) | ||
|
||
writeHttp :: HttpClient -> ByteString -> IO Int | ||
writeHttp hc bs = do | ||
resp <- httpLbs (hcWriteReq hc bs) (hcManager hc) | ||
when (responseStatus resp /= ok200) $ | ||
return () -- XXX: increment hcErrors | ||
return (read (LBSChar8.unpack (responseBody resp))) | ||
|
||
readHttp :: HttpClient -> Int -> IO ByteString | ||
readHttp hc ix = do | ||
resp <- httpLbs (hcReadReq hc ix) (hcManager hc) | ||
when (responseStatus resp /= ok200) $ | ||
return () -- XXX: increment hcErrors | ||
return (responseBody resp) | ||
|
||
writeHttp_ :: HttpClient -> ByteString -> IO () | ||
writeHttp_ hc bs = do | ||
resp <- httpNoBody (hcWriteReq hc bs) (hcManager hc) | ||
when (responseStatus resp /= ok200) $ | ||
return () -- XXX: increment hcErrors | ||
return () | ||
|
||
readHttp_ :: HttpClient -> Int -> IO () | ||
readHttp_ hc ix = do | ||
resp <- httpNoBody (hcReadReq hc ix) (hcManager hc) | ||
when (responseStatus resp /= ok200) $ | ||
return () -- XXX: increment hcErrors | ||
return () |
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