/// <summary> /// Adds an authorization header. /// </summary> /// <param name="requestBuilder">A request builder.</param> /// <param name="methodInfo">A method info.</param> /// <param name="names">The methods parameter names.</param> /// <param name="arguments">The methods arguments.</param> /// <param name="serviceProvider">A <see cref="IServiceProvider"/> instance.</param> /// <returns>The request builder.</returns> public static async Task <HttpRequestBuilder> AddAuthorizationHeaderAsync( this HttpRequestBuilder requestBuilder, MethodInfo methodInfo, IEnumerable <string> names, IEnumerable <object> arguments, IServiceProvider serviceProvider) { var authAttr = methodInfo.GetCustomAttribute <AddAuthorizationHeaderAttribute>() ?? methodInfo.DeclaringType.GetCustomAttribute <AddAuthorizationHeaderAttribute>(); if (authAttr != null) { if (authAttr.HeaderValue != null) { requestBuilder.AddHeader( "Authorization", authAttr.HeaderValue.ExpandString(names, arguments)); } else { var authFactoryType = authAttr.AuthorizationFactoryType ?? typeof(IAuthorizationHeaderFactory); var authFactory = serviceProvider?.GetService(authFactoryType) as IAuthorizationHeaderFactory; if (authFactory != null) { requestBuilder.AddAuthorizationHeader( authFactory.GetAuthorizationHeaderScheme(), await authFactory.GetAuthorizationHeaderValueAsync()); } } } return(requestBuilder); }
/// <summary> /// Checks a parameter for any <see cref="SendAsHeaderAttribute"/> attributes. /// </summary> /// <param name="requestBuilder">A request builder instance.</param> /// <param name="attrs">The parameters attributes.</param> /// <param name="argument">The parameters actual value.</param> /// <returns>The request builder instance.</returns> internal static HttpRequestBuilder CheckParameterForSendAsHeader( this HttpRequestBuilder requestBuilder, IEnumerable <Attribute> attrs, object argument) { void AddHeader(string name, string format, object arg) { if (string.IsNullOrEmpty(format) == false) { requestBuilder.AddHeader( name, string.Format(format, arg.ToString())); } else { requestBuilder.AddHeader( name, arg.ToString()); } } foreach (var attr in attrs.OfType <SendAsHeaderAttribute>()) { if (typeof(IEnumerable <string>).IsAssignableFrom(argument.GetType()) == true) { foreach (var item in (IEnumerable <string>)argument) { AddHeader(attr.Name, attr.Format, item); } } else { AddHeader(attr.Name, attr.Format, argument); } } return(requestBuilder); }
/// <summary> /// Adds the method headers to a <see cref="HttpRequestBuilder"/>. /// </summary> /// <param name="requestBuilder">A <see cref="HttpRequestBuilder"/> instance.</param> /// <param name="method">The <see cref="MethodInfo"/>.</param> /// <param name="names">A list of keys.</param> /// <param name="values">A list of values.</param> /// <returns>The <see cref="HttpRequestBuilder"/> instance.</returns> public static HttpRequestBuilder AddMethodHeaders( this HttpRequestBuilder requestBuilder, MethodInfo method, IEnumerable <string> names, IEnumerable <object> values) { var headerAttrs = method .GetCustomAttributes <AddHeaderAttribute>() .Union( method.DeclaringType.GetCustomAttributes <AddHeaderAttribute>()); if (headerAttrs.Any() == true) { foreach (var attr in headerAttrs) { requestBuilder.AddHeader( attr.Header, attr.Value.ExpandString(names, values)); } } return(requestBuilder); }