using System.Linq; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; namespace Proyecto26.Common { public static class Extensions { /// /// Create an object with the response of the server /// /// An UnityWebRequest object. /// An object with the response. public static ResponseHelper CreateWebResponse(this UnityWebRequest request) { return new ResponseHelper(request); } /// /// Validate if the request is OK with the current options /// /// An UnityWebRequest object. /// The options of the request. /// A boolean that indicates if the request is valid. public static bool IsValidRequest(this UnityWebRequest request, RequestHelper options) { return request.isDone && !request.isNetworkError && ( !request.isHttpError || options.IgnoreHttpException ); } /// /// Escapes characters in a string to ensure they are URL-friendly /// /// A query string param /// Escaped query string param. public static string EscapeURL(this string queryParam) { #if UNITY_2018_3_OR_NEWER return UnityWebRequest.EscapeURL(queryParam); #else return WWW.EscapeURL(queryParam); #endif } /// /// Generate the url and escape params /// /// The URI of the resource to retrieve via HTTP. /// Query string parameters. /// The full url with query string params. public static string BuildUrl(this string uri, Dictionary queryParams) { var url = uri; var defaultParams = RestClient.DefaultRequestParams; if (defaultParams.Any() || queryParams.Any()) { var urlParamKeys = queryParams.Keys; url += (url.Contains("?") ? "&" : "?") + string.Join("&", queryParams .Concat( defaultParams .Where(p => !urlParamKeys.Contains(p.Key)) ) .Select(p => string.Format("{0}={1}", p.Key, p.Value.EscapeURL())) .ToArray() ); } return url; } } }