using Apewer.Network;
using System;
using System.Collections.Generic;
using System.Net;
using System.Reflection;
namespace Apewer.Web
{
/// API 调用器。
public sealed class ApiInvoker
{
#region
private static ApiInvoker _default = new ApiInvoker();
/// 默认调用器实例。
public static ApiInvoker Default { get => _default; }
#endregion
object _locker = new object();
bool _initialized = false;
/// 获取或设置要使用的 API 入口。
public ApiEntries Entries { get; set; }
/// 获取或设置 Logger。
public Logger Logger { get; set; }
/// 获取或设置选项。
public ApiOptions Options { get; set; }
/// 异常捕获程序。
public Action Catcher { get; set; }
/// 输出前的检查。
public ApiPreOutput PreOutput { get; set; }
/// 执行初始化程序,每个 ApiInvoker 实例仅执行一次初始化。
public void Initialize(Action action)
{
if (action == null) return;
lock (_locker)
{
if (_initialized) return;
action.Invoke();
_initialized = true;
}
}
/// 发起调用。
///
public void Invoke(ApiProvider provider)
{
if (provider == null) throw new ArgumentNullException(nameof(provider));
var entries = Entries;
if (entries == null) entries = ApiEntries.AppDomain();
Entries = entries;
Invoke(provider, entries);
}
/// 发起调用。
///
public void Invoke(ApiProvider provider, ApiEntries entries)
{
if (provider == null) throw new ArgumentNullException(nameof(provider));
if (entries == null) throw new ArgumentNullException(nameof(entries));
var context = new ApiContext(this, provider, entries);
var processor = new ApiProcessor(context);
processor.Run();
}
}
}