Skip to content

Commit

Permalink
增加HttpApiRequestMessage类,提高程序集外部扩展性
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Oct 8, 2017
1 parent e66864f commit 5f1f6e4
Show file tree
Hide file tree
Showing 25 changed files with 529 additions and 562 deletions.
8 changes: 2 additions & 6 deletions WebApiClient/Attributes/FormFieldAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ public override async Task BeforeRequestAsync(ApiActionContext context)
throw new NotSupportedException("请传入name和value参数:" + this.GetType().Name);
}

var keyValue = new KeyValuePair<string, string>(this.name, this.value);
var httpContent = await context.EnsureNoGet().RequestMessage.Content.MergeKeyValuesAsync(new[] { keyValue });
context.RequestMessage.Content = httpContent;
await context.RequestMessage.AddFieldAsync(this.name, this.value);
}

/// <summary>
Expand All @@ -74,9 +72,7 @@ public override async Task BeforeRequestAsync(ApiActionContext context)
async Task IApiParameterAttribute.BeforeRequestAsync(ApiActionContext context, ApiParameterDescriptor parameter)
{
var stringValue = parameter.Value == null ? null : parameter.Value.ToString();
var keyValue = new KeyValuePair<string, string>(parameter.Name, stringValue);
var httpContent = await context.EnsureNoGet().RequestMessage.Content.MergeKeyValuesAsync(new[] { keyValue });
context.RequestMessage.Content = httpContent;
await context.RequestMessage.AddFieldAsync(parameter.Name, stringValue);
}
}
}
11 changes: 2 additions & 9 deletions WebApiClient/Attributes/HeaderAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
Expand All @@ -12,6 +13,7 @@ namespace WebApiClient.Attributes
/// 表示Http请求Header的特性
/// </summary>
[AttributeUsage(AttributeTargets.Interface | AttributeTargets.Method | AttributeTargets.Parameter, AllowMultiple = true, Inherited = true)]
[DebuggerDisplay("{name} = {value}")]
public class HeaderAttribute : ApiActionAttribute, IApiParameterAttribute
{
/// <summary>
Expand Down Expand Up @@ -190,14 +192,5 @@ private IEnumerable<Cookie> GetCookies(string cookieValues)
let encode = HttpUtility.UrlEncode(value, Encoding.UTF8)
select new Cookie(name, encode);
}

/// <summary>
/// 转换为字符串
/// </summary>
/// <returns></returns>
public override string ToString()
{
return string.Format("{0} = {1}", this.name, this.value);
}
}
}
10 changes: 2 additions & 8 deletions WebApiClient/Attributes/MulitpartTextAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ public override async Task BeforeRequestAsync(ApiActionContext context)
throw new NotSupportedException("请传入name和value参数:" + this.GetType().Name);
}

var httpContent = context.EnsureNoGet().RequestMessage.Content.CastOrCreateMultipartContent();
httpContent.AddText(this.name, this.value);
context.RequestMessage.Content = httpContent;

context.RequestMessage.AddText(this.name, this.value);
await TaskExtend.CompletedTask;
}

Expand All @@ -76,10 +73,7 @@ public override async Task BeforeRequestAsync(ApiActionContext context)
async Task IApiParameterAttribute.BeforeRequestAsync(ApiActionContext context, ApiParameterDescriptor parameter)
{
var stringValue = parameter.Value == null ? null : parameter.Value.ToString();
var httpContent = context.EnsureNoGet().RequestMessage.Content.CastOrCreateMultipartContent();
httpContent.AddText(parameter.Name, stringValue);
context.RequestMessage.Content = httpContent;

context.RequestMessage.AddText(parameter.Name, stringValue);
await TaskExtend.CompletedTask;
}
}
Expand Down
4 changes: 3 additions & 1 deletion WebApiClient/BasicAuth.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -12,6 +13,7 @@ namespace WebApiClient
/// <summary>
/// 表示将自身作为请求的基本授权
/// </summary>
[DebuggerDisplay("{Authorization} = {authValue}")]
public class BasicAuth : IApiParameterable
{
/// <summary>
Expand Down Expand Up @@ -75,7 +77,7 @@ Task IApiParameterable.BeforeRequestAsync(ApiActionContext context, ApiParameter
/// <returns></returns>
public override string ToString()
{
return this.authValue;
return string.Format("{0}: {1}", scheme, this.authValue);
}
}
}
28 changes: 2 additions & 26 deletions WebApiClient/Contexts/ApiActionContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Reflection;
Expand Down Expand Up @@ -27,36 +28,11 @@ public class ApiActionContext
/// <summary>
/// 获取关联的HttpRequestMessage
/// </summary>
public HttpRequestMessage RequestMessage { get; internal set; }
public HttpApiRequestMessage RequestMessage { get; internal set; }

/// <summary>
/// 获取关联的HttpResponseMessage
/// </summary>
public HttpResponseMessage ResponseMessage { get; internal set; }

/// <summary>
/// 转换为字符串
/// </summary>
/// <returns></returns>
public override string ToString()
{
return this.ApiActionDescriptor.ToString();
}

/// <summary>
/// 确保不是get之类的请求
/// </summary>
/// <exception cref="NotSupportedException"></exception>
/// <returns></returns>
internal ApiActionContext EnsureNoGet()
{
var method = this.RequestMessage.Method;
if (method == HttpMethod.Get || method == HttpMethod.Head)
{
var message = string.Format("{0}方法不支持使用{1}", method, this.GetType().Name);
throw new NotSupportedException(message);
}
return this;
}
}
}
11 changes: 2 additions & 9 deletions WebApiClient/Contexts/ApiActionDescriptor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Text;
Expand All @@ -12,6 +13,7 @@ namespace WebApiClient.Contexts
/// <summary>
/// 表示请求Api描述
/// </summary>
[DebuggerDisplay("Name = {Name}")]
public class ApiActionDescriptor : ICloneable
{
/// <summary>
Expand Down Expand Up @@ -112,14 +114,5 @@ public object Clone()
Parameters = this.Parameters.Select(item => (ApiParameterDescriptor)item.Clone()).ToArray()
};
}

