microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.Common/Http/HttpRequestOptions.cs
57lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | namespace Microsoft.Teams.Common.Http; |
| 5 | |
| 6 | using IHttpHeaders = IDictionary<string, IList<string>>; |
| 7 | |
| 8 | public interface IHttpRequestOptions |
| 9 | { |
| 10 | public IHttpHeaders Headers { get; set; } |
| 11 | |
| 12 | public void AddUserAgent(IList<string> value); |
| 13 | public void AddUserAgent(params string[] value); |
| 14 | public void AddHeader(string key, IList<string> value); |
| 15 | public void AddHeader(string key, params string[] value); |
| 16 | } |
| 17 | |
| 18 | public class HttpRequestOptions : IHttpRequestOptions |
| 19 | { |
| 20 | public IHttpHeaders Headers { get; set; } = new Dictionary<string, IList<string>>(); |
| 21 | |
| 22 | public void AddUserAgent(IList<string> value) |
| 23 | { |
| 24 | AddHeader("User-Agent", value); |
| 25 | } |
| 26 | |
| 27 | public void AddUserAgent(params string[] value) |
| 28 | { |
| 29 | AddHeader("User-Agent", value); |
| 30 | } |
| 31 | |
| 32 | public void AddHeader(string key, IList<string> value) |
| 33 | { |
| 34 | Headers.TryGetValue(key, out IList<string>? values); |
| 35 | values ??= []; |
| 36 | |
| 37 | foreach (var headerValue in value) |
| 38 | { |
| 39 | values.Add(headerValue); |
| 40 | } |
| 41 | |
| 42 | Headers.Add(key, values); |
| 43 | } |
| 44 | |
| 45 | public void AddHeader(string key, params string[] value) |
| 46 | { |
| 47 | Headers.TryGetValue(key, out IList<string>? values); |
| 48 | values ??= []; |
| 49 | |
| 50 | foreach (var headerValue in value) |
| 51 | { |
| 52 | values.Add(headerValue); |
| 53 | } |
| 54 | |
| 55 | Headers.Add(key, values); |
| 56 | } |
| 57 | } |