Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 117 lines (108 sloc) 4.285 kB
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
1 %% Copyright (C) 2006 Gaspar Chilingarov <nm@web.am>
2 %% Gurgen Tumanyan <barbarian@armkb.com>
3 %% All rights reserved.
4 %%
5 %%
6 %% Redistribution and use in source and binary forms, with or without
7 %% modification, are permitted provided that the following conditions
455578a @vinoski major trailing whitespace cleanup
vinoski authored
8 %% are met:
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
9 %%
10 %% 1. Redistributions of source code must retain the above copyright
455578a @vinoski major trailing whitespace cleanup
vinoski authored
11 %% notice, this list of conditions and the following disclaimer.
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
12 %% 2. Redistributions in binary form must reproduce the above
13 %% copyright notice, this list of conditions and the following
14 %% disclaimer in the documentation and/or other materials provided
15 %% with the distribution.
16 %%
17 %% THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18 %% OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 %% WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 %% ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 %% DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 %% DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 %% GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 %% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 %% WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 %% NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 %% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 -module(jsonrpc).
30 -author("Gaspar Chilingarov <nm@web.am>, Gurgen Tumanyan <barbarian@armkb.com>").
15454bc @vinoski JSON-RPC 2.0 support
vinoski authored
31 -vsn("3").
183ec82 @vinoski restore removed jsonrpc:call/3
vinoski authored
32 -export([call/3]).
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
33 -export([s/2]). % extract element from proplist
34
183ec82 @vinoski restore removed jsonrpc:call/3
vinoski authored
35 %%%
36 %%% call function calls json-rpc method on remote host
37 %%%
38 %%% URL - remote server url (may use https)
39 %%% Options - option list to be passed to http:request
40 %%% (ssl options ot timeout, for example)
41 %%% Payload -> {call, MethodName, Args} tuple
42 %%% MethodName -> atom
43 %%% Args -> list
44 %%%
45 call(URL, Options, Payload) ->
46 try
47 {ok, CallPayloadDeep} = encode_call_payload(Payload),
48 CallPayload = lists:flatten(CallPayloadDeep),
49 {ok, Response} = httpc:request(
50 post,
51 {URL,[{"Content-Length",length(CallPayload)}],
52 "application/x-www-form-urlencoded",CallPayload},
53 Options, []),
54
55 RespBody = if
56 (size(Response) == 2) or (size(Response) == 3) ->
57 element(size(Response), Response)
58 end,
59 decode_call_payload(RespBody)
60 catch
61 error:Err->
62 error_logger:error_report([{'json_rpc:call', error},
63 {error, Err},
64 {stack, erlang:get_stacktrace()}]),
65 {error,Err}
66 end.
67
68 %%%
69 %%% json-rpc.org defines such structure for making call
70 %%%
71 encode_call_payload({call, Method, Args}) when is_list(Args) ->
72 %% id makes sense when there are many requests in same
73 %% communication channel and replies can come in random
74 %% order here it can be changed to something less expensive
75 ID = element(3, erlang:now()),
76 Struct = json2:encode({struct, [{"jsonrpc", "2.0"},
77 {method, Method},
78 {params, {array, Args}},
79 {id, ID}]}),
80 {ok, Struct}.
81
82 %%%
83 %%% decode response structure
84 %%%
85 decode_call_payload(JSonStr) ->
86 {ok, JSON} = json2:decode_string(JSonStr),
87 Result = s(JSON, result),
88 Error = s(JSON, error),
89 case Error of
90 undefined ->
91 {ok,{response,[Result]}}; % make it compliant with xmlrpc response
92 Error ->
93 {error, Error}
94 end.
95
455578a @vinoski major trailing whitespace cleanup
vinoski authored
96 %%% lookup element in proplist
15454bc @vinoski JSON-RPC 2.0 support
vinoski authored
97 s({struct, List}, ElemName) ->
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
98 s(List, ElemName);
99 s(List, ElemName) when is_list(List) ->
100 case lists:keysearch(ElemName,1,List) of
15454bc @vinoski JSON-RPC 2.0 support
vinoski authored
101 {value,{ElemName,Val}} ->
102 Val;
5ca422e @vinoski fix object lookup problem in jsonrpc
vinoski authored
103 false when is_atom(ElemName) ->
15454bc @vinoski JSON-RPC 2.0 support
vinoski authored
104 ElemList = atom_to_list(ElemName),
105 case lists:keysearch(ElemList,1,List) of
106 {value,{ElemList,Val}} ->
107 Val;
108 _ ->
109 undefined
110 end;
111 _ ->
112 undefined
455578a @vinoski major trailing whitespace cleanup
vinoski authored
113 end.
114
0be3c7e @klacke untabified all of yaws
authored
115
47e5a7c @klacke JSON ajax code from Gaspar Chilingarov, I added docs describing an ex…
authored
116 % vim: tabstop=4 ft=erlang
Something went wrong with that request. Please try again.