blob: 0ba5f4f005f7b7465cf1a39ed0af8e5af73fb8fc [file] [log] [blame]
henrique31236232014-02-23 20:16:44 +01001/*
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
Cameron Martincaef0ed2025-01-15 11:58:39 +010020var thrift = require("thrift");
21var Calculator = require("./gen-nodejs/Calculator");
22var ttypes = require("./gen-nodejs/tutorial_types");
23const assert = require("assert");
henrique31236232014-02-23 20:16:44 +010024
Mark Sonnabaumc2256fc2016-08-25 09:08:47 -050025var transport = thrift.TBufferedTransport;
26var protocol = thrift.TBinaryProtocol;
henrique31236232014-02-23 20:16:44 +010027
28var connection = thrift.createConnection("localhost", 9090, {
Cameron Martincaef0ed2025-01-15 11:58:39 +010029 transport: transport,
30 protocol: protocol,
henrique31236232014-02-23 20:16:44 +010031});
32
Cameron Martincaef0ed2025-01-15 11:58:39 +010033connection.on("error", function (err) {
henrique31236232014-02-23 20:16:44 +010034 assert(false, err);
35});
36
37// Create a Calculator client with the connection
38var client = thrift.createClient(Calculator, connection);
39
Cameron Martincaef0ed2025-01-15 11:58:39 +010040client.ping().then(function () {
41 console.log("ping()");
42});
henrique31236232014-02-23 20:16:44 +010043
Cameron Martincaef0ed2025-01-15 11:58:39 +010044client.add(1, 1).then(function (response) {
45 console.log("1+1=" + response);
46});
henrique31236232014-02-23 20:16:44 +010047
48work = new ttypes.Work();
49work.op = ttypes.Operation.DIVIDE;
50work.num1 = 1;
51work.num2 = 0;
52
Cameron Martincaef0ed2025-01-15 11:58:39 +010053client
54 .calculate(1, work)
55 .then(function (message) {
56 console.log("Whoa? You know how to divide by zero?");
henrique31236232014-02-23 20:16:44 +010057 })
Cameron Martincaef0ed2025-01-15 11:58:39 +010058 .catch(function (err) {
henrique31236232014-02-23 20:16:44 +010059 console.log("InvalidOperation " + err);
60 });
61
henrique31236232014-02-23 20:16:44 +010062work.op = ttypes.Operation.SUBTRACT;
63work.num1 = 15;
64work.num2 = 10;
65
Cameron Martincaef0ed2025-01-15 11:58:39 +010066client
67 .calculate(1, work)
68 .then(function (value) {
69 console.log("15-10=" + value);
70 return client.getStruct(1);
henrique31236232014-02-23 20:16:44 +010071 })
Cameron Martincaef0ed2025-01-15 11:58:39 +010072 .then(function (message) {
73 console.log("Check log: " + message.value);
henrique31236232014-02-23 20:16:44 +010074 })
Cameron Martincaef0ed2025-01-15 11:58:39 +010075 .finally(function () {
76 //close the connection once we're done
77 connection.end();
henrique31236232014-02-23 20:16:44 +010078 });