blob: b5d0b4e3987b82be6786c80fb49fda53dabb250e [file] [log] [blame]
Jan Tattermuscha7fff862015-02-13 11:08:08 -08001#region Copyright notice and license
Jan Tattermusch7897ae92017-06-07 22:57:36 +02002// Copyright 2015-2016 gRPC authors.
Craig Tiller190d3602015-02-18 09:23:38 -08003//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02004// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
Craig Tiller190d3602015-02-18 09:23:38 -08007//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02008// http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller190d3602015-02-18 09:23:38 -08009//
Jan Tattermusch7897ae92017-06-07 22:57:36 +020010// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
Jan Tattermuscha7fff862015-02-13 11:08:08 -080015#endregion
16
Jan Tattermuscha7608b02015-02-03 17:54:38 -080017using System;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080018using System.Collections.Generic;
Jan Tattermusch30868622015-02-19 09:22:33 -080019using System.Threading.Tasks;
Jan Tattermuschd6a83972016-05-10 16:38:23 -070020using Grpc.Core;
Jan Tattermusch30868622015-02-19 09:22:33 -080021using Grpc.Core.Utils;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080022
Jan Tattermusch28526312015-08-03 09:21:38 -070023namespace Math
Jan Tattermuscha7608b02015-02-03 17:54:38 -080024{
Jan Tattermusch13cd1252015-03-06 10:11:50 -080025 public static class MathExamples
26 {
Jan Tattermusch809148d2016-03-22 15:09:41 -070027 public static void DivExample(Math.MathClient client)
Jan Tattermusch13cd1252015-03-06 10:11:50 -080028 {
Jan Tattermusch28526312015-08-03 09:21:38 -070029 DivReply result = client.Div(new DivArgs { Dividend = 10, Divisor = 3 });
Jan Tattermusch13cd1252015-03-06 10:11:50 -080030 Console.WriteLine("Div Result: " + result);
31 }
Jan Tattermuscha7608b02015-02-03 17:54:38 -080032
Jan Tattermusch809148d2016-03-22 15:09:41 -070033 public static async Task DivAsyncExample(Math.MathClient client)
Jan Tattermusch13cd1252015-03-06 10:11:50 -080034 {
Jan Tattermusch28526312015-08-03 09:21:38 -070035 DivReply result = await client.DivAsync(new DivArgs { Dividend = 4, Divisor = 5 });
Jan Tattermusch8c61db92015-04-02 15:22:31 -070036 Console.WriteLine("DivAsync Result: " + result);
37 }
38
Jan Tattermusch809148d2016-03-22 15:09:41 -070039 public static async Task FibExample(Math.MathClient client)
Jan Tattermusch13cd1252015-03-06 10:11:50 -080040 {
Jan Tattermusch28526312015-08-03 09:21:38 -070041 using (var call = client.Fib(new FibArgs { Limit = 5 }))
Jan Tattermusch9f550a32015-05-18 16:00:36 -070042 {
Jan Tattermuschf22abfb2015-08-09 16:15:34 -070043 List<Num> result = await call.ResponseStream.ToListAsync();
Jan Tattermusch9f550a32015-05-18 16:00:36 -070044 Console.WriteLine("Fib Result: " + string.Join("|", result));
45 }
Jan Tattermusch13cd1252015-03-06 10:11:50 -080046 }
Jan Tattermuscha7608b02015-02-03 17:54:38 -080047
Jan Tattermusch809148d2016-03-22 15:09:41 -070048 public static async Task SumExample(Math.MathClient client)
Jan Tattermusch13cd1252015-03-06 10:11:50 -080049 {
Jan Tattermusch8c61db92015-04-02 15:22:31 -070050 var numbers = new List<Num>
Jan Tattermusch075dde42015-03-11 18:21:00 -070051 {
Jan Tattermusch28526312015-08-03 09:21:38 -070052 new Num { Num_ = 1 },
53 new Num { Num_ = 2 },
54 new Num { Num_ = 3 }
Jan Tattermusch13cd1252015-03-06 10:11:50 -080055 };
Craig Tiller190d3602015-02-18 09:23:38 -080056
Jan Tattermusche5c9b802015-07-14 17:46:58 -070057 using (var call = client.Sum())
Jan Tattermusch9f550a32015-05-18 16:00:36 -070058 {
Jan Tattermuschf22abfb2015-08-09 16:15:34 -070059 await call.RequestStream.WriteAllAsync(numbers);
Jan Tattermuscha236ff22015-07-21 12:33:31 -070060 Console.WriteLine("Sum Result: " + await call.ResponseAsync);
Jan Tattermusch9f550a32015-05-18 16:00:36 -070061 }
Jan Tattermusch13cd1252015-03-06 10:11:50 -080062 }
Jan Tattermuscha7608b02015-02-03 17:54:38 -080063
Jan Tattermusch809148d2016-03-22 15:09:41 -070064 public static async Task DivManyExample(Math.MathClient client)
Jan Tattermusch13cd1252015-03-06 10:11:50 -080065 {
Jan Tattermusch8c61db92015-04-02 15:22:31 -070066 var divArgsList = new List<DivArgs>
Jan Tattermusch13cd1252015-03-06 10:11:50 -080067 {
Jan Tattermusch28526312015-08-03 09:21:38 -070068 new DivArgs { Dividend = 10, Divisor = 3 },
69 new DivArgs { Dividend = 100, Divisor = 21 },
70 new DivArgs { Dividend = 7, Divisor = 2 }
Jan Tattermusch13cd1252015-03-06 10:11:50 -080071 };
Jan Tattermusche5c9b802015-07-14 17:46:58 -070072 using (var call = client.DivMany())
Jan Tattermusch9f550a32015-05-18 16:00:36 -070073 {
Jan Tattermuschf22abfb2015-08-09 16:15:34 -070074 await call.RequestStream.WriteAllAsync(divArgsList);
75 Console.WriteLine("DivMany Result: " + string.Join("|", await call.ResponseStream.ToListAsync()));
Jan Tattermusch9f550a32015-05-18 16:00:36 -070076 }
Jan Tattermusch13cd1252015-03-06 10:11:50 -080077 }
Jan Tattermuscha7608b02015-02-03 17:54:38 -080078
Jan Tattermusch809148d2016-03-22 15:09:41 -070079 public static async Task DependendRequestsExample(Math.MathClient client)
Jan Tattermusch13cd1252015-03-06 10:11:50 -080080 {
Jan Tattermusch8c61db92015-04-02 15:22:31 -070081 var numbers = new List<Num>
Jan Tattermusch075dde42015-03-11 18:21:00 -070082 {
Jan Tattermusch28526312015-08-03 09:21:38 -070083 new Num { Num_ = 1 },
84 new Num { Num_ = 2 },
85 new Num { Num_ = 3 }
Jan Tattermusch13cd1252015-03-06 10:11:50 -080086 };
Jan Tattermuscha7608b02015-02-03 17:54:38 -080087
Jan Tattermusch9f550a32015-05-18 16:00:36 -070088 Num sum;
Jan Tattermusche5c9b802015-07-14 17:46:58 -070089 using (var sumCall = client.Sum())
Jan Tattermusch9f550a32015-05-18 16:00:36 -070090 {
Jan Tattermuschf22abfb2015-08-09 16:15:34 -070091 await sumCall.RequestStream.WriteAllAsync(numbers);
Jan Tattermuscha236ff22015-07-21 12:33:31 -070092 sum = await sumCall.ResponseAsync;
Jan Tattermusch9f550a32015-05-18 16:00:36 -070093 }
Jan Tattermusch8c61db92015-04-02 15:22:31 -070094
Jan Tattermusch28526312015-08-03 09:21:38 -070095 DivReply result = await client.DivAsync(new DivArgs { Dividend = sum.Num_, Divisor = numbers.Count });
Jan Tattermusch8c61db92015-04-02 15:22:31 -070096 Console.WriteLine("Avg Result: " + result);
Jan Tattermusch13cd1252015-03-06 10:11:50 -080097 }
Jan Tattermuschd6a83972016-05-10 16:38:23 -070098
99 /// <summary>
100 /// Shows how to handle a call ending with non-OK status.
101 /// </summary>
102 public static async Task HandleErrorExample(Math.MathClient client)
103 {
104 try
105 {
106 DivReply result = await client.DivAsync(new DivArgs { Dividend = 5, Divisor = 0 });
107 }
108 catch (RpcException ex)
109 {
110 Console.WriteLine(string.Format("RPC ended with status {0}", ex.Status));
111 }
112 }
113
114 /// <summary>
115 /// Shows how to send request headers and how to access response headers
116 /// and response trailers.
117 /// </summary>
118 public static async Task MetadataExample(Math.MathClient client)
119 {
120 var requestHeaders = new Metadata
121 {
122 { "custom-header", "custom-value" }
123 };
124
125 var call = client.DivAsync(new DivArgs { Dividend = 5, Divisor = 0 }, requestHeaders);
126
127 // Get response headers
128 Metadata responseHeaders = await call.ResponseHeadersAsync;
129
130 var result = await call;
131
132 // Get response trailers after the call has finished.
133 Metadata responseTrailers = call.GetTrailers();
134 }
Jan Tattermusch13cd1252015-03-06 10:11:50 -0800135 }
Jan Tattermuscha7608b02015-02-03 17:54:38 -0800136}