4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
28 "github.com/apache/thrift/lib/go/thrift"
31 var defaultCtx = context.Background()
33 func handleClient(client *tutorial.CalculatorClient) (err error) {
34 client.Ping(defaultCtx)
37 sum, _ := client.Add(defaultCtx, 1, 1)
38 fmt.Print("1+1=", sum, "\n")
40 work := tutorial.NewWork()
41 work.Op = tutorial.Operation_DIVIDE
44 quotient, err := client.Calculate(defaultCtx, 1, work)
46 switch v := err.(type) {
47 case *tutorial.InvalidOperation:
48 fmt.Println("Invalid operation:", v)
50 fmt.Println("Error during operation:", err)
54 fmt.Println("Whoa we can divide by 0 with new value:", quotient)
57 work.Op = tutorial.Operation_SUBTRACT
60 diff, err := client.Calculate(defaultCtx, 1, work)
62 switch v := err.(type) {
63 case *tutorial.InvalidOperation:
64 fmt.Println("Invalid operation:", v)
66 fmt.Println("Error during operation:", err)
70 fmt.Print("15-10=", diff, "\n")
73 log, err := client.GetStruct(defaultCtx, 1)
75 fmt.Println("Unable to get struct:", err)
78 fmt.Println("Check log:", log.Value)
83 func runClient(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory, addr string, secure bool) error {
84 var transport thrift.TTransport
87 cfg := new(tls.Config)
88 cfg.InsecureSkipVerify = true
89 transport, err = thrift.NewTSSLSocket(addr, cfg)
91 transport, err = thrift.NewTSocket(addr)
94 fmt.Println("Error opening socket:", err)
97 transport, err = transportFactory.GetTransport(transport)
101 defer transport.Close()
102 if err := transport.Open(); err != nil {
105 iprot := protocolFactory.GetProtocol(transport)
106 oprot := protocolFactory.GetProtocol(transport)
107 return handleClient(tutorial.NewCalculatorClient(thrift.NewTStandardClient(iprot, oprot)))