stack-1.4.0: The Haskell Tool Stack

Safe Haskell | None
Language | Haskell2010

Network.HTTP.Download

Synopsis

- verifiedDownload :: (MonadIO m, MonadLogger m) => DownloadRequest -> Path Abs File -> (Maybe Integer -> Sink ByteString IO ()) -> m Bool
- data DownloadRequest = DownloadRequest {
  - drRequest :: Request
  - drHashChecks :: [HashCheck]
  - drLengthCheck :: Maybe LengthCheck
  - drRetryPolicy :: RetryPolicy
  }
- drRetryPolicyDefault :: RetryPolicy
- data HashCheck = (Show a, HashAlgorithm a) => HashCheck {
  - hashCheckAlgorithm :: a
  - hashCheckHexDigest :: CheckHexDigest
  }
- data DownloadException = RedownloadFailed Request (Path Abs File) (Response ())
- data CheckHexDigest
  - = CheckHexDigestString String
  - | CheckHexDigestByteString ByteString
  - | CheckHexDigestHeader ByteString
- type LengthCheck = Int
- data VerifiedDownloadException
  - = WrongContentLength Request Int ByteString
  - | WrongStreamLength Request Int Int
  - | WrongDigest Request String CheckHexDigest String
- download :: (MonadIO m, MonadLogger m) => Request -> Path Abs File -> m Bool
- redownload :: (MonadIO m, MonadLogger m) => Request -> Path Abs File -> m Bool
- httpJSON :: (MonadIO m, FromJSON a) => Request -> m (Response a)
- parseRequest :: MonadThrow m => String -> m Request
- parseUrlThrow :: MonadThrow m => String -> m Request
- setGithubHeaders :: Request -> Request

# Documentation

verifiedDownload #

Arguments

:: (MonadIO m, MonadLogger m) |
=> DownloadRequest |
-> Path Abs File | destination
-> (Maybe Integer -> Sink ByteString IO ()) | custom hook to observe progress
-> m Bool | Whether a download was performed

Copied and extended version of Network.HTTP.Download.download.

Has the following additional features: * Verifies that response content-length header (if present) matches expected length * Limits the download to (close to) the expected # of bytes * Verifies that the expected # bytes were downloaded (not too few) * Verifies md5 if response includes content-md5 header * Verifies the expected hashes

Throws VerifiedDownloadException. Throws IOExceptions related to file system operations. Throws HttpException.

data DownloadRequest #

A request together with some checks to perform.

Constructors

DownloadRequest |
Fields drRequest :: Request drHashChecks :: [HashCheck] drLengthCheck :: Maybe LengthCheck drRetryPolicy :: RetryPolicy

drRetryPolicyDefault :: RetryPolicy #

Default to retrying thrice with a short constant delay.

data HashCheck #

Constructors

(Show a, HashAlgorithm a) => HashCheck |
Fields hashCheckAlgorithm :: a hashCheckHexDigest :: CheckHexDigest

Instances

Show HashCheck # |
Methods showsPrec :: Int -> HashCheck -> ShowS # show :: HashCheck -> String # showList :: [HashCheck] -> ShowS #

data DownloadException #

Constructors

RedownloadFailed Request (Path Abs File) (Response ()) |

Instances

Show DownloadException # |
Methods showsPrec :: Int -> DownloadException -> ShowS # show :: DownloadException -> String # showList :: [DownloadException] -> ShowS #
Exception DownloadException # |
Methods toException :: DownloadException -> SomeException # fromException :: SomeException -> Maybe DownloadException # displayException :: DownloadException -> String #

data CheckHexDigest #

Constructors

CheckHexDigestString String |
CheckHexDigestByteString ByteString |
CheckHexDigestHeader ByteString |

Instances

Show CheckHexDigest # |
Methods showsPrec :: Int -> CheckHexDigest -> ShowS # show :: CheckHexDigest -> String # showList :: [CheckHexDigest] -> ShowS #
IsString CheckHexDigest # |
Methods fromString :: String -> CheckHexDigest #

type LengthCheck = Int #

data VerifiedDownloadException #

An exception regarding verification of a download.

Constructors

WrongContentLength Request Int ByteString |
WrongStreamLength Request Int Int |
WrongDigest Request String CheckHexDigest String |

Instances

Show VerifiedDownloadException # |
Methods showsPrec :: Int -> VerifiedDownloadException -> ShowS # show :: VerifiedDownloadException -> String # showList :: [VerifiedDownloadException] -> ShowS #
Exception VerifiedDownloadException # |
Methods toException :: VerifiedDownloadException -> SomeException # fromException :: SomeException -> Maybe VerifiedDownloadException # displayException :: VerifiedDownloadException -> String #

download #

Arguments

:: (MonadIO m, MonadLogger m) |
=> Request |
-> Path Abs File | destination
-> m Bool | Was a downloaded performed (True) or did the file already exist (False)?

Download the given URL to the given location. If the file already exists, no download is performed. Otherwise, creates the parent directory, downloads to a temporary file, and on file download completion moves to the appropriate destination.

Throws an exception if things go wrong

redownload #

Arguments

:: (MonadIO m, MonadLogger m) |
=> Request |
-> Path Abs File | destination
-> m Bool |

Same as `download`, but will download a file a second time if it is already present.

Returns `True` if the file was downloaded, `False` otherwise

httpJSON :: (MonadIO m, FromJSON a) => Request -> m (Response a) #

Perform an HTTP request and parse the body as JSON. In the event of an JSON parse errors, a `JSONException` runtime exception will be thrown.

Since: 2.1.10

parseRequest :: MonadThrow m => String -> m Request #

Convert a URL into a `Request`.

This defaults some of the values in `Request`, such as setting `method` to GET and `requestHeaders` to `[]`.

Since this function uses `MonadThrow`, the return monad can be anything that is an instance of `MonadThrow`, such as `IO` or `Maybe`.

You can place the request method at the beginning of the URL separated by a space, e.g.:

@@`parseRequeset "POST http://httpbin.org/post"` @@

Note that the request method must be provided as all capital letters.

`Request` created by this function won't cause exceptions on non-2XX response status codes.

Since: 0.4.30

parseUrlThrow :: MonadThrow m => String -> m Request #

Same as `parseRequest`, except will throw an `HttpException` in the event of a non-2XX response.

Since: 0.4.30

setGithubHeaders :: Request -> Request #

Set the user-agent request header