forked from Textalk/jsonrpc2-erlang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonrpc2_client.erl
168 lines (143 loc) · 6.46 KB
/
jsonrpc2_client.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
-module(jsonrpc2_client).
-export_type([request/0, response/0]).
-export([create_request/1, parse_response/1, batch_call/5]).
-type call_req() :: {jsonrpc2:method(), jsonrpc2:params(), jsonrpc2:id()}.
-type notification_req() :: {jsonrpc2:method(), jsonrpc2:params()}.
-type batch_req() :: [call_req() | notification_req()].
-type request() :: call_req() | notification_req() | batch_req().
-type response() :: {ok, jsonrpc2:json()} | {error, jsonrpc2:error()}.
-type transportfun() :: fun ((binary()) -> binary()).
-type json_encode() :: fun ((jsonrpc2:json()) -> binary()).
-type json_decode() :: fun ((binary()) -> jsonrpc2:json()).
%% @doc Creates a call, notification or batch request, depending on the parameter.
-spec create_request(request()) -> jsonrpc2:json().
create_request({Method, Params}) ->
{[{<<"jsonrpc">>, <<"2.0">>},
{<<"method">>, Method},
{<<"params">>, Params}]};
create_request({Method, Params, Id}) ->
{[{<<"jsonrpc">>, <<"2.0">>},
{<<"method">>, Method},
{<<"params">>, Params},
{<<"id">>, Id}]};
create_request(Reqs) when is_list(Reqs) ->
lists:map(fun create_request/1, Reqs).
%% @doc Parses a structured response and returns a list of pairs, with id and the retuned value.
%% Throws invalid_jsonrpc_response.
-spec parse_response(jsonrpc2:json()) -> [{jsonrpc2:id(), response()}].
parse_response({_} = Response) ->
parse_single_response(Response);
parse_response(BatchResponse) when is_list(BatchResponse) ->
lists:map(fun parse_single_response/1, BatchResponse).
%% @doc Calls multiple methods as a batch call and returns the results in the same order
-spec batch_call([{jsonrpc2:method(), jsonrpc2:params()}], transportfun(),
json_decode(), json_encode(), FirstId :: integer()) ->
[response()].
batch_call(MethodsAndParams, TransportFun, JsonDecode, JsonEncode, FirstId) ->
MethodParamsIds = enumerate_call_tuples(MethodsAndParams, FirstId),
JsonReq = create_request(MethodParamsIds),
BinReq = JsonEncode(JsonReq),
try
%% The transport fun can fail gracefully by throwing {transport_error, binary()}
BinResp = try TransportFun(BinReq)
catch throw:{transport_error, TransportError} when is_binary(TransportError) ->
throw({jsonrpc2_client, TransportError})
end,
%% JsonDecode can fail (any kind of error)
JsonResp = try JsonDecode(BinResp)
catch _:_ -> throw({jsonrpc2_client, invalid_json})
end,
%% parse_response can fail by throwing invalid_jsonrpc_response
RepliesById = try parse_response(JsonResp)
catch throw:invalid_jsonrpc_response ->
throw({jsonrpc2_client, invalid_jsonrpc_response})
end,
%% Decompose the replies into a list in the same order as MethodsAndParams.
LastId = FirstId + length(MethodsAndParams) - 1,
denumerate_replies(RepliesById, FirstId, LastId)
catch throw:{jsonrpc2_client, ErrorData} ->
%% Failure in transport function. Repeat the error data for each request to
%% simulate a batch response.
lists:duplicate(length(MethodsAndParams), {error, {server_error, ErrorData}})
end.
%%----------
%% Internal
%%----------
%% @doc Helper for parse_response/1. Returns a single pair {Id, Response}.
-spec parse_single_response(jsonrpc2:json()) -> {jsonrpc2:id(), response()}.
parse_single_response({Response}) ->
<<"2.0">> == proplists:get_value(<<"jsonrpc">>, Response)
orelse throw(invalid_jsonrpc_response),
Id = proplists:get_value(<<"id">>, Response),
is_number(Id) orelse Id == null
orelse throw(invalid_jsonrpc_response),
Result = proplists:get_value(<<"result">>, Response, undefined),
Error = proplists:get_value(<<"error">>, Response, undefined),
Reply = case {Result, Error} of
{undefined, undefined} ->
{error, {server_error, <<"Invalid JSON-RPC 2.0 response">>}};
{_, undefined} ->
{ok, Result};
{undefined, {ErrorProplist}} ->
%% extract the error code and convert to atom
%ErrorType = internal_error,
Code = proplists:get_value(<<"code">>, ErrorProplist, -32000),
ErrorMessage = proplists:get_value(<<"message">>, ErrorProplist, undefined),
ErrorData = proplists:get_value(<<"data">>, ErrorProplist, ErrorMessage),
{error, {Code, ErrorMessage, ErrorData}};
_ ->
%% both error and result
{error, {server_error, <<"Invalid JSON-RPC 2.0 response">>}}
end,
{Id, Reply}.
%% @doc Gives each method-params pair a number. Returns a list of triples: method-params-id.
enumerate_call_tuples(MethodParamsPairs, FirstId) ->
enumerate_call_tuples(MethodParamsPairs, FirstId, []).
%% @doc Helper for enumerate_call_tuples/2.
enumerate_call_tuples([{Method, Params} | MPs], NextId, Acc) ->
Triple = {Method, Params, NextId},
enumerate_call_tuples(MPs, NextId + 1, [Triple | Acc]);
enumerate_call_tuples([], _, Acc) ->
lists:reverse(Acc).
%% @doc Finds each pair {Id, Reply} for each Id in the range FirstId..LastId in the proplist
%% Replies. Removes the id and returns the only the replies in the correct order.
denumerate_replies(Replies, FirstId, LastId) ->
denumerate_replies(dict:from_list(Replies), FirstId, LastId, []).
%% @doc Helper for denumerate_replies/3.
denumerate_replies(ReplyDict, FirstId, LastId, Acc) when FirstId =< LastId ->
Reply = dict:fetch(FirstId, ReplyDict),
Acc1 = [Reply | Acc],
denumerate_replies(ReplyDict, FirstId + 1, LastId, Acc1);
denumerate_replies(_, _, _, Acc) ->
lists:reverse(Acc).
%%------------
%% Unit tests
%%------------
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
enumerate_call_tuples_test() ->
Input = [{x, foo}, {y, bar}, {z, baz}],
FirstId = 3,
Expect = [{x, foo, 3}, {y, bar, 4}, {z, baz, 5}],
Expect = enumerate_call_tuples(Input, FirstId).
denumerate_replies_test() ->
Input = [{3, foo}, {5, baz}, {4, bar}],
FirstId = 3,
LastId = 5 = FirstId + length(Input) - 1,
Expect = [foo, bar, baz],
Expect = denumerate_replies(Input, FirstId, LastId).
transport_error_test() ->
TransportFun = fun (_) -> throw({transport_error, <<"404 or whatever">>}) end,
JsonEncode = fun (_) -> <<"foo">> end,
JsonDecode = fun (_) -> [] end,
MethodsAndParams = [{<<"foo">>, []}],
Expect = [{error, {server_error, <<"404 or whatever">>}}],
?assertEqual(Expect, batch_call(MethodsAndParams, TransportFun, JsonDecode, JsonEncode, 1)).
transport_return_invalid_json_test() ->
TransportFun = fun (_) -> <<"some non-JSON junk">> end,
JsonEncode = fun (_) -> <<"{\"foo\":\"bar\"}">> end,
JsonDecode = fun (_) -> throw(invalid_json) end,
MethodsAndParams = [{<<"foo">>, []}],
Expect = [{error, {server_error, invalid_json}}],
?assertEqual(Expect, batch_call(MethodsAndParams, TransportFun, JsonDecode, JsonEncode, 1)).
-endif.