summaryrefslogtreecommitdiffstats
path: root/src/jaegertracing/thrift/tutorial/erl/json_client.erl
blob: 0b39ceda9d101cb2b86881f67375e9f41160d7b1 (plain)
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
%%
%% Licensed to the Apache Software Foundation (ASF) under one
%% or more contributor license agreements. See the NOTICE file
%% distributed with this work for additional information
%% regarding copyright ownership. The ASF licenses this file
%% to you under the Apache License, Version 2.0 (the
%% "License"); you may not use this file except in compliance
%% with the License. You may obtain a copy of the License at
%%
%%   http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% The JSON protocol over HTTP implementation was created by
%% Peter Neumark <neumark.peter@gmail.com> based on
%% the binary protocol + socket tutorial. Use with the same server
%% that the Javascript tutorial uses!

-module(json_client).

-include("calculator_thrift.hrl").

-export([t/0]).

%% Client constructor for the http transports
%% with the json protocol
new_client(Host, Path, Service, _Options) ->
    {ProtoOpts, TransOpts} = {[],[]},
    TransportFactory = fun() -> thrift_http_transport:new(Host, Path, TransOpts) end,
    {ok, ProtocolFactory} = thrift_json_protocol:new_protocol_factory(
                              TransportFactory, ProtoOpts),
    {ok, Protocol} = ProtocolFactory(),
    thrift_client:new(Protocol, Service).

p(X) ->
    io:format("~p~n", [X]),
    ok.

t() ->
    inets:start(),
    {ok, Client0} = new_client("127.0.0.1:8088", "/thrift/service/tutorial/",
                                           calculator_thrift,
                                           []),
    {Client1, {ok, ok}} = thrift_client:call(Client0, ping, []),
    io:format("ping~n", []),

    {Client2, {ok, Sum}} = thrift_client:call(Client1, add,  [1, 1]),
    io:format("1+1=~p~n", [Sum]),

    {Client3, {ok, Sum1}} = thrift_client:call(Client2, add, [1, 4]),
    io:format("1+4=~p~n", [Sum1]),

    Work = #'Work'{op=?TUTORIAL_OPERATION_SUBTRACT,
                 num1=15,
                 num2=10},
    {Client4, {ok, Diff}} = thrift_client:call(Client3, calculate, [1, Work]),
    io:format("15-10=~p~n", [Diff]),

    {Client5, {ok, Log}} = thrift_client:call(Client4, getStruct, [1]),
    io:format("Log: ~p~n", [Log]),

    Client6 =
        try
            Work1 = #'Work'{op=?TUTORIAL_OPERATION_DIVIDE,
                          num1=1,
                          num2=0},
            {ClientS1, {ok, _Quot}} = thrift_client:call(Client5, calculate, [2, Work1]),

            io:format("LAME: exception handling is broken~n", []),
            ClientS1
        catch
            throw:{ClientS2, Z} ->
                io:format("Got exception where expecting - the " ++
                          "following is NOT a problem!!!~n"),
                p(Z),
                ClientS2
        end,


    {Client7, {ok, ok}} = thrift_client:call(Client6, zip, []),
    io:format("zip~n", []),

    {_Client8, ok} = thrift_client:close(Client7),
    ok.