http2-client-grpc 0.5.0.0 → 0.5.0.1
raw patch · 2 files changed
+42/−2 lines, 2 filesdep +lensPVP: minor bump suggested
API additions: PVP suggests at least a minor version bump
Dependencies added: lens
API changes (from Hackage documentation)
+ Network.GRPC.Client.Helpers: close :: GrpcClient -> IO ()
+ Network.GRPC.Client.Helpers: unaryOutput :: (Applicative f, Field3 a1 b1 (Either c1 a2) (Either c1 b2)) => (a2 -> f b2) -> Either c2 (Either c3 a1) -> f (Either c2 (Either c3 b1))
Files
http2-client-grpc.cabal view
@@ -1,5 +1,5 @@ name: http2-client-grpc-version: 0.5.0.0+version: 0.5.0.1 synopsis: Implement gRPC-over-HTTP2 clients. description: Uses http2-client and proto-lens to generate client code. homepage: https://github.com/lucasdicioccio/http2-client-grpc#readme@@ -23,6 +23,7 @@ , bytestring , case-insensitive , data-default-class+ , lens , http2 , http2-client , http2-grpc-types >= 0.3
src/Network/GRPC/Client/Helpers.hs view
@@ -1,6 +1,7 @@ {-# LANGUAGE DataKinds #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE GADTs #-}+{-# LANGUAGE FlexibleContexts #-} -- | Set of helpers helping with writing gRPC clients with not much exposure of -- the http2-client complexity.@@ -13,7 +14,8 @@ -- are not planned in this library and should be added at higher-levels. module Network.GRPC.Client.Helpers where -import Control.Concurrent.Async (Async, async)+import Control.Lens+import Control.Concurrent.Async (Async, async, cancel) import Control.Concurrent (threadDelay) import Control.Exception (throwIO) import Control.Monad (forever)@@ -122,34 +124,71 @@ let tasks = BackgroundTasks wuAsync pingAsync return $ GrpcClient cli authority headers timeout compression tasks +-- | Cancels background tasks and closes the underlying HTTP2 client.+close :: GrpcClient -> IO ()+close grpc = do+ cancel $ backgroundPing $ _grpcClientBackground grpc+ cancel $ backgroundWindowUpdate $ _grpcClientBackground grpc+ _close $ _grpcClientHttp2Client grpc++-- | Run an unary query. rawUnary :: (Service s, HasMethod s m) => RPC s m+ -- ^ The RPC to call. -> GrpcClient+ -- ^ An initialized client. -> MethodInput s m+ -- ^ The input. -> IO (Either TooMuchConcurrency (RawReply (MethodOutput s m))) rawUnary rpc (GrpcClient client authority headers timeout compression _) input = let call = singleRequest rpc input in open client authority headers timeout (Encoding compression) (Decoding compression) call +-- | Prism helper to unpack an unary gRPC call output.+--+-- @ out <- rawUnary rpc grpc method+-- print $ out ^? unaryOutput . somefield+-- @+unaryOutput+ :: (Applicative f, Field3 a1 b1 (Either c1 a2) (Either c1 b2)) =>+ (a2 -> f b2)+ -> Either c2 (Either c3 a1) -> f (Either c2 (Either c3 b1))+unaryOutput = _Right . _Right . _3 . _Right++-- | Calls for a server stream of requests. rawStreamServer :: (Service s, HasMethod s m, MethodStreamingType s m ~ 'ServerStreaming) => RPC s m+ -- ^ The RPC to call. -> GrpcClient+ -- ^ An initialized client. -> a+ -- ^ An initial state. -> MethodInput s m+ -- ^ The input of the stream request. -> (a -> HeaderList -> MethodOutput s m -> IO a)+ -- ^ A state-passing handler called for each server-sent output.+ -- Headers are repeated for convenience but are the same for every iteration. -> IO (Either TooMuchConcurrency (a, HeaderList, HeaderList)) rawStreamServer rpc (GrpcClient client authority headers timeout compression _) v0 input handler = let call = streamReply rpc v0 input handler in open client authority headers timeout (Encoding compression) (Decoding compression) call +-- | Sends a streams of requests to the server.+--+-- Messages are submitted to the HTTP2 underlying client and hence this+-- function can block until the HTTP2 client has some network credit. rawStreamClient :: (Service s, HasMethod s m, MethodStreamingType s m ~ 'ClientStreaming) => RPC s m+ -- ^ The RPC to call. -> GrpcClient+ -- ^ An initialized client. -> a+ -- ^ An initial state. -> (a -> IO (a, Either StreamDone (CompressMode, MethodInput s m)))+ -- ^ A state-passing step function to decide the next message. -> IO (Either TooMuchConcurrency (a, (RawReply (MethodOutput s m)))) rawStreamClient rpc (GrpcClient client authority headers timeout compression _) v0 getNext = let call = streamRequest rpc v0 getNext