Skip to content

Commit

Permalink
[SDK-595] Remove headers for icon web requests (#167)
Browse files Browse the repository at this point in the history
## [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
  • Loading branch information
rYuuk authored Nov 29, 2023
1 parent 7c47e45 commit b5a254d
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 72 deletions.
7 changes: 1 addition & 6 deletions Runtime/Analytics/AmplitudeEventLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,8 @@ private static async Task Dispatch(string url, string payload)
throw new Exception(NO_INTERNET_CONNECTION);
}

var headers = new Dictionary<string, string>
{
{ "Content-Type", "application/json" }
};

var webRequestDispatcher = new WebRequestDispatcher();
var response = await webRequestDispatcher.SendRequest<Response>(url, HttpMethod.POST, headers, payload);
var response = await webRequestDispatcher.SendRequest<Response>(url, HttpMethod.POST, CommonHeaders.GetHeadersWithAppId(), payload);

if (!response.IsSuccess)
{
Expand Down
5 changes: 1 addition & 4 deletions Runtime/AvatarCreator/WebRequests/AuthenticationRequests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ namespace ReadyPlayerMe.AvatarCreator
public class AuthenticationRequests
{
private readonly string domain;
private readonly Dictionary<string, string> headers = new Dictionary<string, string>
{
{ "Content-Type", "application/json" }
};
private readonly IDictionary<string, string> headers = CommonHeaders.GetHeadersWithAppId();

private readonly WebRequestDispatcher webRequestDispatcher;

Expand Down
7 changes: 2 additions & 5 deletions Runtime/AvatarCreator/WebRequests/AuthorizedRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,8 @@ public class AuthorizedRequest

private async Task<T> Send<T>(RequestData requestData, CancellationToken ctx) where T : IResponse, new()
{
var headers = new Dictionary<string, string>
{
{ "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<T>(
Expand Down
8 changes: 2 additions & 6 deletions Runtime/AvatarCreator/WebRequests/PartnerAssetsRequests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,8 @@ public PartnerAssetsRequests(string appId)
}

var downloadHandler = new DownloadHandlerTexture();
var response = await authorizedRequest.SendRequest<ResponseTexture>(new RequestData
{
Url = url,
Method = HttpMethod.GET,
DownloadHandler = downloadHandler
}, ctx: ctx);
var webRequestDispatcher = new WebRequestDispatcher();
var response = await webRequestDispatcher.SendRequest<ResponseTexture>(url,HttpMethod.GET, downloadHandler: downloadHandler, ctx: ctx);

response.ThrowIfError();

Expand Down
22 changes: 20 additions & 2 deletions Runtime/CommonHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,25 @@ public static class CommonHeaders
private const string EDITMODE = "editmode";
private const string SEPARATOR = "-";

public static Dictionary<string, string> GetRequestHeaders()
public static KeyValuePair<string, string> GetApplicationJsonHeader()
{
return new KeyValuePair<string, string>("Content-Type", "application/json");
}

public static KeyValuePair<string, string> GetAppIdHeader()
{
return new KeyValuePair<string, string>("X-APP-ID", CoreSettingsHandler.CoreSettings.AppId);
}

public static IDictionary<string, string> GetHeadersWithAppId()
{
IDictionary<string,string> dictionary = new Dictionary<string, string>();
dictionary.Add(GetApplicationJsonHeader());
dictionary.Add(GetAppIdHeader());
return dictionary;
}

public static Dictionary<string, string> GetAnalyticsHeaders()
{
var source = UNITY_PREFIX + SEPARATOR;
if (Application.isEditor)
Expand All @@ -28,7 +46,7 @@ public static Dictionary<string, string> GetRequestHeaders()
return new Dictionary<string, string>
{
{ RPM_SOURCE, source },
{ RPM_SDK_VERSION, ApplicationData.GetData().SDKVersion }
{ RPM_SDK_VERSION, ApplicationData.GetData().SDKVersion },
};
}
}
Expand Down
4 changes: 1 addition & 3 deletions Runtime/Utils/WebRequestDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class WebRequestDispatcher
public async Task<T> SendRequest<T>(
string url,
HttpMethod httpMethod,
Dictionary<string, string> headers = null,
IDictionary<string, string> headers = null,
string payload = null,
DownloadHandler downloadHandler = default,
CancellationToken ctx = new CancellationToken()) where T : IResponse, new()
Expand All @@ -46,8 +46,6 @@ public async Task<T> SendRequest<T>(
}
}

request.SetRequestHeader(APP_ID, CoreSettingsHandler.CoreSettings.AppId);

downloadHandler ??= new DownloadHandlerBuffer();

request.downloadHandler = downloadHandler;
Expand Down
55 changes: 9 additions & 46 deletions Runtime/Utils/WebRequestDispatcherExtension.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
Expand All @@ -18,42 +19,6 @@ public static class WebRequestDispatcherExtension

private static bool HasInternetConnection => Application.internetReachability != NetworkReachability.NotReachable;

/// <summary>
/// This asynchronous method makes a request to the provided <paramref name="url" /> and returns the response.
/// </summary>
/// <param name="webRequestDispatcher">WebRequestDispatcher object</param>
/// <param name="url">The URL to make the <see cref="UnityWebRequest" /> to.</param>
/// <param name="payload">The payload data to send as a <c>string</c>.</param>
/// <param name="token">Can be used to cancel the operation.</param>
/// <param name="timeout">The number of seconds to wait for the WebRequest to finish before aborting.</param>
/// <returns>A <see cref="Response" /> if successful otherwise it will throw an exception.</returns>
public static async Task<Response> 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<string, string>()
{
{ "Content-Type", "application/json" }
};

webRequestDispatcher.Timeout = timeout;
var response = await webRequestDispatcher.SendRequest<Response>(url, HttpMethod.POST, headers, payload, ctx: token);

token.ThrowCustomExceptionIfCancellationRequested();

if (!response.IsSuccess)
{
throw new CustomException(FailureType.DownloadError, response.Error);
}

return response;
}

/// <summary>
/// This asynchronous method makes GET request to the <paramref name="url" /> and returns the data in the
/// <see cref="Response" />.
Expand All @@ -71,15 +36,14 @@ public static async Task<Response> DownloadIntoMemory(this WebRequestDispatcher
throw new CustomException(FailureType.NoInternetConnection, NO_INTERNET_CONNECTION);
}

var headers = new Dictionary<string, string>();
IDictionary<string, string> headers = new Dictionary<string, string>();
if (!url.Contains(CLOUDFRONT_IDENTIFIER)) // Required to prevent CORS errors in WebGL
{
foreach (KeyValuePair<string, string> 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<Response>(url, HttpMethod.GET, headers, ctx: token);
token.ThrowCustomExceptionIfCancellationRequested();
Expand Down Expand Up @@ -113,15 +77,14 @@ public static async Task<ResponseFile> DownloadIntoFile(this WebRequestDispatche
var downloadHandler = new DownloadHandlerFile(path);
downloadHandler.removeFileOnAbort = true;

var headers = new Dictionary<string, string>();
IDictionary<string, string> headers = new Dictionary<string, string>();
if (!url.Contains(CLOUDFRONT_IDENTIFIER)) // Required to prevent CORS errors in WebGL
{
foreach (KeyValuePair<string, string> 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<ResponseFile>(url, HttpMethod.GET, headers, downloadHandler: downloadHandler,
ctx: token);
Expand Down

0 comments on commit b5a254d

Please sign in to comment.