/// <summary>
/// 转换为字符串
/// </summary>
/// <returns></returns>
public override string ToString()
{
return this.Name;
}
}
}
123 changes: 122 additions & 1 deletion WebApiClient/Contexts/ApiParameterDescriptor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -10,6 +12,7 @@ namespace WebApiClient.Contexts
/// <summary>
/// 表示请求Api的参数描述
/// </summary>
[DebuggerDisplay("{Name} = {Value}")]
public class ApiParameterDescriptor : ICloneable
{
/// <summary>
Expand Down Expand Up @@ -73,7 +76,7 @@ public class ApiParameterDescriptor : ICloneable
/// <returns></returns>
public override string ToString()
{
return string.Format("{0} = {1}", this.Name, this.Value);
return this.Value == null ? null : this.Value.ToString();
}

/// <summary>
Expand All @@ -97,5 +100,123 @@ public object Clone()
Value = this.Value
};
}


/// <summary>
/// 格式化参数为文本
/// </summary>
/// <param name="stringFormater">广本格式化工具</param>
/// <param name="encoding">格式化编码</param>
/// <returns></returns>
public string FormatAsString(IStringFormatter stringFormater, Encoding encoding)
{
if (this.Value == null)
{
return null;
}

if (this.ParameterType == typeof(string))
{
return this.Value.ToString();
}

return stringFormater.Serialize(this.Value, encoding);
}


#region key-value format

/// <summary>
/// 格式化参数为键值对
/// </summary>
/// <returns></returns>
public IEnumerable<KeyValuePair<string, string>> FormatAsKeyValues()
{
if (this.IsSimpleType == true)
{
var kv = this.FormatAsSimple(this.Name, this.Value);
return new[] { kv };
}

if (this.IsDictionaryOfString == true)
{
return this.FormatAsDictionary<string>();
}

if (this.IsDictionaryOfObject == true)
{
return this.FormatAsDictionary<object>();
}

if (this.IsEnumerable == true)
{
return this.ForamtAsEnumerable();
}

return this.FormatAsComplex();
}

/// <summary>
/// 数组为键值对
/// </summary>
/// <returns></returns>
private IEnumerable<KeyValuePair<string, string>> ForamtAsEnumerable()
{
var array = this.Value as IEnumerable;
if (array == null)
{
return Enumerable.Empty<KeyValuePair<string, string>>();
}

return from item in array.Cast<object>()
select this.FormatAsSimple(this.Name, item);
}

/// <summary>
/// 复杂类型为键值对
/// </summary>
/// <returns></returns>
private IEnumerable<KeyValuePair<string, string>> FormatAsComplex()
{
var instance = this.Value;
if (instance == null)
{
return Enumerable.Empty<KeyValuePair<string, string>>();
}

return
from p in Property.GetProperties(this.ParameterType)
let value = p.GetValue(instance)
select this.FormatAsSimple(p.Name, value);
}

/// <summary>
/// 字典转换为键值对
/// </summary>
/// <returns></returns>
private IEnumerable<KeyValuePair<string, string>> FormatAsDictionary<TValue>()
{
var dic = this.Value as IDictionary<string, TValue>;
if (dic == null)
{
return Enumerable.Empty<KeyValuePair<string, string>>();
}

return from kv in dic select this.FormatAsSimple(kv.Key, kv.Value);
}

/// <summary>
/// 简单类型为键值对
/// </summary>
/// <param name="name">名称</param>
/// <param name="value">值</param>
/// <returns></returns>
private KeyValuePair<string, string> FormatAsSimple(string name, object value)
{
var valueString = value == null ? null : value.ToString();
return new KeyValuePair<string, string>(name, valueString);
}

#endregion
}
}
2 changes: 2 additions & 0 deletions WebApiClient/Contexts/ApiReturnDescriptor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -9,6 +10,7 @@ namespace WebApiClient.Contexts
/// <summary>
/// 表示请求Api的返回描述
/// </summary>
[DebuggerDisplay("DataType = {DataType}")]
public class ApiReturnDescriptor
{
/// <summary>
Expand Down
18 changes: 3 additions & 15 deletions WebApiClient/FormField.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
Expand All @@ -12,6 +13,7 @@ namespace WebApiClient
/// <summary>
/// 表示将自身作为x-www-form-urlencoded的字段
/// </summary>
[DebuggerDisplay("{stringValue}")]
public class FormField : IApiParameterable
{
/// <summary>
Expand Down Expand Up @@ -45,22 +47,8 @@ public FormField(string value)
/// <param name="parameter">特性关联的参数</param>
/// <returns></returns>
async Task IApiParameterable.BeforeRequestAsync(ApiActionContext context, ApiParameterDescriptor parameter)
{
var keyValue = new KeyValuePair<string, string>(parameter.Name, this.stringValue);
var httpContent = await context.EnsureNoGet().RequestMessage.Content.MergeKeyValuesAsync(new[] { keyValue });
context.RequestMessage.Content = httpContent;

await TaskExtend.CompletedTask;
}


/// <summary>
/// 转换为字符串
/// </summary>
/// <returns></returns>
public override string ToString()
{
return this.stringValue;
await context.RequestMessage.AddFieldAsync(parameter.Name, this.stringValue);
}

/// <summary>
Expand Down
Loading

0 comments on commit 5f1f6e4

Please sign in to comment.