Refactor request builder and Payload type

This commit is contained in:
Aditya Manthramurthy 2017-01-10 01:17:29 +05:30
parent 1ad670a328
commit c9f6d666db
3 changed files with 28 additions and 51 deletions

View File

@ -34,61 +34,43 @@ import Network.Minio.Sign.V4
-- -- print $ NC.requestBody r -- -- print $ NC.requestBody r
-- NC.httpLbs r mgr -- NC.httpLbs r mgr
executeRequest :: RequestInfo -> Minio (Response LByteString) buildRequest :: (MonadIO m, MonadReader MinioConn m)
executeRequest ri = do => RequestInfo -> m NC.Request
let PayloadSingle pload = riPayload ri buildRequest ri = do
let pload = maybe "" identity $ riPayload ri
phash = hashSHA256 pload phash = hashSHA256 pload
newRI = ri { newRi = ri {
riPayloadHash = phash riPayloadHash = phash
, riHeaders = ("x-amz-content-sha256", phash) : (riHeaders ri) , riHeaders = ("x-amz-content-sha256", phash) : (riHeaders ri)
} }
ci <- asks mcConnInfo ci <- asks mcConnInfo
reqHeaders <- liftIO $ signV4 ci newRI reqHeaders <- liftIO $ signV4 ci newRi
return NC.defaultRequest {
NC.method = riMethod newRi
, NC.secure = connectIsSecure ci
, NC.host = encodeUtf8 $ connectHost ci
, NC.port = connectPort ci
, NC.path = getPathFromRI ri
, NC.queryString = HT.renderQuery False $ riQueryParams ri
, NC.requestHeaders = reqHeaders
, NC.requestBody = NC.RequestBodyBS pload
}
executeRequest :: RequestInfo -> Minio (Response LByteString)
executeRequest ri = do
req <- buildRequest ri
mgr <- asks mcConnManager mgr <- asks mcConnManager
let req = NC.defaultRequest {
NC.method = riMethod newRI
, NC.secure = connectIsSecure ci
, NC.host = encodeUtf8 $ connectHost ci
, NC.port = connectPort ci
, NC.path = getPathFromRI ri
, NC.queryString = HT.renderQuery False $ riQueryParams ri
, NC.requestHeaders = reqHeaders
, NC.requestBody = NC.RequestBodyBS pload
}
NC.httpLbs req mgr NC.httpLbs req mgr
mkStreamRequest :: RequestInfo mkStreamRequest :: RequestInfo
-> Minio (Response (C.ResumableSource Minio ByteString)) -> Minio (Response (C.ResumableSource Minio ByteString))
mkStreamRequest ri = do mkStreamRequest ri = do
let PayloadSingle pload = riPayload ri req <- buildRequest ri
phash = hashSHA256 pload
newRI = ri {
riPayloadHash = phash
, riHeaders = ("x-amz-content-sha256", phash) : (riHeaders ri)
}
ci <- asks mcConnInfo
reqHeaders <- liftIO $ signV4 ci newRI
mgr <- asks mcConnManager mgr <- asks mcConnManager
let req = NC.defaultRequest {
NC.method = riMethod newRI
, NC.secure = connectIsSecure ci
, NC.host = encodeUtf8 $ connectHost ci
, NC.port = connectPort ci
, NC.path = getPathFromRI ri
, NC.queryString = HT.renderQuery False $ riQueryParams ri
, NC.requestHeaders = reqHeaders
, NC.requestBody = NC.RequestBodyBS pload
}
NC.http req mgr NC.http req mgr
requestInfo :: Method -> Maybe Bucket -> Maybe Object requestInfo :: Method -> Maybe Bucket -> Maybe Object

View File

