microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
mehak/logger

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

Libraries/Microsoft.Teams.Common/Http/HttpClientOptions.cs

119lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4namespace Microsoft.Teams.Common.Http;
5
6/// <summary>
7/// Http Client Options
8/// </summary>
9public interface IHttpClientOptions : IHttpRequestOptions
10{
11 /// <summary>
12 /// The client name
13 /// </summary>
14 public string? Name { get; set; }
15
16 /// <summary>
17 /// The authorization token to use
18 /// </summary>
19 public object? Token { get; set; }
20
21 /// <summary>
22 /// The authorization token factory to use
23 /// </summary>
24 public HttpTokenFactory? TokenFactory { get; set; }
25
26 /// <summary>
27 /// Default request timeout (ms)
28 /// </summary>
29 public TimeSpan? Timeout { get; set; }
30
31 /// <summary>
32 /// apply options to an http client
33 /// </summary>
34 /// <param name="client">the client to apply the http options to</param>
35 public void Apply(System.Net.Http.HttpClient client);
36
37 /// <summary>
38 /// apply options to an http request
39 /// </summary>
40 /// <param name="request">the request to apply the http options to</param>
41 public void Apply(HttpRequestMessage request);
42
43 /// <summary>
44 /// a factory for adding a token to http requests
45 /// </summary>
46 public delegate object? HttpTokenFactory();
47}
48
49/// <summary>
50/// Http Client Options
51/// </summary>
52public class HttpClientOptions : HttpRequestOptions, IHttpClientOptions
53{
54 /// <summary>
55 /// The client name
56 /// </summary>
57 public string? Name { get; set; }
58
59 /// <summary>
60 /// The authorization token to use
61 /// </summary>
62 public object? Token { get; set; }
63
64 /// <summary>
65 /// The authorization token factory to use
66 /// </summary>
67 public IHttpClientOptions.HttpTokenFactory? TokenFactory { get; set; }
68
69 /// <summary>
70 /// Default request timeout (ms)
71 /// </summary>
72 public TimeSpan? Timeout { get; set; }
73
74 /// <summary>
75 /// apply options to an http client
76 /// </summary>
77 /// <param name="client">the client to apply the http options to</param>
78 public void Apply(System.Net.Http.HttpClient client)
79 {
80 if (Timeout is not null)
81 client.Timeout = (TimeSpan)Timeout;
82
83 if (Token is not null)
84 client.DefaultRequestHeaders.Add("Authorization", $"Bearer {Token}");
85
86 foreach (var kv in Headers)
87 {
88 client.DefaultRequestHeaders.TryAddWithoutValidation(kv.Key, kv.Value);
89 }
90 }
91
92 /// <summary>
93 /// apply options to an http request
94 /// </summary>
95 /// <param name="request">the request to apply the http options to</param>
96 public void Apply(HttpRequestMessage request)
97 {
98 if (TokenFactory is not null)
99 {
100 var token = TokenFactory();
101
102 if (token is not null)
103 {
104 request.Headers.Authorization = new("Bearer", token.ToString());
105 }
106 }
107
108 foreach (var kv in Headers)
109 {
110 if (kv.Key.StartsWith("Content-"))
111 {
112 request.Content?.Headers.TryAddWithoutValidation(kv.Key, kv.Value);
113 continue;
114 }
115
116 request.Headers.TryAddWithoutValidation(kv.Key, kv.Value);
117 }
118 }
119}