Safe Haskell | None |
---|---|
Language | Haskell2010 |
Functions for implementing the client side of JSON-RPC 2.0. See http://www.jsonrpc.org/specification.
- type Connection m = ByteString -> m (Maybe ByteString)
- type RpcResult m r = ExceptT RpcError m r
- data Signature ps r = Signature Text ps
- data p ::: ps = Text ::: ps
- toFunction :: (Monad m, ClientFunction ps r f, ComposeMultiParam (Batch r -> RpcResult m r) f g) => Connection m -> Signature ps r -> g
- toFunction_ :: (Monad m, ClientFunction ps r f, ComposeMultiParam (Batch r -> RpcResult m ()) f g) => Connection m -> Signature ps r -> g
- data Batch r
- toBatchFunction :: ClientFunction ps r f => Signature ps r -> f
- toBatchFunction_ :: (ClientFunction ps r f, ComposeMultiParam (Batch r -> Batch ()) f g) => Signature ps r -> g
- voidBatch :: Batch r -> Batch ()
- runBatch :: Monad m => Connection m -> Batch r -> RpcResult m r
- data RpcError :: * = RpcError {}
- clientCode :: Int
- class ClientFunction ps r f | ps r -> f, f -> ps r
- class ComposeMultiParam f g h | f g -> h, g h -> f
Summary
- Create one
Signature
for each server-side method to be called.Signature
s can be shared between client and server, usingtoServerMethod
. - Create a function of type
Monad m =>
for communicating with the server.Connection
m - Create client-side functions by calling
toFunction
,toFunction_
,toBatchFunction
, ortoBatchFunction_
on theSignature
s. - Client-side functions created with
toBatchFunction
ortoBatchFunction_
return values of type
. Combine them usingBatch
aBatch
'sApplicative
andAlternative
instances, before callingrunBatch
on the result.
Demo
The demo folder contains a client and server that communicate
using a shared set of Signature
s. The client runs the server as a
subprocess, sending requests to stdin and receiving responses from stdout.
Compile both programs with the demo
flag. Then run the client by passing
it a command to run the server (e.g., demo-client demo-server
).
Types
type Connection m = ByteString -> m (Maybe ByteString) Source
Function used to send requests to the server.
Nothing
represents no response, as when a JSON-RPC
server receives only notifications.
type RpcResult m r = ExceptT RpcError m r
Return type of a method. A method call can either fail with an RpcError
or succeed with a result of type r
.
Signatures
Signature specifying the name,
parameter names and types (ps
), and return type (r
) of a method.
A node in a linked list specifying parameter names and types. It is right associative.
Show ps => Show ((:::) p ps) Source | |
(ClientFunction ps r f, ToJSON a) => ClientFunction ((:::) a ps) r (a -> f) Source | |
ConvertParams ps1 ps2 => ConvertParams ((:::) p ps1) ((:+:) p ps2) Source |
Single Requests
:: (Monad m, ClientFunction ps r f, ComposeMultiParam (Batch r -> RpcResult m r) f g) | |
=> Connection m | Function for sending requests to the server. |
-> Signature ps r | Method signature. |
-> g | Client-side function with a return type of |
Creates a function for calling a JSON-RPC method on the server.
:: (Monad m, ClientFunction ps r f, ComposeMultiParam (Batch r -> RpcResult m ()) f g) | |
=> Connection m | Function for sending requests to the server. |
-> Signature ps r | Method signature. |
-> g | Client-side function with a return type of |
Creates a function for calling a JSON-RPC method on the server as a notification.
Batch Requests
A batch call. Batch multiple requests by combining
values of this type using its Applicative
and Alternative
instances before running them with runBatch
.
Functor Batch Source | |
Applicative Batch Source | |
Alternative Batch Source | |
FromJSON r => ClientFunction () r (Batch r) Source | |
ComposeMultiParam (Batch a -> b) (Batch a) b Source |
:: ClientFunction ps r f | |
=> Signature ps r | Method signature. |
-> f | Client-side function with a return type of |
Creates a function for calling a JSON-RPC method as part of a batch request.
:: (ClientFunction ps r f, ComposeMultiParam (Batch r -> Batch ()) f g) | |
=> Signature ps r | Method signature. |
-> g | Client-side function with a return type of |
Creates a function for calling a JSON-RPC method as a notification and as part of a batch request.
:: Monad m | |
=> Connection m | Function for sending requests to the server. |
-> Batch r | Batch to be evaluated. |
-> RpcResult m r | Result. |
Evaluates a batch. The process depends on its size:
- If the batch is empty, the server function is not called.
- If the batch has exactly one request, it is sent as a request object.
- If the batch has multiple requests, they are sent as an array of request objects.
Errors
RpcError
is used for all server-side errors, as described in the
specification. Additionally, the error code -31999
is used for any
errors that occur while parsing the server's response.
data RpcError :: *
JSON-RPC error.
clientCode :: Int Source
Code used for all client-side errors. It is -31999.
Type Classes
class ClientFunction ps r f | ps r -> f, f -> ps r Source
Relationship between the parameters (ps
), return type (r
),
and client-side batch function (f
) of a JSON-RPC method.
FromJSON r => ClientFunction () r (Batch r) Source | |
(ClientFunction ps r f, ToJSON a) => ClientFunction ((:::) a ps) r (a -> f) Source |
class ComposeMultiParam f g h | f g -> h, g h -> f Source
Relationship between a function (g
) taking any number of arguments and yielding a
,
a function (Batch
af
) taking a
, and the function (Batch
ah
) that applies g to all of its
arguments and then applies f to the result.
ComposeMultiParam f g h => ComposeMultiParam f (a -> g) (a -> h) Source | |
ComposeMultiParam (Batch a -> b) (Batch a) b Source |