using NCC.ClayObject.Extensions;
using NCC.JsonSerialization;
using NCC.Templates.Extensions;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
namespace NCC.RemoteRequest
{
///
/// HttpClient 对象组装部件
///
public sealed partial class HttpRequestPart
{
///
/// 设置请求地址
///
///
///
public HttpRequestPart SetRequestUrl(string requestUrl)
{
if (string.IsNullOrWhiteSpace(requestUrl)) return this;
// 解决配置 BaseAddress 客户端后,地址首位斜杆问题
requestUrl = requestUrl.StartsWith("/") ? requestUrl[1..] : requestUrl;
// 支持读取配置渲染
RequestUrl = requestUrl.Render();
return this;
}
///
/// 设置 URL 模板
///
///
///
public HttpRequestPart SetTemplates(IDictionary templates)
{
if (templates != null) Templates = templates;
return this;
}
///
/// 设置 URL 模板
///
///
///
public HttpRequestPart SetTemplates(object templates)
{
return SetTemplates(templates.ToDictionary());
}
///
/// 设置请求方法
///
///
///
public HttpRequestPart SetHttpMethod(HttpMethod httpMethod)
{
if (httpMethod != null) Method = httpMethod;
return this;
}
///
/// 设置请求报文头
///
///
///
public HttpRequestPart SetHeaders(IDictionary headers)
{
if (headers != null) Headers = headers;
return this;
}
///
/// 设置请求报文头
///
///
///
public HttpRequestPart SetHeaders(object headers)
{
return SetHeaders(headers.ToDictionary());
}
///
/// 设置 URL 参数
///
///
///
public HttpRequestPart SetQueries(IDictionary queries)
{
if (queries != null) Queries = queries;
return this;
}
///
/// 设置 URL 参数
///
///
///
public HttpRequestPart SetQueries(object queries)
{
return SetQueries(queries.ToDictionary());
}
///
/// 设置客户端分类名
///
///
///
public HttpRequestPart SetClient(string name)
{
if (!string.IsNullOrWhiteSpace(name)) ClientName = name;
return this;
}
///
/// 设置内容类型
///
///
///
public HttpRequestPart SetContentType(string contentType)
{
if (!string.IsNullOrWhiteSpace(contentType)) ContentType = contentType;
return this;
}
///
/// 设置内容编码
///
///
///
public HttpRequestPart SetContentEncoding(Encoding encoding)
{
if (encoding != null) ContentEncoding = encoding;
return this;
}
///
/// 设置 Body 内容
///
///
///
///
///
public HttpRequestPart SetBody(object body, string contentType = default, Encoding encoding = default)
{
if (body != null) Body = body;
SetContentType(contentType).SetContentEncoding(encoding);
return this;
}
///
/// 设置 Body Bytes
///
///
///
public HttpRequestPart SetBodyBytes(params (string Name, byte[] Bytes, string FileName)[] bytesData)
{
BodyBytes ??= new List<(string Name, byte[] Bytes, string FileName)>();
if (bytesData != null && bytesData.Length > 0) BodyBytes.AddRange(bytesData);
return this;
}
///
/// 设置超时时间(秒)
///
///
///
public HttpRequestPart SetClientTimeout(long timeout)
{
if (timeout > 0) Timeout = timeout;
return this;
}
///
/// 设置 JSON 序列化提供器
///
///
///
///
public HttpRequestPart SetJsonSerialization(Type providerType, object jsonSerializerOptions = default)
{
if (providerType != null) JsonSerializerProvider = providerType;
if (jsonSerializerOptions != null) JsonSerializerOptions = jsonSerializerOptions;
return this;
}
///
/// 设置 JSON 序列化提供器
///
///
///
///
public HttpRequestPart SetJsonSerialization(object jsonSerializerOptions = default)
where TJsonSerializationProvider : IJsonSerializerProvider
{
return SetJsonSerialization(typeof(TJsonSerializationProvider), jsonSerializerOptions);
}
///
/// 是否启用验证状态
///
///
///
///
public HttpRequestPart SetValidationState(bool enabled = true, bool includeNull = true)
{
ValidationState = (enabled, includeNull);
return this;
}
///
/// 构建请求对象拦截器
///
///
///
public HttpRequestPart OnRequesting(Action action)
{
if (action == null) return this;
if (!RequestInterceptors.Contains(action)) RequestInterceptors.Add(action);
return this;
}
///
/// 创建客户端对象拦截器
///
///
///
public HttpRequestPart OnClientCreating(Action action)
{
if (action == null) return this;
if (!HttpClientInterceptors.Contains(action)) HttpClientInterceptors.Add(action);
return this;
}
///
/// 请求成功拦截器
///
///
///
public HttpRequestPart OnResponsing(Action action)
{
if (action == null) return this;
if (!ResponseInterceptors.Contains(action)) ResponseInterceptors.Add(action);
return this;
}
///
/// 请求异常拦截器
///
///
///
public HttpRequestPart OnException(Action action)
{
if (action == null) return this;
if (!ExceptionInterceptors.Contains(action)) ExceptionInterceptors.Add(action);
return this;
}
///
/// 设置请求作用域
///
///
///
public HttpRequestPart SetRequestScoped(IServiceProvider serviceProvider)
{
if (serviceProvider != null) RequestScoped = serviceProvider;
return this;
}
///
/// 配置重试策略
///
///
/// 每次延迟时间(毫秒)
///
public HttpRequestPart SetRetryPolicy(int numRetries, int retryTimeout = 1000)
{
RetryPolicy = (numRetries, retryTimeout);
return this;
}
}
}