您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

193 行
5.7 KiB

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Newtonsoft.Json;
[assembly: InternalsVisibleTo("Unity.Services.Authentication.Editor")]
[assembly: InternalsVisibleTo("Unity.Services.Utilities.Tests")]
namespace Unity.Services.Authentication.Utilities
{
interface INetworkingUtilities
{
IWebRequest<T> Get<T>(string url, IDictionary<string, string> headers = null, int maximumAttempts = 1);
IWebRequest<T> PostJson<T>(string url, object payload, IDictionary<string, string> headers = null,
int maximumAttempts = 1);
IWebRequest<T> PostForm<T>(string url, string payload, IDictionary<string, string> headers = null,
int maximumAttempts = 1);
IWebRequest<T> Post<T>(string url, IDictionary<string, string> headers = null, int maximumAttempts = 1);
IWebRequest<T> Put<T>(string url, object payload, IDictionary<string, string> headers = null, int maximumAttempts = 1);
IWebRequest<T> Delete<T>(string url, IDictionary<string, string> headers = null, int maximumAttempts = 1);
}
class NetworkingUtilities : INetworkingUtilities
{
readonly IScheduler m_Scheduler;
readonly ILogger m_Logger;
readonly int m_DefaultRedirectLimit;
public NetworkingUtilities(IScheduler scheduler, ILogger logger)
{
m_Scheduler = scheduler;
m_Logger = logger;
}
/// <summary>
/// The max redirect to follow. By default it's set to 0 and returns the raw 3xx response with a location header.
/// </summary>
public int RedirectLimit { get; set; }
public IWebRequest<T> Get<T>(string url, IDictionary<string, string> headers = null, int maximumAttempts = 1)
{
var request = new WebRequest<T>(m_Scheduler,
m_Logger,
WebRequestVerb.Get,
url,
headers,
string.Empty,
string.Empty,
RedirectLimit,
maximumAttempts);
if (m_Scheduler == null)
{
request.Send();
}
else
{
m_Scheduler.ScheduleAction(request.Send);
}
return request;
}
public IWebRequest<T> Post<T>(string url, IDictionary<string, string> headers = null, int maximumAttempts = 1)
{
var request = new WebRequest<T>(m_Scheduler,
m_Logger,
WebRequestVerb.Post,
url,
headers,
string.Empty,
string.Empty,
RedirectLimit,
maximumAttempts);
if (m_Scheduler == null)
{
request.Send();
}
else
{
m_Scheduler.ScheduleAction(request.Send);
}
return request;
}
public IWebRequest<T> PostJson<T>(string url, object payload, IDictionary<string, string> headers = null, int maximumAttempts = 1)
{
var jsonPayload = JsonConvert.SerializeObject(payload);
var request = new WebRequest<T>(m_Scheduler,
m_Logger,
WebRequestVerb.Post,
url,
headers,
jsonPayload,
"application/json",
RedirectLimit,
maximumAttempts);
if (m_Scheduler == null)
{
request.Send();
}
else
{
m_Scheduler.ScheduleAction(request.Send);
}
return request;
}
public IWebRequest<T> PostForm<T>(string url, string payload, IDictionary<string, string> headers = null, int maximumAttempts = 1)
{
var request = new WebRequest<T>(m_Scheduler,
m_Logger,
WebRequestVerb.Post,
url,
headers,
payload,
"application/x-www-form-urlencoded",
RedirectLimit,
maximumAttempts);
if (m_Scheduler == null)
{
request.Send();
}
else
{
m_Scheduler.ScheduleAction(request.Send);
}
return request;
}
public IWebRequest<T> Put<T>(string url, object payload, IDictionary<string, string> headers = null, int maximumAttempts = 1)
{
var jsonPayload = JsonConvert.SerializeObject(payload);
var request = new WebRequest<T>(m_Scheduler,
m_Logger,
WebRequestVerb.Put,
url,
headers,
jsonPayload,
"application/json",
RedirectLimit,
maximumAttempts);
if (m_Scheduler == null)
{
request.Send();
}
else
{
m_Scheduler.ScheduleAction(request.Send);
}
return request;
}
public IWebRequest<T> Delete<T>(string url, IDictionary<string, string> headers = null, int maximumAttempts = 1)
{
var request = new WebRequest<T>(m_Scheduler,
m_Logger,
WebRequestVerb.Delete,
url,
headers,
string.Empty,
string.Empty,
RedirectLimit,
maximumAttempts);
if (m_Scheduler == null)
{
request.Send();
}
else
{
m_Scheduler.ScheduleAction(request.Send);
}
return request;
}
}
}