/// <summary> /// http请求之前 /// </summary> /// <param name="context">上下文</param> /// <param name="parameter">特性关联的参数</param> /// <exception cref="HttpApiConfigException"></exception> /// <returns></returns> public Task BeforeRequestAsync(ApiActionContext context, ApiParameterDescriptor parameter) { if (parameter.Value == null) { throw new ArgumentNullException(parameter.Name); } if (parameter.Index > 0) { throw new HttpApiConfigException(this.GetType().Name + "必须修饰于第一个参数"); } var relative = new Uri(parameter.ToString(), UriKind.RelativeOrAbsolute); if (relative.IsAbsoluteUri == true) { context.RequestMessage.RequestUri = relative; } else { var baseUri = context.RequestMessage.RequestUri; if (baseUri == null) { throw new HttpApiConfigException($"请配置{nameof(HttpHostAttribute)}或者Url使用绝对路径"); } context.RequestMessage.RequestUri = new Uri(baseUri, relative); } return(ApiTask.CompletedTask); }
/// <summary> /// http请求之前 /// </summary> /// <param name="context">上下文</param> /// <param name="parameter">特性关联的参数</param> /// <returns></returns> async Task IApiParameterAttribute.BeforeRequestAsync(ApiActionContext context, ApiParameterDescriptor parameter) { if (this.IsIgnoreWith(parameter) == false) { await context.RequestMessage.AddFormFieldAsync(parameter.Name, parameter.ToString()).ConfigureAwait(false); } }
/// <summary> /// http请求之前 /// </summary> /// <param name="context">上下文</param> /// <param name="parameter">特性关联的参数</param> /// <returns></returns> void IApiParameterAttribute.BeforeRequestAsync(ApiActionContext context, ApiParameterDescriptor parameter) { if (this.WillIgnore(parameter.Value) == false) { context.RequestMessage.AddFormFieldAsync(parameter.Name, parameter.ToString()); } }
/// <summary> /// http请求之前 /// </summary> /// <param name="context">上下文</param> /// <param name="parameter">特性关联的参数</param> /// <returns></returns> async Task IApiParameterAttribute.BeforeRequestAsync(ApiActionContext context, ApiParameterDescriptor parameter) { if (this.WillIgnore(parameter.Value) == false) { context.RequestMessage.AddMulitpartText(parameter.Name, parameter.ToString()); await ApiTask.CompletedTask; } }
/// <summary> /// 设置参数到http请求内容 /// </summary> /// <param name="context">上下文</param> /// <param name="parameter">特性关联的参数</param> /// <exception cref="HttpApiConfigException"></exception> /// <returns></returns> protected override async Task SetHttpContentAsync(ApiActionContext context, ApiParameterDescriptor parameter) { var form = parameter.ToString(); var httpContent = await UrlEncodedContent.FromHttpContentAsync(context.RequestMessage.Content).ConfigureAwait(false); await httpContent.AddRawFormAsync(form).ConfigureAwait(false); context.RequestMessage.Content = httpContent; }
/// <summary> /// http请求之前 /// </summary> /// <param name="context">上下文</param> /// <param name="parameter">特性关联的参数</param> /// <returns></returns> public virtual Task BeforeRequestAsync(ApiActionContext context, ApiParameterDescriptor parameter) { if (parameter.Value == null) { throw new ArgumentNullException(parameter.Name); } var url = new Uri(parameter.ToString(), UriKind.RelativeOrAbsolute); if (url.IsAbsoluteUri == true) { context.RequestMessage.RequestUri = url; } else { var baseUrl = context.RequestMessage.RequestUri; context.RequestMessage.RequestUri = new Uri(baseUrl, url); } return(ApiTask.CompletedTask); }
public void NewTest() { var p1 = typeof(IAubTestApi).GetMethod("PostAsync").GetParameters()[0]; var m1 = new ApiParameterDescriptor(p1); Assert.True(m1.Attributes.Count == 1); Assert.True(m1.Name == p1.Name); Assert.True(m1.Index == 0); Assert.True(m1.Value == null); Assert.Null(m1.Value); Assert.Null(m1.ToString()); Assert.True(m1.ParameterAttribute is QueryAttribute); var p2 = typeof(IAubTestApi).GetMethod("PostAsync").GetParameters()[1]; var m2 = new ApiParameterDescriptor(p2); Assert.True(m2.Attributes.Count == 1); Assert.True(m2.Name == p2.Name); Assert.True(m2.Index == 1); Assert.True(m2.Value == null); }
/// <summary> /// http请求之前 /// 值从参数过来 /// </summary> /// <param name="context">上下文</param> /// <param name="parameter">特性关联的参数</param> /// <returns></returns> Task IApiParameterAttribute.BeforeRequestAsync(ApiActionContext context, ApiParameterDescriptor parameter) { this.SetHeaderValue(context, parameter.ToString()); return(ApiTask.CompletedTask); }
/// <summary> /// http请求之前 /// </summary> /// <param name="context">上下文</param> /// <param name="parameter">特性关联的参数</param> /// <returns></returns> async Task IApiParameterAttribute.BeforeRequestAsync(ApiActionContext context, ApiParameterDescriptor parameter) { await context.RequestMessage.AddFieldAsync(parameter.Name, parameter.ToString()); }
/// <summary> /// 设置参数到http请求内容 /// </summary> /// <param name="context">上下文</param> /// <param name="parameter">特性关联的参数</param> protected override void SetHttpContent(ApiActionContext context, ApiParameterDescriptor parameter) { var content = parameter.ToString(); context.RequestMessage.Content = new StringContent(content ?? string.Empty, this.encoding, this.mediaType); }
/// <summary> /// http请求之前 /// 值从参数过来 /// </summary> /// <param name="context">上下文</param> /// <param name="parameter">特性关联的参数</param> /// <returns></returns> void IApiParameterAttribute.BeforeRequestAsync(ApiActionContext context, ApiParameterDescriptor parameter) { this.SetHeaderValue(context, parameter.ToString()); }
/// <summary> /// http请求之前 /// </summary> /// <param name="context">上下文</param> /// <param name="parameter">特性关联的参数</param> /// <returns></returns> async Task IApiParameterAttribute.BeforeRequestAsync(ApiActionContext context, ApiParameterDescriptor parameter) { context.RequestMessage.AddText(parameter.Name, parameter.ToString()); await ApiTask.CompletedTask; }