ConnectorClientEx.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Net;
4 using System.Net.Http;
5 using System.Net.Http.Headers;
6 using System.Reflection;
7 
8 using Microsoft.Rest;
9 
10 namespace Microsoft.Bot.Connector
11 {
12  public partial class ConnectorClient
13  {
21  public ConnectorClient(Uri baseUri, string microsoftAppId = null, string microsoftAppPassword = null, params DelegatingHandler[] handlers)
22  : this(baseUri, new MicrosoftAppCredentials(microsoftAppId, microsoftAppPassword), handlers: handlers)
23  {
24  }
25 
33  public ConnectorClient(Uri baseUri, MicrosoftAppCredentials credentials, bool addJwtTokenRefresher = true, params DelegatingHandler[] handlers)
34  : this(baseUri, addJwtTokenRefresher ? AddJwtTokenRefresher(handlers, credentials) : handlers)
35  {
36  this.Credentials = credentials;
37  }
38 
39  private static DelegatingHandler[] AddJwtTokenRefresher(DelegatingHandler[] srcHandlers, MicrosoftAppCredentials credentials)
40  {
41  var handlers = new List<DelegatingHandler>(srcHandlers);
42  handlers.Add(new JwtTokenRefresher(credentials));
43  return handlers.ToArray();
44  }
45 
46  // client defaults to sending the expect: continue header, which isn't very efficient,
47  partial void CustomInitialize()
48  {
49  AddUserAgent(this);
50  HttpClient.DefaultRequestHeaders.ExpectContinue = false;
51  }
52 
53  internal static void AddUserAgent<T>(T client) where T : ServiceClient<T>
54  {
55  client.HttpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("Microsoft-BotFramework", "3.1"));
56  client.HttpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue($"(BotBuilder .Net/{GetClientVersion(client)})"));
57  }
58 
59  internal static string GetClientVersion<T>(T client) where T : ServiceClient<T>
60  {
61  var type = client.GetType();
62  var assembly = type.GetTypeInfo().Assembly;
63  return assembly.GetName().Version.ToString();
64  }
65  }
66 }
ConnectorClient(Uri baseUri, string microsoftAppId=null, string microsoftAppPassword=null, params DelegatingHandler[] handlers)
Create a new instance of the ConnectorClient class
ConnectorClient(Uri baseUri, MicrosoftAppCredentials credentials, bool addJwtTokenRefresher=true, params DelegatingHandler[] handlers)
Create a new instance of the ConnectorClient class