]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | %% |
2 | %% Licensed to the Apache Software Foundation (ASF) under one | |
3 | %% or more contributor license agreements. See the NOTICE file | |
4 | %% distributed with this work for additional information | |
5 | %% regarding copyright ownership. The ASF licenses this file | |
6 | %% to you under the Apache License, Version 2.0 (the | |
7 | %% "License"); you may not use this file except in compliance | |
8 | %% with the License. You may obtain a copy of the License at | |
9 | %% | |
10 | %% http://www.apache.org/licenses/LICENSE-2.0 | |
11 | %% | |
12 | %% Unless required by applicable law or agreed to in writing, | |
13 | %% software distributed under the License is distributed on an | |
14 | %% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | %% KIND, either express or implied. See the License for the | |
16 | %% specific language governing permissions and limitations | |
17 | %% under the License. | |
18 | %% | |
19 | %% The JSON protocol over HTTP implementation was created by | |
20 | %% Peter Neumark <neumark.peter@gmail.com> based on | |
21 | %% the binary protocol + socket tutorial. Use with the same server | |
22 | %% that the Javascript tutorial uses! | |
23 | ||
24 | -module(json_client). | |
25 | ||
26 | -include("calculator_thrift.hrl"). | |
27 | ||
28 | -export([t/0]). | |
29 | ||
30 | %% Client constructor for the http transports | |
31 | %% with the json protocol | |
32 | new_client(Host, Path, Service, _Options) -> | |
33 | {ProtoOpts, TransOpts} = {[],[]}, | |
34 | TransportFactory = fun() -> thrift_http_transport:new(Host, Path, TransOpts) end, | |
35 | {ok, ProtocolFactory} = thrift_json_protocol:new_protocol_factory( | |
36 | TransportFactory, ProtoOpts), | |
37 | {ok, Protocol} = ProtocolFactory(), | |
38 | thrift_client:new(Protocol, Service). | |
39 | ||
40 | p(X) -> | |
41 | io:format("~p~n", [X]), | |
42 | ok. | |
43 | ||
44 | t() -> | |
45 | inets:start(), | |
46 | {ok, Client0} = new_client("127.0.0.1:8088", "/thrift/service/tutorial/", | |
47 | calculator_thrift, | |
48 | []), | |
49 | {Client1, {ok, ok}} = thrift_client:call(Client0, ping, []), | |
50 | io:format("ping~n", []), | |
51 | ||
52 | {Client2, {ok, Sum}} = thrift_client:call(Client1, add, [1, 1]), | |
53 | io:format("1+1=~p~n", [Sum]), | |
54 | ||
55 | {Client3, {ok, Sum1}} = thrift_client:call(Client2, add, [1, 4]), | |
56 | io:format("1+4=~p~n", [Sum1]), | |
57 | ||
58 | Work = #'Work'{op=?TUTORIAL_OPERATION_SUBTRACT, | |
59 | num1=15, | |
60 | num2=10}, | |
61 | {Client4, {ok, Diff}} = thrift_client:call(Client3, calculate, [1, Work]), | |
62 | io:format("15-10=~p~n", [Diff]), | |
63 | ||
64 | {Client5, {ok, Log}} = thrift_client:call(Client4, getStruct, [1]), | |
65 | io:format("Log: ~p~n", [Log]), | |
66 | ||
67 | Client6 = | |
68 | try | |
69 | Work1 = #'Work'{op=?TUTORIAL_OPERATION_DIVIDE, | |
70 | num1=1, | |
71 | num2=0}, | |
72 | {ClientS1, {ok, _Quot}} = thrift_client:call(Client5, calculate, [2, Work1]), | |
73 | ||
74 | io:format("LAME: exception handling is broken~n", []), | |
75 | ClientS1 | |
76 | catch | |
77 | throw:{ClientS2, Z} -> | |
78 | io:format("Got exception where expecting - the " ++ | |
79 | "following is NOT a problem!!!~n"), | |
80 | p(Z), | |
81 | ClientS2 | |
82 | end, | |
83 | ||
84 | ||
85 | {Client7, {ok, ok}} = thrift_client:call(Client6, zip, []), | |
86 | io:format("zip~n", []), | |
87 | ||
88 | {_Client8, ok} = thrift_client:close(Client7), | |
89 | ok. |