{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module JSONRPC (
RequestId (..),
IsJSONRPCRequest (..),
IsJSONRPCNotification (..),
ViaJSONRPCRequest (..),
ViaJSONRPCNotification (..),
JSONRPCRequest (..),
JSONRPCResponse (..),
JSONRPCError (..),
JSONRPCNotification (..),
JSONRPCMessage (..),
JSONRPCErrorInfo (..),
EmptyParams,
pARSE_ERROR,
iNVALID_REQUEST,
mETHOD_NOT_FOUND,
iNVALID_PARAMS,
iNTERNAL_ERROR,
rPC_VERSION,
) where
import Control.Applicative ((<|>))
import Data.Aeson
import Data.Aeson qualified as Aeson
import Data.Data (Proxy (..), Typeable, typeRep)
import Data.Kind (Type)
import Data.Text (Text)
import GHC.Generics
import GHC.Records (HasField (getField))
import GHC.TypeError (ErrorMessage (..), TypeError)
newtype RequestId = RequestId Value
deriving stock (Int -> RequestId -> ShowS
[RequestId] -> ShowS
RequestId -> String
(Int -> RequestId -> ShowS)
-> (RequestId -> String)
-> ([RequestId] -> ShowS)
-> Show RequestId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> RequestId -> ShowS
showsPrec :: Int -> RequestId -> ShowS
$cshow :: RequestId -> String
show :: RequestId -> String
$cshowList :: [RequestId] -> ShowS
showList :: [RequestId] -> ShowS
Show, RequestId -> RequestId -> Bool
(RequestId -> RequestId -> Bool)
-> (RequestId -> RequestId -> Bool) -> Eq RequestId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: RequestId -> RequestId -> Bool
== :: RequestId -> RequestId -> Bool
$c/= :: RequestId -> RequestId -> Bool
/= :: RequestId -> RequestId -> Bool
Eq)
deriving newtype ([RequestId] -> Value
[RequestId] -> Encoding
RequestId -> Bool
RequestId -> Value
RequestId -> Encoding
(RequestId -> Value)
-> (RequestId -> Encoding)
-> ([RequestId] -> Value)
-> ([RequestId] -> Encoding)
-> (RequestId -> Bool)
-> ToJSON RequestId
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: RequestId -> Value
toJSON :: RequestId -> Value
$ctoEncoding :: RequestId -> Encoding
toEncoding :: RequestId -> Encoding
$ctoJSONList :: [RequestId] -> Value
toJSONList :: [RequestId] -> Value
$ctoEncodingList :: [RequestId] -> Encoding
toEncodingList :: [RequestId] -> Encoding
$comitField :: RequestId -> Bool
omitField :: RequestId -> Bool
ToJSON, Maybe RequestId
Value -> Parser [RequestId]
Value -> Parser RequestId
(Value -> Parser RequestId)
-> (Value -> Parser [RequestId])
-> Maybe RequestId
-> FromJSON RequestId
forall a.
(Value -> Parser a)
-> (Value -> Parser [a]) -> Maybe a -> FromJSON a
$cparseJSON :: Value -> Parser RequestId
parseJSON :: Value -> Parser RequestId
$cparseJSONList :: Value -> Parser [RequestId]
parseJSONList :: Value -> Parser [RequestId]
$comittedField :: Maybe RequestId
omittedField :: Maybe RequestId
FromJSON)
rPC_VERSION :: Text
rPC_VERSION :: Text
rPC_VERSION = Text
"2.0"
data EmptyParams
deriving stock ((forall x. EmptyParams -> Rep EmptyParams x)
-> (forall x. Rep EmptyParams x -> EmptyParams)
-> Generic EmptyParams
forall x. Rep EmptyParams x -> EmptyParams
forall x. EmptyParams -> Rep EmptyParams x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. EmptyParams -> Rep EmptyParams x
from :: forall x. EmptyParams -> Rep EmptyParams x
$cto :: forall x. Rep EmptyParams x -> EmptyParams
to :: forall x. Rep EmptyParams x -> EmptyParams
Generic)
instance Show EmptyParams where
show :: EmptyParams -> String
show :: EmptyParams -> String
show EmptyParams
_ = Value -> String
forall a. Show a => a -> String
show Value
Aeson.Null
instance Eq EmptyParams where
(==) :: EmptyParams -> EmptyParams -> Bool
EmptyParams
_ == :: EmptyParams -> EmptyParams -> Bool
== EmptyParams
_ = Bool
True
instance ToJSON EmptyParams where
toJSON :: EmptyParams -> Value
toJSON :: EmptyParams -> Value
toJSON EmptyParams
_ = Value
Aeson.Null
instance FromJSON EmptyParams where
parseJSON :: Value -> Parser EmptyParams
parseJSON Value
_ = String -> Parser EmptyParams
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"EmptyParams can only be used as a placeholder for empty params"
data JSONRPCErrorInfo = JSONRPCErrorInfo
{ JSONRPCErrorInfo -> Int
code :: Int
, JSONRPCErrorInfo -> Text
message :: Text
, JSONRPCErrorInfo -> Maybe Value
errorData :: Maybe Value
}
deriving stock (Int -> JSONRPCErrorInfo -> ShowS
[JSONRPCErrorInfo] -> ShowS
JSONRPCErrorInfo -> String
(Int -> JSONRPCErrorInfo -> ShowS)
-> (JSONRPCErrorInfo -> String)
-> ([JSONRPCErrorInfo] -> ShowS)
-> Show JSONRPCErrorInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> JSONRPCErrorInfo -> ShowS
showsPrec :: Int -> JSONRPCErrorInfo -> ShowS
$cshow :: JSONRPCErrorInfo -> String
show :: JSONRPCErrorInfo -> String
$cshowList :: [JSONRPCErrorInfo] -> ShowS
showList :: [JSONRPCErrorInfo] -> ShowS
Show, JSONRPCErrorInfo -> JSONRPCErrorInfo -> Bool
(JSONRPCErrorInfo -> JSONRPCErrorInfo -> Bool)
-> (JSONRPCErrorInfo -> JSONRPCErrorInfo -> Bool)
-> Eq JSONRPCErrorInfo
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: JSONRPCErrorInfo -> JSONRPCErrorInfo -> Bool
== :: JSONRPCErrorInfo -> JSONRPCErrorInfo -> Bool
$c/= :: JSONRPCErrorInfo -> JSONRPCErrorInfo -> Bool
/= :: JSONRPCErrorInfo -> JSONRPCErrorInfo -> Bool
Eq, (forall x. JSONRPCErrorInfo -> Rep JSONRPCErrorInfo x)
-> (forall x. Rep JSONRPCErrorInfo x -> JSONRPCErrorInfo)
-> Generic JSONRPCErrorInfo
forall x. Rep JSONRPCErrorInfo x -> JSONRPCErrorInfo
forall x. JSONRPCErrorInfo -> Rep JSONRPCErrorInfo x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. JSONRPCErrorInfo -> Rep JSONRPCErrorInfo x
from :: forall x. JSONRPCErrorInfo -> Rep JSONRPCErrorInfo x
$cto :: forall x. Rep JSONRPCErrorInfo x -> JSONRPCErrorInfo
to :: forall x. Rep JSONRPCErrorInfo x -> JSONRPCErrorInfo
Generic)
instance ToJSON JSONRPCErrorInfo where
toJSON :: JSONRPCErrorInfo -> Value
toJSON (JSONRPCErrorInfo Int
c Text
m Maybe Value
d) =
[Pair] -> Value
object ([Pair] -> Value) -> [Pair] -> Value
forall a b. (a -> b) -> a -> b
$
[ Key
"code" Key -> Int -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Int
c
, Key
"message" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text
m
]
[Pair] -> [Pair] -> [Pair]
forall a. Semigroup a => a -> a -> a
<> [Pair] -> (Value -> [Pair]) -> Maybe Value -> [Pair]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (\Value
v -> [Key
"data" Key -> Value -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Value
v]) Maybe Value
d
instance FromJSON JSONRPCErrorInfo where
parseJSON :: Value -> Parser JSONRPCErrorInfo
parseJSON = String
-> (Object -> Parser JSONRPCErrorInfo)
-> Value
-> Parser JSONRPCErrorInfo
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"JSONRPCErrorInfo" ((Object -> Parser JSONRPCErrorInfo)
-> Value -> Parser JSONRPCErrorInfo)
-> (Object -> Parser JSONRPCErrorInfo)
-> Value
-> Parser JSONRPCErrorInfo
forall a b. (a -> b) -> a -> b
$ \Object
o ->
Int -> Text -> Maybe Value -> JSONRPCErrorInfo
JSONRPCErrorInfo
(Int -> Text -> Maybe Value -> JSONRPCErrorInfo)
-> Parser Int -> Parser (Text -> Maybe Value -> JSONRPCErrorInfo)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser Int
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"code"
Parser (Text -> Maybe Value -> JSONRPCErrorInfo)
-> Parser Text -> Parser (Maybe Value -> JSONRPCErrorInfo)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"message"
Parser (Maybe Value -> JSONRPCErrorInfo)
-> Parser (Maybe Value) -> Parser JSONRPCErrorInfo
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe Value)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"data"
data JSONRPCRequest = JSONRPCRequest
{ JSONRPCRequest -> Text
jsonrpc :: Text
, JSONRPCRequest -> RequestId
id :: RequestId
, JSONRPCRequest -> Text
method :: Text
, JSONRPCRequest -> Value
params :: Value
}
deriving stock (Int -> JSONRPCRequest -> ShowS
[JSONRPCRequest] -> ShowS
JSONRPCRequest -> String
(Int -> JSONRPCRequest -> ShowS)
-> (JSONRPCRequest -> String)
-> ([JSONRPCRequest] -> ShowS)
-> Show JSONRPCRequest
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> JSONRPCRequest -> ShowS
showsPrec :: Int -> JSONRPCRequest -> ShowS
$cshow :: JSONRPCRequest -> String
show :: JSONRPCRequest -> String
$cshowList :: [JSONRPCRequest] -> ShowS
showList :: [JSONRPCRequest] -> ShowS
Show, JSONRPCRequest -> JSONRPCRequest -> Bool
(JSONRPCRequest -> JSONRPCRequest -> Bool)
-> (JSONRPCRequest -> JSONRPCRequest -> Bool) -> Eq JSONRPCRequest
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: JSONRPCRequest -> JSONRPCRequest -> Bool
== :: JSONRPCRequest -> JSONRPCRequest -> Bool
$c/= :: JSONRPCRequest -> JSONRPCRequest -> Bool
/= :: JSONRPCRequest -> JSONRPCRequest -> Bool
Eq, (forall x. JSONRPCRequest -> Rep JSONRPCRequest x)
-> (forall x. Rep JSONRPCRequest x -> JSONRPCRequest)
-> Generic JSONRPCRequest
forall x. Rep JSONRPCRequest x -> JSONRPCRequest
forall x. JSONRPCRequest -> Rep JSONRPCRequest x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. JSONRPCRequest -> Rep JSONRPCRequest x
from :: forall x. JSONRPCRequest -> Rep JSONRPCRequest x
$cto :: forall x. Rep JSONRPCRequest x -> JSONRPCRequest
to :: forall x. Rep JSONRPCRequest x -> JSONRPCRequest
Generic)
deriving anyclass ([JSONRPCRequest] -> Value
[JSONRPCRequest] -> Encoding
JSONRPCRequest -> Bool
JSONRPCRequest -> Value
JSONRPCRequest -> Encoding
(JSONRPCRequest -> Value)
-> (JSONRPCRequest -> Encoding)
-> ([JSONRPCRequest] -> Value)
-> ([JSONRPCRequest] -> Encoding)
-> (JSONRPCRequest -> Bool)
-> ToJSON JSONRPCRequest
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: JSONRPCRequest -> Value
toJSON :: JSONRPCRequest -> Value
$ctoEncoding :: JSONRPCRequest -> Encoding
toEncoding :: JSONRPCRequest -> Encoding
$ctoJSONList :: [JSONRPCRequest] -> Value
toJSONList :: [JSONRPCRequest] -> Value
$ctoEncodingList :: [JSONRPCRequest] -> Encoding
toEncodingList :: [JSONRPCRequest] -> Encoding
$comitField :: JSONRPCRequest -> Bool
omitField :: JSONRPCRequest -> Bool
ToJSON)
instance FromJSON JSONRPCRequest where
parseJSON :: Value -> Parser JSONRPCRequest
parseJSON = String
-> (Object -> Parser JSONRPCRequest)
-> Value
-> Parser JSONRPCRequest
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"JSONRPCRequest" ((Object -> Parser JSONRPCRequest)
-> Value -> Parser JSONRPCRequest)
-> (Object -> Parser JSONRPCRequest)
-> Value
-> Parser JSONRPCRequest
forall a b. (a -> b) -> a -> b
$ \Object
o ->
Text -> RequestId -> Text -> Value -> JSONRPCRequest
JSONRPCRequest
(Text -> RequestId -> Text -> Value -> JSONRPCRequest)
-> Parser Text
-> Parser (RequestId -> Text -> Value -> JSONRPCRequest)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"jsonrpc"
Parser (RequestId -> Text -> Value -> JSONRPCRequest)
-> Parser RequestId -> Parser (Text -> Value -> JSONRPCRequest)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser RequestId
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"id"
Parser (Text -> Value -> JSONRPCRequest)
-> Parser Text -> Parser (Value -> JSONRPCRequest)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"method"
Parser (Value -> JSONRPCRequest)
-> Parser Value -> Parser JSONRPCRequest
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe Value)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"params" Parser (Maybe Value) -> Value -> Parser Value
forall a. Parser (Maybe a) -> a -> Parser a
.!= Value
Null
data JSONRPCResponse = JSONRPCResponse
{ JSONRPCResponse -> Text
jsonrpc :: Text
, JSONRPCResponse -> RequestId
id :: RequestId
, JSONRPCResponse -> Value
result :: Value
}
deriving stock (Int -> JSONRPCResponse -> ShowS
[JSONRPCResponse] -> ShowS
JSONRPCResponse -> String
(Int -> JSONRPCResponse -> ShowS)
-> (JSONRPCResponse -> String)
-> ([JSONRPCResponse] -> ShowS)
-> Show JSONRPCResponse
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> JSONRPCResponse -> ShowS
showsPrec :: Int -> JSONRPCResponse -> ShowS
$cshow :: JSONRPCResponse -> String
show :: JSONRPCResponse -> String
$cshowList :: [JSONRPCResponse] -> ShowS
showList :: [JSONRPCResponse] -> ShowS
Show, JSONRPCResponse -> JSONRPCResponse -> Bool
(JSONRPCResponse -> JSONRPCResponse -> Bool)
-> (JSONRPCResponse -> JSONRPCResponse -> Bool)
-> Eq JSONRPCResponse
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: JSONRPCResponse -> JSONRPCResponse -> Bool
== :: JSONRPCResponse -> JSONRPCResponse -> Bool
$c/= :: JSONRPCResponse -> JSONRPCResponse -> Bool
/= :: JSONRPCResponse -> JSONRPCResponse -> Bool
Eq, (forall x. JSONRPCResponse -> Rep JSONRPCResponse x)
-> (forall x. Rep JSONRPCResponse x -> JSONRPCResponse)
-> Generic JSONRPCResponse
forall x. Rep JSONRPCResponse x -> JSONRPCResponse
forall x. JSONRPCResponse -> Rep JSONRPCResponse x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. JSONRPCResponse -> Rep JSONRPCResponse x
from :: forall x. JSONRPCResponse -> Rep JSONRPCResponse x
$cto :: forall x. Rep JSONRPCResponse x -> JSONRPCResponse
to :: forall x. Rep JSONRPCResponse x -> JSONRPCResponse
Generic)
deriving anyclass (Maybe JSONRPCResponse
Value -> Parser [JSONRPCResponse]
Value -> Parser JSONRPCResponse
(Value -> Parser JSONRPCResponse)
-> (Value -> Parser [JSONRPCResponse])
-> Maybe JSONRPCResponse
-> FromJSON JSONRPCResponse
forall a.
(Value -> Parser a)
-> (Value -> Parser [a]) -> Maybe a -> FromJSON a
$cparseJSON :: Value -> Parser JSONRPCResponse
parseJSON :: Value -> Parser JSONRPCResponse
$cparseJSONList :: Value -> Parser [JSONRPCResponse]
parseJSONList :: Value -> Parser [JSONRPCResponse]
$comittedField :: Maybe JSONRPCResponse
omittedField :: Maybe JSONRPCResponse
FromJSON, [JSONRPCResponse] -> Value
[JSONRPCResponse] -> Encoding
JSONRPCResponse -> Bool
JSONRPCResponse -> Value
JSONRPCResponse -> Encoding
(JSONRPCResponse -> Value)
-> (JSONRPCResponse -> Encoding)
-> ([JSONRPCResponse] -> Value)
-> ([JSONRPCResponse] -> Encoding)
-> (JSONRPCResponse -> Bool)
-> ToJSON JSONRPCResponse
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: JSONRPCResponse -> Value
toJSON :: JSONRPCResponse -> Value
$ctoEncoding :: JSONRPCResponse -> Encoding
toEncoding :: JSONRPCResponse -> Encoding
$ctoJSONList :: [JSONRPCResponse] -> Value
toJSONList :: [JSONRPCResponse] -> Value
$ctoEncodingList :: [JSONRPCResponse] -> Encoding
toEncodingList :: [JSONRPCResponse] -> Encoding
$comitField :: JSONRPCResponse -> Bool
omitField :: JSONRPCResponse -> Bool
ToJSON)
data JSONRPCError = JSONRPCError
{ JSONRPCError -> Text
jsonrpc :: Text
, JSONRPCError -> RequestId
id :: RequestId
, JSONRPCError -> JSONRPCErrorInfo
error :: JSONRPCErrorInfo
}
deriving stock (Int -> JSONRPCError -> ShowS
[JSONRPCError] -> ShowS
JSONRPCError -> String
(Int -> JSONRPCError -> ShowS)
-> (JSONRPCError -> String)
-> ([JSONRPCError] -> ShowS)
-> Show JSONRPCError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> JSONRPCError -> ShowS
showsPrec :: Int -> JSONRPCError -> ShowS
$cshow :: JSONRPCError -> String
show :: JSONRPCError -> String
$cshowList :: [JSONRPCError] -> ShowS
showList :: [JSONRPCError] -> ShowS
Show, JSONRPCError -> JSONRPCError -> Bool
(JSONRPCError -> JSONRPCError -> Bool)
-> (JSONRPCError -> JSONRPCError -> Bool) -> Eq JSONRPCError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: JSONRPCError -> JSONRPCError -> Bool
== :: JSONRPCError -> JSONRPCError -> Bool
$c/= :: JSONRPCError -> JSONRPCError -> Bool
/= :: JSONRPCError -> JSONRPCError -> Bool
Eq, (forall x. JSONRPCError -> Rep JSONRPCError x)
-> (forall x. Rep JSONRPCError x -> JSONRPCError)
-> Generic JSONRPCError
forall x. Rep JSONRPCError x -> JSONRPCError
forall x. JSONRPCError -> Rep JSONRPCError x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. JSONRPCError -> Rep JSONRPCError x
from :: forall x. JSONRPCError -> Rep JSONRPCError x
$cto :: forall x. Rep JSONRPCError x -> JSONRPCError
to :: forall x. Rep JSONRPCError x -> JSONRPCError
Generic)
deriving anyclass (Maybe JSONRPCError
Value -> Parser [JSONRPCError]
Value -> Parser JSONRPCError
(Value -> Parser JSONRPCError)
-> (Value -> Parser [JSONRPCError])
-> Maybe JSONRPCError
-> FromJSON JSONRPCError
forall a.
(Value -> Parser a)
-> (Value -> Parser [a]) -> Maybe a -> FromJSON a
$cparseJSON :: Value -> Parser JSONRPCError
parseJSON :: Value -> Parser JSONRPCError
$cparseJSONList :: Value -> Parser [JSONRPCError]
parseJSONList :: Value -> Parser [JSONRPCError]
$comittedField :: Maybe JSONRPCError
omittedField :: Maybe JSONRPCError
FromJSON, [JSONRPCError] -> Value
[JSONRPCError] -> Encoding
JSONRPCError -> Bool
JSONRPCError -> Value
JSONRPCError -> Encoding
(JSONRPCError -> Value)
-> (JSONRPCError -> Encoding)
-> ([JSONRPCError] -> Value)
-> ([JSONRPCError] -> Encoding)
-> (JSONRPCError -> Bool)
-> ToJSON JSONRPCError
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: JSONRPCError -> Value
toJSON :: JSONRPCError -> Value
$ctoEncoding :: JSONRPCError -> Encoding
toEncoding :: JSONRPCError -> Encoding
$ctoJSONList :: [JSONRPCError] -> Value
toJSONList :: [JSONRPCError] -> Value
$ctoEncodingList :: [JSONRPCError] -> Encoding
toEncodingList :: [JSONRPCError] -> Encoding
$comitField :: JSONRPCError -> Bool
omitField :: JSONRPCError -> Bool
ToJSON)
data JSONRPCNotification = JSONRPCNotification
{ JSONRPCNotification -> Text
jsonrpc :: Text
, JSONRPCNotification -> Text
method :: Text
, JSONRPCNotification -> Value
params :: Value
}
deriving stock (Int -> JSONRPCNotification -> ShowS
[JSONRPCNotification] -> ShowS
JSONRPCNotification -> String
(Int -> JSONRPCNotification -> ShowS)
-> (JSONRPCNotification -> String)
-> ([JSONRPCNotification] -> ShowS)
-> Show JSONRPCNotification
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> JSONRPCNotification -> ShowS
showsPrec :: Int -> JSONRPCNotification -> ShowS
$cshow :: JSONRPCNotification -> String
show :: JSONRPCNotification -> String
$cshowList :: [JSONRPCNotification] -> ShowS
showList :: [JSONRPCNotification] -> ShowS
Show, JSONRPCNotification -> JSONRPCNotification -> Bool
(JSONRPCNotification -> JSONRPCNotification -> Bool)
-> (JSONRPCNotification -> JSONRPCNotification -> Bool)
-> Eq JSONRPCNotification
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: JSONRPCNotification -> JSONRPCNotification -> Bool
== :: JSONRPCNotification -> JSONRPCNotification -> Bool
$c/= :: JSONRPCNotification -> JSONRPCNotification -> Bool
/= :: JSONRPCNotification -> JSONRPCNotification -> Bool
Eq, (forall x. JSONRPCNotification -> Rep JSONRPCNotification x)
-> (forall x. Rep JSONRPCNotification x -> JSONRPCNotification)
-> Generic JSONRPCNotification
forall x. Rep JSONRPCNotification x -> JSONRPCNotification
forall x. JSONRPCNotification -> Rep JSONRPCNotification x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. JSONRPCNotification -> Rep JSONRPCNotification x
from :: forall x. JSONRPCNotification -> Rep JSONRPCNotification x
$cto :: forall x. Rep JSONRPCNotification x -> JSONRPCNotification
to :: forall x. Rep JSONRPCNotification x -> JSONRPCNotification
Generic)
instance ToJSON JSONRPCNotification where
toJSON :: JSONRPCNotification -> Value
toJSON (JSONRPCNotification Text
j Text
m Value
Null) =
[Pair] -> Value
object [Key
"jsonrpc" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text
j, Key
"method" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text
m]
toJSON (JSONRPCNotification Text
j Text
m Value
p) =
[Pair] -> Value
object [Key
"jsonrpc" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text
j, Key
"method" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text
m, Key
"params" Key -> Value -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Value
p]
instance FromJSON JSONRPCNotification where
parseJSON :: Value -> Parser JSONRPCNotification
parseJSON = String
-> (Object -> Parser JSONRPCNotification)
-> Value
-> Parser JSONRPCNotification
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"JSONRPCNotification" ((Object -> Parser JSONRPCNotification)
-> Value -> Parser JSONRPCNotification)
-> (Object -> Parser JSONRPCNotification)
-> Value
-> Parser JSONRPCNotification
forall a b. (a -> b) -> a -> b
$ \Object
o ->
Text -> Text -> Value -> JSONRPCNotification
JSONRPCNotification
(Text -> Text -> Value -> JSONRPCNotification)
-> Parser Text -> Parser (Text -> Value -> JSONRPCNotification)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"jsonrpc"
Parser (Text -> Value -> JSONRPCNotification)
-> Parser Text -> Parser (Value -> JSONRPCNotification)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"method"
Parser (Value -> JSONRPCNotification)
-> Parser Value -> Parser JSONRPCNotification
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser (Maybe Value)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"params" Parser (Maybe Value) -> Value -> Parser Value
forall a. Parser (Maybe a) -> a -> Parser a
.!= Value
Null
data JSONRPCMessage
= RequestMessage JSONRPCRequest
| ResponseMessage JSONRPCResponse
| ErrorMessage JSONRPCError
| NotificationMessage JSONRPCNotification
deriving stock (Int -> JSONRPCMessage -> ShowS
[JSONRPCMessage] -> ShowS
JSONRPCMessage -> String
(Int -> JSONRPCMessage -> ShowS)
-> (JSONRPCMessage -> String)
-> ([JSONRPCMessage] -> ShowS)
-> Show JSONRPCMessage
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> JSONRPCMessage -> ShowS
showsPrec :: Int -> JSONRPCMessage -> ShowS
$cshow :: JSONRPCMessage -> String
show :: JSONRPCMessage -> String
$cshowList :: [JSONRPCMessage] -> ShowS
showList :: [JSONRPCMessage] -> ShowS
Show, JSONRPCMessage -> JSONRPCMessage -> Bool
(JSONRPCMessage -> JSONRPCMessage -> Bool)
-> (JSONRPCMessage -> JSONRPCMessage -> Bool) -> Eq JSONRPCMessage
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: JSONRPCMessage -> JSONRPCMessage -> Bool
== :: JSONRPCMessage -> JSONRPCMessage -> Bool
$c/= :: JSONRPCMessage -> JSONRPCMessage -> Bool
/= :: JSONRPCMessage -> JSONRPCMessage -> Bool
Eq, (forall x. JSONRPCMessage -> Rep JSONRPCMessage x)
-> (forall x. Rep JSONRPCMessage x -> JSONRPCMessage)
-> Generic JSONRPCMessage
forall x. Rep JSONRPCMessage x -> JSONRPCMessage
forall x. JSONRPCMessage -> Rep JSONRPCMessage x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. JSONRPCMessage -> Rep JSONRPCMessage x
from :: forall x. JSONRPCMessage -> Rep JSONRPCMessage x
$cto :: forall x. Rep JSONRPCMessage x -> JSONRPCMessage
to :: forall x. Rep JSONRPCMessage x -> JSONRPCMessage
Generic)
instance ToJSON JSONRPCMessage where
toJSON :: JSONRPCMessage -> Value
toJSON (RequestMessage JSONRPCRequest
r) = JSONRPCRequest -> Value
forall a. ToJSON a => a -> Value
toJSON JSONRPCRequest
r
toJSON (ResponseMessage JSONRPCResponse
r) = JSONRPCResponse -> Value
forall a. ToJSON a => a -> Value
toJSON JSONRPCResponse
r
toJSON (ErrorMessage JSONRPCError
e) = JSONRPCError -> Value
forall a. ToJSON a => a -> Value
toJSON JSONRPCError
e
toJSON (NotificationMessage JSONRPCNotification
n) = JSONRPCNotification -> Value
forall a. ToJSON a => a -> Value
toJSON JSONRPCNotification
n
instance FromJSON JSONRPCMessage where
parseJSON :: Value -> Parser JSONRPCMessage
parseJSON Value
v =
(JSONRPCRequest -> JSONRPCMessage
RequestMessage (JSONRPCRequest -> JSONRPCMessage)
-> Parser JSONRPCRequest -> Parser JSONRPCMessage
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser JSONRPCRequest
forall a. FromJSON a => Value -> Parser a
parseJSON Value
v)
Parser JSONRPCMessage
-> Parser JSONRPCMessage -> Parser JSONRPCMessage
forall a. Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (JSONRPCResponse -> JSONRPCMessage
ResponseMessage (JSONRPCResponse -> JSONRPCMessage)
-> Parser JSONRPCResponse -> Parser JSONRPCMessage
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser JSONRPCResponse
forall a. FromJSON a => Value -> Parser a
parseJSON Value
v)
Parser JSONRPCMessage
-> Parser JSONRPCMessage -> Parser JSONRPCMessage
forall a. Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (JSONRPCError -> JSONRPCMessage
ErrorMessage (JSONRPCError -> JSONRPCMessage)
-> Parser JSONRPCError -> Parser JSONRPCMessage
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser JSONRPCError
forall a. FromJSON a => Value -> Parser a
parseJSON Value
v)
Parser JSONRPCMessage
-> Parser JSONRPCMessage -> Parser JSONRPCMessage
forall a. Parser a -> Parser a -> Parser a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (JSONRPCNotification -> JSONRPCMessage
NotificationMessage (JSONRPCNotification -> JSONRPCMessage)
-> Parser JSONRPCNotification -> Parser JSONRPCMessage
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser JSONRPCNotification
forall a. FromJSON a => Value -> Parser a
parseJSON Value
v)
type family RequestParamType (rep :: Type -> Type) :: Type where
RequestParamType (D1 _ (C1 _ (S1 _ (K1 _ RequestId) :*: S1 _ (K1 _ p)))) = p
RequestParamType (D1 ('MetaData nm _ _ _) _) =
TypeError
( ('Text "Error when defining IsJSONRPCRequest for " :<>: 'Text nm :<>: 'Text ":")
:$$: 'Text "The request datatype must be a record with fields 'id' and 'params'"
)
class
( HasField "id" a RequestId
, HasField "params" a (RequestParams a)
, Typeable a
, ToJSON (RequestParams a)
, FromJSON (RequestParams a)
) =>
IsJSONRPCRequest a
where
type RequestParams a
type RequestParams a = RequestParamType (Rep a)
requestMethod :: Proxy a -> Text
toJSONRPCRequest :: a -> JSONRPCRequest
toJSONRPCRequest (a
req :: a) =
Text -> RequestId -> Text -> Value -> JSONRPCRequest
JSONRPCRequest Text
rPC_VERSION (forall {k} (x :: k) r a. HasField x r a => r -> a
forall (x :: Symbol) r a. HasField x r a => r -> a
getField @"id" a
req) (Proxy a -> Text
forall a. IsJSONRPCRequest a => Proxy a -> Text
requestMethod (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @a)) (RequestParams a -> Value
forall a. ToJSON a => a -> Value
toJSON (forall {k} (x :: k) r a. HasField x r a => r -> a
forall (x :: Symbol) r a. HasField x r a => r -> a
getField @"params" a
req))
fromJSONRPCRequest :: JSONRPCRequest -> Either String a
default fromJSONRPCRequest ::
( Generic a
, Rep a ~ D1 c0 (C1 i0 (S1 s0 (K1 i1 RequestId) :*: S1 s1 (K1 i30 (RequestParams a))))
) =>
JSONRPCRequest ->
Either String a
fromJSONRPCRequest JSONRPCRequest{id :: JSONRPCRequest -> RequestId
id = RequestId
req_id, params :: JSONRPCRequest -> Value
params = Value
req_params} =
case forall a. FromJSON a => Value -> Result a
fromJSON @(RequestParams a) Value
req_params of
Success RequestParams a
p -> a -> Either String a
forall a b. b -> Either a b
Right (a -> Either String a) -> a -> Either String a
forall a b. (a -> b) -> a -> b
$ Rep a Any -> a
forall a x. Generic a => Rep a x -> a
forall x. Rep a x -> a
to (Rep a Any -> a) -> Rep a Any -> a
forall a b. (a -> b) -> a -> b
$ M1
C
i0
(S1 s0 (K1 i1 RequestId) :*: S1 s1 (K1 i30 (RequestParams a)))
Any
-> M1
D
c0
(C1
i0 (S1 s0 (K1 i1 RequestId) :*: S1 s1 (K1 i30 (RequestParams a))))
Any
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 ((:*:)
(S1 s0 (K1 i1 RequestId)) (S1 s1 (K1 i30 (RequestParams a))) Any
-> M1
C
i0
(S1 s0 (K1 i1 RequestId) :*: S1 s1 (K1 i30 (RequestParams a)))
Any
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (K1 i1 RequestId Any -> M1 S s0 (K1 i1 RequestId) Any
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (RequestId -> K1 i1 RequestId Any
forall k i c (p :: k). c -> K1 i c p
K1 RequestId
req_id) M1 S s0 (K1 i1 RequestId) Any
-> M1 S s1 (K1 i30 (RequestParams a)) Any
-> (:*:)
(S1 s0 (K1 i1 RequestId)) (S1 s1 (K1 i30 (RequestParams a))) Any
forall k (f :: k -> *) (g :: k -> *) (p :: k).
f p -> g p -> (:*:) f g p
:*: K1 i30 (RequestParams a) Any
-> M1 S s1 (K1 i30 (RequestParams a)) Any
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (RequestParams a -> K1 i30 (RequestParams a) Any
forall k i c (p :: k). c -> K1 i c p
K1 RequestParams a
p)))
Aeson.Error String
err -> String -> Either String a
forall a b. a -> Either a b
Left String
err
newtype ViaJSONRPCRequest a = ViaJSONRPCRequest {forall a. ViaJSONRPCRequest a -> a
unViaJSONRPCRequest :: a}
instance (IsJSONRPCRequest a) => ToJSON (ViaJSONRPCRequest a) where
toJSON :: ViaJSONRPCRequest a -> Value
toJSON = JSONRPCRequest -> Value
forall a. ToJSON a => a -> Value
toJSON (JSONRPCRequest -> Value)
-> (ViaJSONRPCRequest a -> JSONRPCRequest)
-> ViaJSONRPCRequest a
-> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> JSONRPCRequest
forall a. IsJSONRPCRequest a => a -> JSONRPCRequest
toJSONRPCRequest (a -> JSONRPCRequest)
-> (ViaJSONRPCRequest a -> a)
-> ViaJSONRPCRequest a
-> JSONRPCRequest
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ViaJSONRPCRequest a -> a
forall a. ViaJSONRPCRequest a -> a
unViaJSONRPCRequest
instance (IsJSONRPCRequest a) => FromJSON (ViaJSONRPCRequest a) where
parseJSON :: Value -> Parser (ViaJSONRPCRequest a)
parseJSON = String
-> (Object -> Parser (ViaJSONRPCRequest a))
-> Value
-> Parser (ViaJSONRPCRequest a)
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject (TypeRep -> String
forall a. Show a => a -> String
show (TypeRep -> String) -> TypeRep -> String
forall a b. (a -> b) -> a -> b
$ Proxy a -> TypeRep
forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @a)) ((Object -> Parser (ViaJSONRPCRequest a))
-> Value -> Parser (ViaJSONRPCRequest a))
-> (Object -> Parser (ViaJSONRPCRequest a))
-> Value
-> Parser (ViaJSONRPCRequest a)
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
Text
m <- Object
o Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"method"
RequestId
req_id <- Object
o Object -> Key -> Parser RequestId
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"id"
if Text
m Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Proxy a -> Text
forall a. IsJSONRPCRequest a => Proxy a -> Text
requestMethod (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @a)
then do
Value
p <- Object
o Object -> Key -> Parser (Maybe Value)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"params" Parser (Maybe Value) -> Value -> Parser Value
forall a. Parser (Maybe a) -> a -> Parser a
.!= Value
Null
case JSONRPCRequest -> Either String a
forall a. IsJSONRPCRequest a => JSONRPCRequest -> Either String a
fromJSONRPCRequest (Text -> RequestId -> Text -> Value -> JSONRPCRequest
JSONRPCRequest Text
rPC_VERSION RequestId
req_id Text
m Value
p) of
Right a
r -> ViaJSONRPCRequest a -> Parser (ViaJSONRPCRequest a)
forall a. a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> ViaJSONRPCRequest a
forall a. a -> ViaJSONRPCRequest a
ViaJSONRPCRequest a
r)
Left String
err -> String -> Parser (ViaJSONRPCRequest a)
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser (ViaJSONRPCRequest a))
-> String -> Parser (ViaJSONRPCRequest a)
forall a b. (a -> b) -> a -> b
$ String
"Failed to parse params for " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> TypeRep -> String
forall a. Show a => a -> String
show (Proxy a -> TypeRep
forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @a)) String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
": " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
err
else String -> Parser (ViaJSONRPCRequest a)
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser (ViaJSONRPCRequest a))
-> String -> Parser (ViaJSONRPCRequest a)
forall a b. (a -> b) -> a -> b
$ String
"Expected method '" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
forall a. Show a => a -> String
show (Proxy a -> Text
forall a. IsJSONRPCRequest a => Proxy a -> Text
requestMethod (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @a)) String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"'"
type family NotificationParamType (rep :: Type -> Type) :: Type where
NotificationParamType (D1 _ (C1 _ (S1 _ (K1 _ p)))) = p
NotificationParamType (D1 ('MetaData nm _ _ _) _) =
TypeError
( ('Text "Error when defining IsJSONRPCNotification for " :<>: 'Text nm :<>: 'Text ":")
:$$: 'Text "The notification datatype must be a record with a single field 'params'"
)
class
( HasField "params" a (NotificationParams a)
, Typeable a
, ToJSON (NotificationParams a)
, FromJSON (NotificationParams a)
) =>
IsJSONRPCNotification a
where
type NotificationParams a
type NotificationParams a = NotificationParamType (Rep a)
notificationsMethod :: Proxy a -> Text
toJSONRPCNotification :: a -> JSONRPCNotification
toJSONRPCNotification (a
req :: a) =
Text -> Text -> Value -> JSONRPCNotification
JSONRPCNotification Text
rPC_VERSION (Proxy a -> Text
forall a. IsJSONRPCNotification a => Proxy a -> Text
notificationsMethod (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @a)) (NotificationParams a -> Value
forall a. ToJSON a => a -> Value
toJSON (forall {k} (x :: k) r a. HasField x r a => r -> a
forall (x :: Symbol) r a. HasField x r a => r -> a
getField @"params" a
req))
fromJSONRPCNotification :: JSONRPCNotification -> Either String a
default fromJSONRPCNotification ::
( Generic a
, Rep a ~ D1 c0 (C1 i0 (S1 s1 (K1 i30 (NotificationParams a))))
) =>
JSONRPCNotification ->
Either String a
fromJSONRPCNotification JSONRPCNotification{params :: JSONRPCNotification -> Value
params = Value
notification_params} =
case forall a. FromJSON a => Value -> Result a
fromJSON @(NotificationParams a) Value
notification_params of
Success NotificationParams a
p -> a -> Either String a
forall a b. b -> Either a b
Right (a -> Either String a) -> a -> Either String a
forall a b. (a -> b) -> a -> b
$ Rep a Any -> a
forall a x. Generic a => Rep a x -> a
forall x. Rep a x -> a
to (Rep a Any -> a) -> Rep a Any -> a
forall a b. (a -> b) -> a -> b
$ M1 C i0 (S1 s1 (K1 i30 (NotificationParams a))) Any
-> M1 D c0 (C1 i0 (S1 s1 (K1 i30 (NotificationParams a)))) Any
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (M1 S s1 (K1 i30 (NotificationParams a)) Any
-> M1 C i0 (S1 s1 (K1 i30 (NotificationParams a))) Any
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (K1 i30 (NotificationParams a) Any
-> M1 S s1 (K1 i30 (NotificationParams a)) Any
forall k i (c :: Meta) (f :: k -> *) (p :: k). f p -> M1 i c f p
M1 (NotificationParams a -> K1 i30 (NotificationParams a) Any
forall k i c (p :: k). c -> K1 i c p
K1 NotificationParams a
p)))
Aeson.Error String
err -> String -> Either String a
forall a b. a -> Either a b
Left String
err
newtype ViaJSONRPCNotification a = ViaJSONRPCNotification {forall a. ViaJSONRPCNotification a -> a
unViaJSONRPCNotification :: a}
instance (IsJSONRPCNotification a) => ToJSON (ViaJSONRPCNotification a) where
toJSON :: ViaJSONRPCNotification a -> Value
toJSON = JSONRPCNotification -> Value
forall a. ToJSON a => a -> Value
toJSON (JSONRPCNotification -> Value)
-> (ViaJSONRPCNotification a -> JSONRPCNotification)
-> ViaJSONRPCNotification a
-> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> JSONRPCNotification
forall a. IsJSONRPCNotification a => a -> JSONRPCNotification
toJSONRPCNotification (a -> JSONRPCNotification)
-> (ViaJSONRPCNotification a -> a)
-> ViaJSONRPCNotification a
-> JSONRPCNotification
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ViaJSONRPCNotification a -> a
forall a. ViaJSONRPCNotification a -> a
unViaJSONRPCNotification
instance (IsJSONRPCNotification a) => FromJSON (ViaJSONRPCNotification a) where
parseJSON :: Value -> Parser (ViaJSONRPCNotification a)
parseJSON = String
-> (Object -> Parser (ViaJSONRPCNotification a))
-> Value
-> Parser (ViaJSONRPCNotification a)
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject (TypeRep -> String
forall a. Show a => a -> String
show (TypeRep -> String) -> TypeRep -> String
forall a b. (a -> b) -> a -> b
$ Proxy a -> TypeRep
forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @a)) ((Object -> Parser (ViaJSONRPCNotification a))
-> Value -> Parser (ViaJSONRPCNotification a))
-> (Object -> Parser (ViaJSONRPCNotification a))
-> Value
-> Parser (ViaJSONRPCNotification a)
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
Text
m <- Object
o Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"method"
if Text
m Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Proxy a -> Text
forall a. IsJSONRPCNotification a => Proxy a -> Text
notificationsMethod (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @a)
then do
Value
p <- Object
o Object -> Key -> Parser (Maybe Value)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"params" Parser (Maybe Value) -> Value -> Parser Value
forall a. Parser (Maybe a) -> a -> Parser a
.!= Value
Null
case JSONRPCNotification -> Either String a
forall a.
IsJSONRPCNotification a =>
JSONRPCNotification -> Either String a
fromJSONRPCNotification (Text -> Text -> Value -> JSONRPCNotification
JSONRPCNotification Text
rPC_VERSION Text
m Value
p) of
Right a
r -> ViaJSONRPCNotification a -> Parser (ViaJSONRPCNotification a)
forall a. a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> ViaJSONRPCNotification a
forall a. a -> ViaJSONRPCNotification a
ViaJSONRPCNotification a
r)
Left String
err -> String -> Parser (ViaJSONRPCNotification a)
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser (ViaJSONRPCNotification a))
-> String -> Parser (ViaJSONRPCNotification a)
forall a b. (a -> b) -> a -> b
$ String
"Failed to parse params for " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> TypeRep -> String
forall a. Show a => a -> String
show (Proxy a -> TypeRep
forall {k} (proxy :: k -> *) (a :: k).
Typeable a =>
proxy a -> TypeRep
typeRep (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @a)) String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
": " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
err
else String -> Parser (ViaJSONRPCNotification a)
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser (ViaJSONRPCNotification a))
-> String -> Parser (ViaJSONRPCNotification a)
forall a b. (a -> b) -> a -> b
$ String
"Expected method '" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
forall a. Show a => a -> String
show (Proxy a -> Text
forall a. IsJSONRPCNotification a => Proxy a -> Text
notificationsMethod (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @a)) String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"'"
pARSE_ERROR :: Int
pARSE_ERROR :: Int
pARSE_ERROR = -Int
32700
iNVALID_REQUEST :: Int
iNVALID_REQUEST :: Int
iNVALID_REQUEST = -Int
32600
mETHOD_NOT_FOUND :: Int
mETHOD_NOT_FOUND :: Int
mETHOD_NOT_FOUND = -Int
32601
iNVALID_PARAMS :: Int
iNVALID_PARAMS :: Int
iNVALID_PARAMS = -Int
32602
iNTERNAL_ERROR :: Int
iNTERNAL_ERROR :: Int
iNTERNAL_ERROR = -Int
32603