From b5a254d185d5761c44eeb921a33badf4648d2736 Mon Sep 17 00:00:00 2001 From: Robin <1121080+rYuuk@users.noreply.github.com> Date: Wed, 29 Nov 2023 17:58:04 +0530 Subject: [PATCH] [SDK-595] Remove headers for icon web requests (#167) ## [SDK-595](https://ready-player-me.atlassian.net/browse/SDK-595) ## Description - Removed auth and app Id headers for template icon and asset icon requests --- Runtime/Analytics/AmplitudeEventLogger.cs | 7 +-- .../WebRequests/AuthenticationRequests.cs | 5 +- .../WebRequests/AuthorizedRequest.cs | 7 +-- .../WebRequests/PartnerAssetsRequests.cs | 8 +-- Runtime/CommonHeaders.cs | 22 +++++++- Runtime/Utils/WebRequestDispatcher.cs | 4 +- .../Utils/WebRequestDispatcherExtension.cs | 55 +++---------------- 7 files changed, 36 insertions(+), 72 deletions(-) diff --git a/Runtime/Analytics/AmplitudeEventLogger.cs b/Runtime/Analytics/AmplitudeEventLogger.cs index 53c9d45a..6268162f 100644 --- a/Runtime/Analytics/AmplitudeEventLogger.cs +++ b/Runtime/Analytics/AmplitudeEventLogger.cs @@ -80,13 +80,8 @@ private static async Task Dispatch(string url, string payload) throw new Exception(NO_INTERNET_CONNECTION); } - var headers = new Dictionary - { - { "Content-Type", "application/json" } - }; - var webRequestDispatcher = new WebRequestDispatcher(); - var response = await webRequestDispatcher.SendRequest(url, HttpMethod.POST, headers, payload); + var response = await webRequestDispatcher.SendRequest(url, HttpMethod.POST, CommonHeaders.GetHeadersWithAppId(), payload); if (!response.IsSuccess) { diff --git a/Runtime/AvatarCreator/WebRequests/AuthenticationRequests.cs b/Runtime/AvatarCreator/WebRequests/AuthenticationRequests.cs index 35facc91..7dd09643 100644 --- a/Runtime/AvatarCreator/WebRequests/AuthenticationRequests.cs +++ b/Runtime/AvatarCreator/WebRequests/AuthenticationRequests.cs @@ -8,10 +8,7 @@ namespace ReadyPlayerMe.AvatarCreator public class AuthenticationRequests { private readonly string domain; - private readonly Dictionary headers = new Dictionary - { - { "Content-Type", "application/json" } - }; + private readonly IDictionary headers = CommonHeaders.GetHeadersWithAppId(); private readonly WebRequestDispatcher webRequestDispatcher; diff --git a/Runtime/AvatarCreator/WebRequests/AuthorizedRequest.cs b/Runtime/AvatarCreator/WebRequests/AuthorizedRequest.cs index 527e7b91..47318514 100644 --- a/Runtime/AvatarCreator/WebRequests/AuthorizedRequest.cs +++ b/Runtime/AvatarCreator/WebRequests/AuthorizedRequest.cs @@ -40,11 +40,8 @@ public class AuthorizedRequest private async Task Send(RequestData requestData, CancellationToken ctx) where T : IResponse, new() { - var headers = new Dictionary - { - { "Content-Type", "application/json" }, - { "Authorization", $"Bearer {AuthManager.UserSession.Token}" } - }; + var headers = CommonHeaders.GetHeadersWithAppId(); + headers.Add("Authorization", $"Bearer {AuthManager.UserSession.Token}"); var webRequestDispatcher = new WebRequestDispatcher(); return await webRequestDispatcher.SendRequest( diff --git a/Runtime/AvatarCreator/WebRequests/PartnerAssetsRequests.cs b/Runtime/AvatarCreator/WebRequests/PartnerAssetsRequests.cs index d424bfec..a7398535 100644 --- a/Runtime/AvatarCreator/WebRequests/PartnerAssetsRequests.cs +++ b/Runtime/AvatarCreator/WebRequests/PartnerAssetsRequests.cs @@ -132,12 +132,8 @@ public PartnerAssetsRequests(string appId) } var downloadHandler = new DownloadHandlerTexture(); - var response = await authorizedRequest.SendRequest(new RequestData - { - Url = url, - Method = HttpMethod.GET, - DownloadHandler = downloadHandler - }, ctx: ctx); + var webRequestDispatcher = new WebRequestDispatcher(); + var response = await webRequestDispatcher.SendRequest(url,HttpMethod.GET, downloadHandler: downloadHandler, ctx: ctx); response.ThrowIfError(); diff --git a/Runtime/CommonHeaders.cs b/Runtime/CommonHeaders.cs index 621c398b..0e0f1cb8 100644 --- a/Runtime/CommonHeaders.cs +++ b/Runtime/CommonHeaders.cs @@ -14,7 +14,25 @@ public static class CommonHeaders private const string EDITMODE = "editmode"; private const string SEPARATOR = "-"; - public static Dictionary GetRequestHeaders() + public static KeyValuePair GetApplicationJsonHeader() + { + return new KeyValuePair("Content-Type", "application/json"); + } + + public static KeyValuePair GetAppIdHeader() + { + return new KeyValuePair("X-APP-ID", CoreSettingsHandler.CoreSettings.AppId); + } + + public static IDictionary GetHeadersWithAppId() + { + IDictionary dictionary = new Dictionary(); + dictionary.Add(GetApplicationJsonHeader()); + dictionary.Add(GetAppIdHeader()); + return dictionary; + } + + public static Dictionary GetAnalyticsHeaders() { var source = UNITY_PREFIX + SEPARATOR; if (Application.isEditor) @@ -28,7 +46,7 @@ public static Dictionary GetRequestHeaders() return new Dictionary { { RPM_SOURCE, source }, - { RPM_SDK_VERSION, ApplicationData.GetData().SDKVersion } + { RPM_SDK_VERSION, ApplicationData.GetData().SDKVersion }, }; } } diff --git a/Runtime/Utils/WebRequestDispatcher.cs b/Runtime/Utils/WebRequestDispatcher.cs index 30a72299..653d4ba4 100644 --- a/Runtime/Utils/WebRequestDispatcher.cs +++ b/Runtime/Utils/WebRequestDispatcher.cs @@ -28,7 +28,7 @@ public class WebRequestDispatcher public async Task SendRequest( string url, HttpMethod httpMethod, - Dictionary headers = null, + IDictionary headers = null, string payload = null, DownloadHandler downloadHandler = default, CancellationToken ctx = new CancellationToken()) where T : IResponse, new() @@ -46,8 +46,6 @@ public async Task SendRequest( } } - request.SetRequestHeader(APP_ID, CoreSettingsHandler.CoreSettings.AppId); - downloadHandler ??= new DownloadHandlerBuffer(); request.downloadHandler = downloadHandler; diff --git a/Runtime/Utils/WebRequestDispatcherExtension.cs b/Runtime/Utils/WebRequestDispatcherExtension.cs index 011787d4..f9ae83d8 100644 --- a/Runtime/Utils/WebRequestDispatcherExtension.cs +++ b/Runtime/Utils/WebRequestDispatcherExtension.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using System.Threading; using System.Threading.Tasks; using UnityEngine; @@ -18,42 +19,6 @@ public static class WebRequestDispatcherExtension private static bool HasInternetConnection => Application.internetReachability != NetworkReachability.NotReachable; - /// - /// This asynchronous method makes a request to the provided and returns the response. - /// - /// WebRequestDispatcher object - /// The URL to make the to. - /// The payload data to send as a string. - /// Can be used to cancel the operation. - /// The number of seconds to wait for the WebRequest to finish before aborting. - /// A if successful otherwise it will throw an exception. - public static async Task Dispatch(this WebRequestDispatcher webRequestDispatcher, string url, string payload, - CancellationToken token, - int timeout = TIMEOUT) - { - if (!HasInternetConnection) - { - throw new CustomException(FailureType.NoInternetConnection, NO_INTERNET_CONNECTION); - } - - var headers = new Dictionary() - { - { "Content-Type", "application/json" } - }; - - webRequestDispatcher.Timeout = timeout; - var response = await webRequestDispatcher.SendRequest(url, HttpMethod.POST, headers, payload, ctx: token); - - token.ThrowCustomExceptionIfCancellationRequested(); - - if (!response.IsSuccess) - { - throw new CustomException(FailureType.DownloadError, response.Error); - } - - return response; - } - /// /// This asynchronous method makes GET request to the and returns the data in the /// . @@ -71,15 +36,14 @@ public static async Task DownloadIntoMemory(this WebRequestDispatcher throw new CustomException(FailureType.NoInternetConnection, NO_INTERNET_CONNECTION); } - var headers = new Dictionary(); + IDictionary headers = new Dictionary(); if (!url.Contains(CLOUDFRONT_IDENTIFIER)) // Required to prevent CORS errors in WebGL { - foreach (KeyValuePair header in CommonHeaders.GetRequestHeaders()) - { - headers.Add(header.Key, header.Value); - } + headers = CommonHeaders.GetAnalyticsHeaders(); } + headers.Add(CommonHeaders.GetAppIdHeader()); + webRequestDispatcher.Timeout = timeout; var response = await webRequestDispatcher.SendRequest(url, HttpMethod.GET, headers, ctx: token); token.ThrowCustomExceptionIfCancellationRequested(); @@ -113,15 +77,14 @@ public static async Task DownloadIntoFile(this WebRequestDispatche var downloadHandler = new DownloadHandlerFile(path); downloadHandler.removeFileOnAbort = true; - var headers = new Dictionary(); + IDictionary headers = new Dictionary(); if (!url.Contains(CLOUDFRONT_IDENTIFIER)) // Required to prevent CORS errors in WebGL { - foreach (KeyValuePair header in CommonHeaders.GetRequestHeaders()) - { - headers.Add(header.Key, header.Value); - } + headers = CommonHeaders.GetAnalyticsHeaders(); } + headers.Add(CommonHeaders.GetAppIdHeader()); + webRequestDispatcher.Timeout = timeout; var response = await webRequestDispatcher.SendRequest(url, HttpMethod.GET, headers, downloadHandler: downloadHandler, ctx: token);