Skip to content

Commit

Permalink
- adds support for middleware options in dotnet abstractions and core
Browse files Browse the repository at this point in the history
  • Loading branch information
baywet committed Jul 14, 2021
1 parent e05c3c0 commit fed27a4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
7 changes: 7 additions & 0 deletions abstractions/dotnet/src/IMiddlewareOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Microsoft.Kiota.Abstractions {
/// <summary>
/// Represents a middleware option.
/// </summary>
public interface IMiddlewareOption {
}
}
25 changes: 25 additions & 0 deletions abstractions/dotnet/src/RequestInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using Microsoft.Kiota.Abstractions.Serialization;
Expand All @@ -12,6 +13,30 @@ public class RequestInfo
public IDictionary<string, object> QueryParameters { get; set; } = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
public IDictionary<string, string> Headers { get; set; } = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
public Stream Content { get; set; }
private Dictionary<string, IMiddlewareOption> _middlewareOptions = new Dictionary<string, IMiddlewareOption>(StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Gets the middleware options for this request. Options are unique by type. If an option of the same type is added twice, the last one wins.
/// </summary>
public IEnumerable<IMiddlewareOption> MiddlewareOptions { get { return _middlewareOptions.Values; } }
/// <summary>
/// Adds a middleware option to the request.
/// </summary>
/// <param name="middlewareOption">The middleware option to add.</param>
public void AddMiddlewareOptions(params IMiddlewareOption[] options) {
if(!options?.Any() ?? false) throw new ArgumentNullException(nameof(options));
foreach(var option in options.Where(x => x != null))
if(!_middlewareOptions.TryAdd(option.GetType().FullName, option))
_middlewareOptions[option.GetType().FullName] = option;
}
/// <summary>
/// Removes given middleware options from the current request.
/// </summary>
/// <param name="options">Middleware options to remove.</param>
public void RemoveMiddlewareOptions(params IMiddlewareOption[] options) {
if(!options?.Any() ?? false) throw new ArgumentNullException(nameof(options));
foreach(var optionName in options.Where(x => x != null).Select(x => x.GetType().FullName))
_middlewareOptions.Remove(optionName);
}
private const string binaryContentType = "application/octet-stream";
private const string contentTypeHeader = "Content-Type";
public void SetStreamContent(Stream content) {
Expand Down
3 changes: 3 additions & 0 deletions http/dotnet/httpclient/src/HttpCore.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
Expand Down Expand Up @@ -128,6 +129,8 @@ private HttpRequestMessage GetRequestMessageFromRequestInfo(RequestInfo requestI
string.Empty)),

};
if(requestInfo.MiddlewareOptions.Any())
requestInfo.MiddlewareOptions.ToList().ForEach(x => message.Options.Set(new HttpRequestOptionsKey<IMiddlewareOption>(x.GetType().FullName), x));
if(requestInfo.Headers?.Any() ?? false)
requestInfo.Headers.Where(x => !contentTypeHeaderName.Equals(x.Key, StringComparison.OrdinalIgnoreCase)).ToList().ForEach(x => message.Headers.Add(x.Key, x.Value));
if(requestInfo.Content != null) {
Expand Down

0 comments on commit fed27a4

Please sign in to comment.