Skip to content

Commit

Permalink
Add inputStreamPart; documentation improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
istathar committed Aug 2, 2021
1 parent 4e58382 commit 24e61fd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
4 changes: 3 additions & 1 deletion lib/Network/Http/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ module Network.Http.Client (
FieldName,
Boundary,
randomBoundary,
packBoundary,
setContentMultipart,
setExpectContinue,
setTransferEncoding,
Expand All @@ -136,8 +135,10 @@ module Network.Http.Client (
inputStreamBody,
encodedFormBody,
multipartFormBody,
Part,
simplePart,
filePart,
inputStreamPart,
jsonBody,

-- * Processing HTTP response
Expand Down Expand Up @@ -187,6 +188,7 @@ module Network.Http.Client (
Headers,
getHeaders,
getHeadersFull,
packBoundary,

-- * Deprecated
concatHandler,
Expand Down
33 changes: 23 additions & 10 deletions lib/Network/Http/Inconvenience.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module Network.Http.Inconvenience (
Part,
simplePart,
filePart,
inputStreamPart,
put,
baselineContextSSL,
simpleHandler',
Expand Down Expand Up @@ -521,29 +522,28 @@ You use this partially applied:
>
> sendRequest c q (multipartFormBody boundary parts)
You need to have called 'setContentMultipart' when forming the request or the
request body you are sending will be invalid and (obviously) pass in the same
Boundary value.
You /must/ have called 'setContentMultipart' when forming the request or the
request body you are sending will be invalid and (obviously) you must pass in
that same 'Boundary' value when calling this function.
-}
multipartFormBody :: Boundary -> [Part] -> OutputStream Builder -> IO ()
multipartFormBody boundary parts o = do
mapM_ handlePart parts
handleEnding
where
-- boundary = "bEacHV0113YB@ll"

handlePart :: Part -> IO ()
handlePart (Part field possibleContentType possibleFilename action) = do
let h' = composeMultipartBytes boundary field possibleFilename possibleContentType
Streams.write (Just h') o
action o

handleEnding :: IO ()
handleEnding = do
Streams.write (Just (composeMultipartEnding boundary)) o
{-|

{- |
Information about each of the parts of a @multipart/form-data@ form upload.
Build these with 'simplePart', 'filePart', or 'inputStreamPart'.
-}
data Part = Part
{ partFieldName :: FieldName
Expand All @@ -552,7 +552,7 @@ data Part = Part
, partDataHandler :: OutputStream Builder -> IO ()
}

{-|
{- |
Given a simple static set of bytes, send them as a part in a multipart form
upload. You need to specify the name of the field for the form, and optionally
can supply a MIME content-type.
Expand All @@ -565,7 +565,7 @@ simplePart name possibleContentType x' =
Streams.supply i2 o
in Part name possibleContentType Nothing action

{-|
{- |
The most common case in using multipart form data is to upload a file. Specify
the name of the field, optionally a MIME content-type, and then the path to
the file to be transmitted. The filename (without directory) will be used to
Expand All @@ -584,6 +584,19 @@ filePart name possibleContentType path =
filename = takeFileName path
in Part name possibleContentType (Just filename) action

{- |
Build a piece of a multipart submission from an 'InputStream'. You need to
specify a field name for this piece of the submission, and can optionally
indicate the MIME type and a filename (if what you are sending is going to be
interpreted as a file).
-}
inputStreamPart :: FieldName -> Maybe ContentType -> Maybe FilePath -> InputStream ByteString -> Part
inputStreamPart name possibleContentType possilbeFilename i1 =
let action o = do
i2 <- Streams.map Builder.fromByteString i1
Streams.supply i2 o
in Part name possibleContentType possilbeFilename action

{- |
Place content on the server at the given URL via an HTTP PUT
request, specifying the content type and a function to write the
Expand Down

0 comments on commit 24e61fd

Please sign in to comment.