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 Get(string url, IDictionary headers = null, int maximumAttempts = 1); IWebRequest PostJson(string url, object payload, IDictionary headers = null, int maximumAttempts = 1); IWebRequest PostForm(string url, string payload, IDictionary headers = null, int maximumAttempts = 1); IWebRequest Post(string url, IDictionary headers = null, int maximumAttempts = 1); IWebRequest Put(string url, object payload, IDictionary headers = null, int maximumAttempts = 1); IWebRequest Delete(string url, IDictionary 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; } /// /// The max redirect to follow. By default it's set to 0 and returns the raw 3xx response with a location header. /// public int RedirectLimit { get; set; } public IWebRequest Get(string url, IDictionary headers = null, int maximumAttempts = 1) { var request = new WebRequest(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 Post(string url, IDictionary headers = null, int maximumAttempts = 1) { var request = new WebRequest(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 PostJson(string url, object payload, IDictionary headers = null, int maximumAttempts = 1) { var jsonPayload = JsonConvert.SerializeObject(payload); var request = new WebRequest(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 PostForm(string url, string payload, IDictionary headers = null, int maximumAttempts = 1) { var request = new WebRequest(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 Put(string url, object payload, IDictionary headers = null, int maximumAttempts = 1) { var jsonPayload = JsonConvert.SerializeObject(payload); var request = new WebRequest(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 Delete(string url, IDictionary headers = null, int maximumAttempts = 1) { var request = new WebRequest(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; } } }