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