stack-2.5.1.1: The Haskell Tool Stack
Safe HaskellNone
LanguageHaskell2010

Network.HTTP.StackClient

Description

Wrapper functions of Simple and Client to add the 'User-Agent' HTTP request header to each request.

Synopsis

Documentation

withResponse :: (MonadUnliftIO m, MonadIO n) => Request -> (Response (ConduitM i ByteString n ()) -> m a) -> m a Source #

setRequestMethod :: ByteString -> Request -> Request #

Set the request method

Since: http-conduit-2.1.10

setRequestHeader :: HeaderName -> [ByteString] -> Request -> Request #

Set the given request header to the given list of values. Removes any previously set header values with the same name.

Since: http-conduit-2.1.10

addRequestHeader :: HeaderName -> ByteString -> Request -> Request #

Add a request header name/value combination

Since: http-conduit-2.1.10

setRequestBody :: RequestBody -> Request -> Request #

Set the request body to the given RequestBody. You may want to consider using one of the convenience functions in the modules, e.g. requestBodyJSON.

Note: This will not modify the request method. For that, please use requestMethod. You likely don't want the default of GET.

Since: http-conduit-2.1.10

getResponseHeaders :: Response a -> [(HeaderName, ByteString)] #

Get all response headers

Since: http-conduit-2.1.10

getResponseBody :: Response a -> a #

Get the response body

Since: http-conduit-2.1.10

getResponseStatusCode :: Response a -> Int #

Get the integral status code of the response

Since: http-conduit-2.1.10

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

Convert a URL into a Request.

This function 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.:

@@ parseRequest "POST http://httpbin.org/post" @@

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

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

To create a request which throws on non-2XX status codes, see parseUrlThrow

Since: http-client-0.4.30

getUri :: Request -> URI #

Extract a URI from the request.

Since 0.1.0

path :: Request -> ByteString #

Everything from the host to the query string.

Since 0.1.0

checkResponse :: Request -> Request -> Response BodyReader -> IO () #

Check the response immediately after receiving the status and headers. This can be useful for throwing exceptions on non-success status codes.

In previous versions of http-client, this went under the name checkStatus, but was renamed to avoid confusion about the new default behavior (doing nothing).

Since: http-client-0.5.0

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

Same as parseRequest, except will throw an HttpException in the event of a non-2XX response. This uses throwErrorStatusCodes to implement checkResponse.

Since: http-client-0.4.30

requestHeaders :: Request -> RequestHeaders #

Custom HTTP request headers

The Content-Length and Transfer-Encoding headers are set automatically by this module, and shall not be added to requestHeaders.

If not provided by the user, Host will automatically be set based on the host and port fields.

Moreover, the Accept-Encoding header is set implicitly to gzip for convenience by default. This behaviour can be overridden if needed, by setting the header explicitly to a different value. In order to omit the Accept-Header altogether, set it to the empty string "". If you need an empty Accept-Header (i.e. requesting the identity encoding), set it to a non-empty white-space string, e.g. " ". See RFC 2616 section 14.3 for details about the semantics of the Accept-Header field. If you request a content-encoding not supported by this module, you will have to decode it yourself (see also the decompress field).

Note: Multiple header fields with the same field-name will result in multiple header fields being sent and therefore it's the responsibility of the client code to ensure that the rules from RFC 2616 section 4.2 are honoured.

Since 0.1.0

getGlobalManager :: IO Manager #

Get the current global Manager

Since: http-client-tls-0.2.4

applyDigestAuth #

Arguments

:: (MonadIO m, MonadThrow n) 
=> ByteString

username

-> ByteString

password

-> Request 
-> Manager 
-> m (n Request) 

Apply digest authentication to this request.

Note that this function will need to make an HTTP request to the server in order to get the nonce, thus the need for a Manager and to live in IO. This also means that the request body will be sent to the server. If the request body in the supplied Request can only be read once, you should replace it with a dummy value.

In the event of successfully generating a digest, this will return a Just value. If there is any problem with generating the digest, it will return Nothing.

Since: http-client-tls-0.3.1

displayDigestAuthException :: DigestAuthException -> String #

User friendly display of a DigestAuthException

Since: http-client-tls-0.3.3

data Request #

All information on how to connect to a host and what should be sent in the HTTP request.

If you simply wish to download from a URL, see parseRequest.

The constructor for this data type is not exposed. Instead, you should use either the defaultRequest value, or parseRequest to construct from a URL, and then use the records below to make modifications. This approach allows http-client to add configuration options without breaking backwards compatibility.

For example, to construct a POST request, you could do something like:

initReq <- parseRequest "http://www.example.com/path"
let req = initReq
            { method = "POST"
            }

For more information, please see http://www.yesodweb.com/book/settings-types.

Since 0.1.0

Instances

Instances details
Show Request 
Instance details

Defined in Network.HTTP.Client.Types

data RequestBody #

When using one of the RequestBodyStream / RequestBodyStreamChunked constructors, you must ensure that the GivesPopper can be called multiple times. Usually this is not a problem.

The RequestBodyStreamChunked will send a chunked request body. Note that not all servers support this. Only use RequestBodyStreamChunked if you know the server you're sending to supports chunked request bodies.

Since 0.1.0

data Response body #