@ -56,9 +56,7 @@ data BucketInfo = BucketInfo {
, biCreationDate :: UTCTime , biCreationDate :: UTCTime
} deriving (Show, Eq) } deriving (Show, Eq)
type Payload = Maybe ByteString
data Payload = PayloadSingle ByteString
deriving (Show, Eq)
data RequestInfo = RequestInfo { data RequestInfo = RequestInfo {
riMethod :: Method riMethod :: Method

View File

@ -26,15 +26,14 @@ status204 = HT.Status{ HT.statusCode = 204, HT.statusMessage = "No Content" }
getService :: Minio [BucketInfo] getService :: Minio [BucketInfo]
getService = do getService = do
resp <- executeRequest $ resp <- executeRequest $
requestInfo HT.methodGet Nothing Nothing [] [] $ requestInfo HT.methodGet Nothing Nothing [] [] Nothing
PayloadSingle ""
parseListBuckets $ NC.responseBody resp parseListBuckets $ NC.responseBody resp
getLocation :: Bucket -> Minio Text getLocation :: Bucket -> Minio Text
getLocation bucket = do getLocation bucket = do
resp <- executeRequest $ resp <- executeRequest $
requestInfo HT.methodGet (Just bucket) Nothing [("location", Nothing)] [] requestInfo HT.methodGet (Just bucket) Nothing [("location", Nothing)] []
(PayloadSingle "") Nothing
parseLocation $ NC.responseBody resp parseLocation $ NC.responseBody resp
getObject :: Bucket -> Object -> HT.Query -> [HT.Header] getObject :: Bucket -> Object -> HT.Query -> [HT.Header]
@ -48,13 +47,13 @@ getObject bucket object queryParams headers = do
throwError $ MErrXml $ LBS.toStrict $ NC.responseBody errMsg throwError $ MErrXml $ LBS.toStrict $ NC.responseBody errMsg
where where
reqInfo = requestInfo HT.methodGet (Just bucket) (Just object) reqInfo = requestInfo HT.methodGet (Just bucket) (Just object)
queryParams headers (PayloadSingle "") queryParams headers Nothing
putBucket :: Bucket -> Location -> Minio () putBucket :: Bucket -> Location -> Minio ()
putBucket bucket location = do putBucket bucket location = do
resp <- executeRequest $ resp <- executeRequest $
requestInfo HT.methodPut (Just bucket) Nothing [] [] $ requestInfo HT.methodPut (Just bucket) Nothing [] [] $
PayloadSingle $ mkCreateBucketConfig bucket location Just $ mkCreateBucketConfig bucket location
let httpStatus = NC.responseStatus resp let httpStatus = NC.responseStatus resp
when (httpStatus /= HT.ok200) $ when (httpStatus /= HT.ok200) $
@ -63,8 +62,7 @@ putBucket bucket location = do
deleteBucket :: Bucket -> Minio () deleteBucket :: Bucket -> Minio ()
deleteBucket bucket = do deleteBucket bucket = do
resp <- executeRequest $ resp <- executeRequest $
requestInfo HT.methodDelete (Just bucket) Nothing [] [] $ requestInfo HT.methodDelete (Just bucket) Nothing [] [] Nothing
(PayloadSingle "")
let httpStatus = NC.responseStatus resp let httpStatus = NC.responseStatus resp
when (httpStatus /= status204) $ when (httpStatus /= status204) $
throwError $ MErrXml $ LBS.toStrict $ NC.responseBody resp throwError $ MErrXml $ LBS.toStrict $ NC.responseBody resp
@ -72,8 +70,7 @@ deleteBucket bucket = do
deleteObject :: Bucket -> Object -> Minio () deleteObject :: Bucket -> Object -> Minio ()
deleteObject bucket object = do deleteObject bucket object = do
resp <- executeRequest $ resp <- executeRequest $
requestInfo HT.methodDelete (Just bucket) (Just object) [] [] $ requestInfo HT.methodDelete (Just bucket) (Just object) [] [] Nothing
(PayloadSingle "")
let httpStatus = NC.responseStatus resp let httpStatus = NC.responseStatus resp
when (httpStatus /= status204) $ when (httpStatus /= status204) $
throwError $ MErrXml $ LBS.toStrict $ NC.responseBody resp throwError $ MErrXml $ LBS.toStrict $ NC.responseBody resp