Safe Haskell | None |
---|---|
Language | Haskell2010 |
Synopsis
- data Http c :: Effect
- request :: forall c r. Member (Http c) r => Request -> Sem r (Either HttpError (Response LByteString))
- stream :: forall c r a. Member (Http c) r => Request -> (Response c -> Sem r (Either HttpError a)) -> Sem r (Either HttpError a)
- interpretHttpNative :: Members [Embed IO, Log, Resource] r => InterpreterFor (Http BodyReader) r
- interpretHttpStrict :: Member (Embed IO) r => [Response LByteString] -> [ByteString] -> InterpreterFor (Http Int) r
- data Method
- newtype Host = Host {}
- newtype Port = Port {}
- newtype Tls = Tls {}
- newtype Path = Path {}
- newtype QueryKey = QueryKey {
- unQueryKey :: Text
- newtype QueryValue = QueryValue {
- unQueryValue :: Text
- data Request = Request {}
- newtype Body = Body {}
- data Response b = Response {}
- pattern Server :: Status -> b -> [Header] -> Response b
- pattern Client :: Status -> b -> [Header] -> Response b
- pattern Redirect :: Status -> b -> [Header] -> Response b
- pattern Success :: Status -> b -> [Header] -> Response b
- pattern Info :: Status -> b -> [Header] -> Response b
- newtype HeaderName = HeaderName {
- unHeaderName :: Text
- newtype HeaderValue = HeaderValue {}
- data Header = Header {
- name :: HeaderName
- value :: HeaderValue
- get :: Host -> Path -> Request
- post :: Host -> Path -> Body -> Request
- put :: Host -> Path -> Body -> Request
- delete :: Host -> Path -> Request
- getUrl :: Text -> Either Text Request
- postUrl :: Body -> Text -> Either Text Request
- putUrl :: Body -> Text -> Either Text Request
- deleteUrl :: Text -> Either Text Request
- data HttpError
- streamResponse :: Members [Http c, Error HttpError, Resource] r => Request -> (forall x. StreamEvent o c h x -> Sem r x) -> Sem r o
- data StreamEvent r c h a where
- Acquire :: Response c -> StreamEvent r c h h
- Chunk :: h -> StreamChunk -> StreamEvent r c h ()
- Result :: Response c -> h -> StreamEvent r c h r
- Release :: h -> StreamEvent r c h ()
- data EntityDecode d :: Effect
- decode :: forall d r. Member (EntityDecode d) r => LByteString -> Sem r (Either EntityError d)
- decodeStrict :: forall d r. Member (EntityDecode d) r => ByteString -> Sem r (Either EntityError d)
- data EntityEncode d :: Effect
- encode :: forall d r. Member (EntityEncode d) r => d -> Sem r LByteString
- encodeStrict :: forall d r. Member (EntityEncode d) r => d -> Sem r ByteString
- type family Entities es r :: Constraint where ...
- data Decode a
- data Encode a
- type family Decoders ds r :: Constraint where ...
- type family Encoders es r :: Constraint where ...
- data EntityError = EntityError Text Text
- interpretEntityEncodeAeson :: ToJSON d => Sem (EntityEncode d ': r) a -> Sem r a
- interpretEntityDecodeAeson :: FromJSON d => Sem (EntityDecode d ': r) a -> Sem r a
- data Manager :: Effect
- data Log :: Effect
- interpretLogNull :: InterpreterFor Log r
- interpretLogStdout :: Member (Embed IO) r => Sem (Log ': r) a -> Sem r a
Documentation
A basic Polysemy
effect abstracting HTTP requests:
import Polysemy (resourceToIO, runM) import qualified Polysemy.Http as Http import Polysemy.Http (interpretHttpNative, interpretLogStdout) main :: IO () main = do result <- runM $ resourceToIO $ interpretLogStdout $ interpretHttpNative $ Http.request (Http.get "hackage.haskell.org" "package/polysemy-http") print result
data Http c :: Effect Source #
The main effect for HTTP requests.
The parameter c
determines the representation of raw chunks.
Instances
type DefiningModule Http Source # | |
Defined in Polysemy.Http.Data.Http |
request :: forall c r. Member (Http c) r => Request -> Sem r (Either HttpError (Response LByteString)) Source #
Synchronously run an HTTP request and return the response.
stream :: forall c r a. Member (Http c) r => Request -> (Response c -> Sem r (Either HttpError a)) -> Sem r (Either HttpError a) Source #
Open a connection without consuming data and pass the response to a handler for custom transmission. The intended purpose is to allow streaming transfers.
Interpreters
interpretHttpNative :: Members [Embed IO, Log, Resource] r => InterpreterFor (Http BodyReader) r Source #
Interpret
using the native Http
BodyReader
Client
implementation.
BodyReader
is an alias for
, it is how http-client represents chunks.
This uses the default interpreter for IO
ByteString
Manager
.
interpretHttpStrict :: Member (Embed IO) r => [Response LByteString] -> [ByteString] -> InterpreterFor (Http Int) r Source #
Request and Response
All standard HTTP methods, mirroring those from Types
, plus a constructor for arbitrary strings.
Instances
Eq Method Source # | |
Show Method Source # | |
IsString Method Source # | |
Defined in Polysemy.Http.Data.Request fromString :: String -> Method # | |
FromJSON Method Source # | |
Defined in Polysemy.Http.Data.Request parseJSON :: Value -> Parser Method parseJSONList :: Value -> Parser [Method] | |
ToJSON Method Source # | |
Defined in Polysemy.Http.Data.Request toEncoding :: Method -> Encoding toJSONList :: [Method] -> Value toEncodingList :: [Method] -> Encoding |
Request host name.
Instances
Eq Host Source # | |
Show Host Source # | |
IsString Host Source # | |
Defined in Polysemy.Http.Data.Request fromString :: String -> Host # | |
Generic Host Source # | |
FromJSON Host Source # | |
Defined in Polysemy.Http.Data.Request parseJSON :: Value -> Parser Host parseJSONList :: Value -> Parser [Host] | |
ToJSON Host Source # | |
Defined in Polysemy.Http.Data.Request | |
type Rep Host Source # | |
Defined in Polysemy.Http.Data.Request |
Request port.
Instances
Eq Port Source # | |
Show Port Source # | |
Generic Port Source # | |
FromJSON Port Source # | |
Defined in Polysemy.Http.Data.Request parseJSON :: Value -> Parser Port parseJSONList :: Value -> Parser [Port] | |
ToJSON Port Source # | |
Defined in Polysemy.Http.Data.Request | |
type Rep Port Source # | |
Defined in Polysemy.Http.Data.Request |
A flag that indicates whether a request should use TLS.
Instances
Eq Tls Source # | |
Show Tls Source # | |
Generic Tls Source # | |
FromJSON Tls Source # | |
Defined in Polysemy.Http.Data.Request parseJSON :: Value -> Parser Tls parseJSONList :: Value -> Parser [Tls] | |
ToJSON Tls Source # | |
Defined in Polysemy.Http.Data.Request | |
type Rep Tls Source # | |
Defined in Polysemy.Http.Data.Request |
Rrequest path.
Instances
Eq Path Source # | |
Show Path Source # | |
IsString Path Source # | |
Defined in Polysemy.Http.Data.Request fromString :: String -> Path # | |
Generic Path Source # | |
Semigroup Path Source # | |
Monoid Path Source # | |
FromJSON Path Source # | |
Defined in Polysemy.Http.Data.Request parseJSON :: Value -> Parser Path parseJSONList :: Value -> Parser [Path] | |
ToJSON Path Source # | |
Defined in Polysemy.Http.Data.Request | |
type Rep Path Source # | |
Defined in Polysemy.Http.Data.Request |
The key of a query parameter.
Instances
Eq QueryKey Source # | |
Show QueryKey Source # | |
IsString QueryKey Source # | |
Defined in Polysemy.Http.Data.Request fromString :: String -> QueryKey # | |
Generic QueryKey Source # | |
FromJSON QueryKey Source # | |
Defined in Polysemy.Http.Data.Request parseJSON :: Value -> Parser QueryKey parseJSONList :: Value -> Parser [QueryKey] | |
ToJSON QueryKey Source # | |
Defined in Polysemy.Http.Data.Request toEncoding :: QueryKey -> Encoding toJSONList :: [QueryKey] -> Value toEncodingList :: [QueryKey] -> Encoding | |
type Rep QueryKey Source # | |
Defined in Polysemy.Http.Data.Request |
newtype QueryValue Source #
The value of a query parameter.
Instances
HTTP request parameters, used by Http
.
Instances
Request body, using LByteString
because it is what encode
produces.
Instances
Eq Body Source # | |
Show Body Source # | |
IsString Body Source # | |
Defined in Polysemy.Http.Data.Request fromString :: String -> Body # | |
Generic Body Source # | |
type Rep Body Source # | |
Defined in Polysemy.Http.Data.Request type Rep Body = D1 ('MetaData "Body" "Polysemy.Http.Data.Request" "polysemy-http-0.2.0.0-inplace" 'True) (C1 ('MetaCons "Body" 'PrefixI 'True) (S1 ('MetaSel ('Just "unBody") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 LByteString))) |
The response produced by Http
.
pattern Server :: Status -> b -> [Header] -> Response b Source #
Match on a response with a 5xx status.
pattern Client :: Status -> b -> [Header] -> Response b Source #
Match on a response with a 4xx status.
pattern Redirect :: Status -> b -> [Header] -> Response b Source #
Match on a response with a 3xx status.
pattern Success :: Status -> b -> [Header] -> Response b Source #
Match on a response with a 2xx status.
pattern Info :: Status -> b -> [Header] -> Response b Source #
Match on a response with a 1xx status.
newtype HeaderName Source #
The name of a header.
Instances
newtype HeaderValue Source #
The value of a header.
Instances
An HTTP header.
Header | |
|
Instances
Eq Header Source # | |
Show Header Source # | |
Generic Header Source # | |
FromJSON Header Source # | |
Defined in Polysemy.Http.Data.Header parseJSON :: Value -> Parser Header parseJSONList :: Value -> Parser [Header] | |
ToJSON Header Source # | |
Defined in Polysemy.Http.Data.Header toEncoding :: Header -> Encoding toJSONList :: [Header] -> Value toEncodingList :: [Header] -> Encoding | |
type Rep Header Source # | |
Defined in Polysemy.Http.Data.Header type Rep Header = D1 ('MetaData "Header" "Polysemy.Http.Data.Header" "polysemy-http-0.2.0.0-inplace" 'False) (C1 ('MetaCons "Header" 'PrefixI 'True) (S1 ('MetaSel ('Just "name") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 HeaderName) :*: S1 ('MetaSel ('Just "value") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 HeaderValue))) |
Indicates a critical error caused by an exception in the http-client backend.
Streaming
streamResponse :: Members [Http c, Error HttpError, Resource] r => Request -> (forall x. StreamEvent o c h x -> Sem r x) -> Sem r o Source #
Initiate a request and stream the response, calling process
after connecting, for every chunk, after closing the
connection, and for the return value.
StreamEvent
is used to indicate the stage of the request cycle.
handle :: StreamEvent Double (IO ByteString) Int a -> Sem r a handle = \case StreamEvent.Acquire (Response status body headers) -> pure 1 StreamEvent.Chunk handle (StreamChunk c) -> pure () StreamEvent.Result (Response status body headers) handle -> pure 5.5 StreamEvent.Release handle -> pure ()
>>>
runInterpreters $ streamResponse (Http.get "host.com" "path/to/file") handle
5.5
data StreamEvent r c h a where Source #
Control algebra for streaming requests.
r
is the final return type of the stream handler, after the request is processed to completion.
c
is the raw chunk data type
h
is the handle type that identifies the active request. It is entirely controlled by the consumer and may be
empty.
Acquire :: Response c -> StreamEvent r c h h | Used when calling the handler after the request has been initiated, but no data has been read. |
Chunk :: h -> StreamChunk -> StreamEvent r c h () | Used when calling the handler for each received chunk. |
Result :: Response c -> h -> StreamEvent r c h r | Used when calling the handler after the request has finished transferring. It should return the final result. |
Release :: h -> StreamEvent r c h () | Used to finalize the transfer, e.g. for resource cleanup. |
Entity
data EntityDecode d :: Effect Source #
Abstraction of json decoding, potentially usable for other content types like xml.
Instances
type DefiningModule EntityDecode Source # | |
Defined in Polysemy.Http.Data.Entity |
decode :: forall d r. Member (EntityDecode d) r => LByteString -> Sem r (Either EntityError d) Source #
Lazily decode a LByteString
to a value of type d
decodeStrict :: forall d r. Member (EntityDecode d) r => ByteString -> Sem r (Either EntityError d) Source #
Strictly decode a ByteString
to a value of type d
data EntityEncode d :: Effect Source #
Abstraction of json encoding, potentially usable for other content types like xml.
Instances
type DefiningModule EntityEncode Source # | |
Defined in Polysemy.Http.Data.Entity |
encode :: forall d r. Member (EntityEncode d) r => d -> Sem r LByteString Source #
Lazily encode a value of type d
to a LByteString
encodeStrict :: forall d r. Member (EntityEncode d) r => d -> Sem r ByteString Source #
Strictly encode a value of type d
to a ByteString
type family Entities es r :: Constraint where ... Source #
Convenience constraint for requiring multiple entity effects, to be used like Members
.
foo :: Entities [Encode Int, Decode Double] r => Sem r ()
type family Decoders ds r :: Constraint where ... Source #
Convenience constraint for requiring multiple decoders.
foo :: Decoders [Int, Double] r => Sem r ()
Decoders '[] r = () | |
Decoders (d ': ds) r = (Member (EntityDecode d) r, Decoders ds r) |
type family Encoders es r :: Constraint where ... Source #
Convenience constraint for requiring multiple encoders.
foo :: Encoders [Int, Double] r => Sem r ()
Encoders '[] r = () | |
Encoders (d ': ds) r = (Member (EntityEncode d) r, Encoders ds r) |
data EntityError Source #
Generic error type for decoders.
Instances
Eq EntityError Source # | |
Defined in Polysemy.Http.Data.Entity (==) :: EntityError -> EntityError -> Bool # (/=) :: EntityError -> EntityError -> Bool # | |
Show EntityError Source # | |
Defined in Polysemy.Http.Data.Entity showsPrec :: Int -> EntityError -> ShowS # show :: EntityError -> String # showList :: [EntityError] -> ShowS # |
interpretEntityEncodeAeson :: ToJSON d => Sem (EntityEncode d ': r) a -> Sem r a Source #
Interpreter for EntityEncode
that uses Aeson.
interpretEntityDecodeAeson :: FromJSON d => Sem (EntityDecode d ': r) a -> Sem r a Source #
Interpreter for EntityDecode
that uses Aeson.
Utilities
Connection Pool
data Manager :: Effect Source #
This effect abstracts the creation of a Manager
in order to allow pool sharing in a flexible way.
Instances
type DefiningModule Manager Source # | |
Defined in Polysemy.Http.Data.Manager |
Logging
An effect that wraps Log
for less boilerplate.
Constructors are manual because HasCallStack
is always in scope.
interpretLogNull :: InterpreterFor Log r Source #
No-op interpreter for Log