A simple representation of the HTTP response.

Since 0.1.0

Instances

Instances details
Functor Response 
Instance details

Defined in Network.HTTP.Client.Types

Methods

fmap :: (a -> b) -> Response a -> Response b #

(<$) :: a -> Response b -> Response a #

Foldable Response 
Instance details

Defined in Network.HTTP.Client.Types

Methods

fold :: Monoid m => Response m -> m #

foldMap :: Monoid m => (a -> m) -> Response a -> m #

foldMap' :: Monoid m => (a -> m) -> Response a -> m #

foldr :: (a -> b -> b) -> b -> Response a -> b #

foldr' :: (a -> b -> b) -> b -> Response a -> b #

foldl :: (b -> a -> b) -> b -> Response a -> b #

foldl' :: (b -> a -> b) -> b -> Response a -> b #

foldr1 :: (a -> a -> a) -> Response a -> a #

foldl1 :: (a -> a -> a) -> Response a -> a #

toList :: Response a -> [a] #

null :: Response a -> Bool #

length :: Response a -> Int #

elem :: Eq a => a -> Response a -> Bool #

maximum :: Ord a => Response a -> a #

minimum :: Ord a => Response a -> a #

sum :: Num a => Response a -> a #

product :: Num a => Response a -> a #

Traversable Response 
Instance details

Defined in Network.HTTP.Client.Types

Methods

traverse :: Applicative f => (a -> f b) -> Response a -> f (Response b) #

sequenceA :: Applicative f => Response (f a) -> f (Response a) #

mapM :: Monad m => (a -> m b) -> Response a -> m (Response b) #

sequence :: Monad m => Response (m a) -> m (Response a) #

Show body => Show (Response body) 
Instance details

Defined in Network.HTTP.Client.Types

Methods

showsPrec :: Int -> Response body -> ShowS #

show :: Response body -> String #

showList :: [Response body] -> ShowS #

data HttpException #

An exception which may be generated by this library

Since: http-client-0.5.0

methodPut :: Method #

HTTP Method constants.

formDataBody :: MonadIO m => [Part] -> Request -> m Request #

Add form data to the Request.

This sets a new requestBody, adds a content-type request header and changes the method to POST.

partFileRequestBody #

Arguments

:: forall (m :: Type -> Type). Applicative m 
=> Text

Name of the corresponding <input>.

-> FilePath

File name to supply to the server.

-> RequestBody

Data to upload.

-> PartM m 

Construct a Part from form name, filepath and a RequestBody

partFileRequestBody "who_calls" "caller.json" $ RequestBodyBS "{\"caller\":\"Jason J Jason\"}"
-- empty upload form
partFileRequestBody "file" mempty mempty

The Part does not have a content type associated with it.

partBS #

Arguments

:: forall (m :: Type -> Type). Applicative m 
=> Text

Name of the corresponding <input>.

-> ByteString

The body for this Part.

-> PartM m 

Make a Part whose content is a strict ByteString.

The Part does not have a file name or content type associated with it.

partLBS #

Arguments

:: forall (m :: Type -> Type). Applicative m 
=> Text

Name of the corresponding <input>.

-> ByteString

The body for this Part.

-> PartM m 

Make a Part whose content is a lazy ByteString.

The Part does not have a file name or content type associated with it.

setGithubHeaders :: Request -> Request Source #

Set the user-agent request header

download Source #

Arguments

:: HasTerm env 
=> Request 
-> Path Abs File

destination

-> RIO env 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 Source #

Arguments

:: HasTerm env 
=> Request 
-> Path Abs File

destination

-> RIO env 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

verifiedDownload Source #

Arguments

:: HasTerm env 
=> DownloadRequest 
-> Path Abs File

destination

-> (Maybe Integer -> ConduitM ByteString Void (RIO env) ())

custom hook to observe progress

-> RIO env 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.

Construct using the downloadRequest smart constructor and associated setters. The constructor itself is not exposed to avoid breaking changes with additional fields.

Since: http-download-0.2.0.0

drRetryPolicyDefault :: RetryPolicy #

Default to retrying seven times with exponential backoff starting from one hundred milliseconds.

This means the tries will occur after these delays if necessary:

  • 0.1s
  • 0.2s
  • 0.4s
  • 0.8s
  • 1.6s
  • 3.2s
  • 6.4s

data HashCheck #

Instances

Instances details
Show HashCheck 
Instance details

Defined in Network.HTTP.Download.Verified

mkDownloadRequest :: Request -> DownloadRequest #

Construct a new DownloadRequest from the given Request. Use associated setters to modify the value further.

Since: http-download-0.2.0.0

setHashChecks :: [HashCheck] -> DownloadRequest -> DownloadRequest #

Set the hash checks to be run when verifying.

Since: http-download-0.2.0.0

setLengthCheck :: Maybe LengthCheck -> DownloadRequest -> DownloadRequest #

Set the length check to be run when verifying.

Since: http-download-0.2.0.0

setRetryPolicy :: RetryPolicy -> DownloadRequest -> DownloadRequest #

Set the retry policy to be used when downloading.

Since: http-download-0.2.0.0

setForceDownload :: Bool -> DownloadRequest -> DownloadRequest #

If True, force download even if the file already exists. Useful for download a resource which may change over time.