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