Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release Candidate v1.2.5 #145

Merged
merged 18 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 86 additions & 62 deletions Runtime/Client/LootLockerBaseServerAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ public void SwitchURL(LootLocker.LootLockerEnums.LootLockerCallerRole mainCaller
callerRole = mainCallerRole;
}

private bool WebRequestSucceeded(UnityWebRequest webRequest)
{
return !
#if UNITY_2020_1_OR_NEWER
(webRequest.result == UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError || !string.IsNullOrEmpty(webRequest.error));
#else
(webRequest.isHttpError || webRequest.isNetworkError || !string.IsNullOrEmpty(webRequest.error));
#endif
}

public void SendRequest(LootLockerServerRequest request, System.Action<LootLockerResponse> OnServerResponse = null)
{
StartCoroutine(coroutine());
Expand Down Expand Up @@ -97,7 +107,7 @@ IEnumerator coroutine()
if (!webRequest.isDone && timedOut)
{
LootLockerLogger.GetForLogLevel(LootLockerLogger.LogLevel.Warning)("Exceeded maxTimeOut waiting for a response from " + request.httpMethod + " " + url);
OnServerResponse?.Invoke(new LootLockerResponse() { hasError = true, statusCode = 408, text = "{\"error\": \"" + request.endpoint + " Timed out.\"}", Error = request.endpoint + " Timed out." });
OnServerResponse?.Invoke(LootLockerResponseFactory.Error<LootLockerResponse>(request.endpoint + " Timed out.", 408));
yield break;
}

Expand All @@ -112,55 +122,17 @@ IEnumerator coroutine()
LootLockerLogger.GetForLogLevel(LootLockerLogger.LogLevel.Error)(ObfuscateJsonStringForLogging(webRequest.downloadHandler.text));
}

LootLockerResponse response = new LootLockerResponse();
response.statusCode = (int)webRequest.responseCode;
#if UNITY_2020_1_OR_NEWER
if (webRequest.result == UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError || !string.IsNullOrEmpty(webRequest.error))
#else
if (webRequest.isHttpError || webRequest.isNetworkError || !string.IsNullOrEmpty(webRequest.error))
#endif

if(WebRequestSucceeded(webRequest))
{
switch (webRequest.responseCode)
{
case 200:
response.Error = "";
break;
case 400:
response.Error = "Bad Request -- Your request has an error";
break;
case 402:
response.Error = "Payment Required -- Payment failed. Insufficient funds, etc.";
break;
case 401:
response.Error = "Unauthorized -- Your session_token is invalid";
break;
case 403:
response.Error = "Forbidden -- You do not have access";
break;
case 404:
response.Error = "Not Found";
break;
case 405:
response.Error = "Method Not Allowed";
break;
case 406:
response.Error = "Not Acceptable -- Purchasing is disabled";
break;
case 409:
response.Error = "Conflict -- Your state is most likely not aligned with the servers.";
break;
case 429:
response.Error = "Too Many Requests -- You're being limited for sending too many requests too quickly.";
break;
case 500:
response.Error = "Internal Server Error -- We had a problem with our server. Try again later.";
break;
case 503:
response.Error = "Service Unavailable -- We're either offline for maintenance, or an error that should be solvable by calling again later was triggered.";
break;
}

LootLockerResponse response = new LootLockerResponse();
response.statusCode = (int)webRequest.responseCode;
response.success = true;
response.text = webRequest.downloadHandler.text;
response.errorData = null;
OnServerResponse?.Invoke(response);
}
else
{
if ((webRequest.responseCode == 401 || webRequest.responseCode == 403) && LootLockerConfig.current.allowTokenRefresh && CurrentPlatform.Get() != Platforms.Steam && tries < maxRetry)
{
tries++;
Expand All @@ -170,23 +142,75 @@ IEnumerator coroutine()
else
{
tries = 0;
response.Error += " " + ObfuscateJsonStringForLogging(webRequest.downloadHandler.text);
LootLockerResponse response = new LootLockerResponse();
response.statusCode = (int)webRequest.responseCode;
response.success = false;
response.hasError = true;
response.text = webRequest.downloadHandler.text;
LootLockerLogger.GetForLogLevel(LootLockerLogger.LogLevel.Error)(response.Error);

LootLockerErrorData errorData = LootLockerJson.DeserializeObject<LootLockerErrorData>(webRequest.downloadHandler.text);
// Check if the error uses the "new" error style (https://docs.lootlocker.com/reference/error-codes)
if (errorData != null && !string.IsNullOrEmpty(errorData.code))
{
response.errorData = errorData;
}
else
{
errorData = new LootLockerErrorData();
switch (webRequest.responseCode)
{
case 400:
errorData.message = "Bad Request -- Your request has an error.";
errorData.code = "bad_request";
break;
case 401:
errorData.message = "Unauthorized -- Your session_token is invalid.";
errorData.code = "unauthorized";
break;
case 402:
errorData.message = "Payment Required -- Payment failed. Insufficient funds, etc.";
errorData.code = "payment_required";
break;
case 403:
errorData.message = "Forbidden -- You do not have access to this resource.";
errorData.code = "forbidden";
break;
case 404:
errorData.message = "Not Found -- The requested resource could not be found.";
errorData.code = "not_found";
break;
case 405:
errorData.message = "Method Not Allowed -- The selected http method is invalid for this resource.";
errorData.code = "method_not_allowed";
break;
case 406:
errorData.message = "Not Acceptable -- Purchasing is disabled.";
errorData.code = "not_acceptable";
break;
case 409:
errorData.message = "Conflict -- Your state is most likely not aligned with the servers.";
errorData.code = "conflict";
break;
case 429:
errorData.message = "Too Many Requests -- You're being limited for sending too many requests too quickly.";
errorData.code = "too_many_requests";
break;
case 500:
errorData.message = "Internal Server Error -- We had a problem with our server. Try again later.";
errorData.code = "internal_server_error";
break;
case 503:
errorData.message = "Service Unavailable -- We're either offline for maintenance, or an error that should be solvable by calling again later was triggered.";
errorData.code = "service_unavailable";
break;
default:
errorData.message = "Unknown error.";
break;
}
errorData.message += " " + ObfuscateJsonStringForLogging(webRequest.downloadHandler.text);
}
LootLockerLogger.GetForLogLevel(LootLockerLogger.LogLevel.Error)(errorData.message + (!string.IsNullOrEmpty(errorData.doc_url) ? " -- " + errorData.doc_url : ""));
OnServerResponse?.Invoke(response);
}

}
else
{
response.success = true;
response.hasError = false;
response.statusCode = (int)webRequest.responseCode;
response.text = webRequest.downloadHandler.text;
OnServerResponse?.Invoke(response);
}

}
Expand Down Expand Up @@ -278,7 +302,7 @@ UnityWebRequest CreateWebRequest(string url, LootLockerServerRequest request)
// Set the content type - NO QUOTES around the boundary
string contentType = String.Concat("multipart/form-data; boundary=--", Encoding.UTF8.GetString(boundary));

// Make my request object and add the raw body. Set anything else you need here
// Make my request object and add the raw text. Set anything else you need here
webRequest = new UnityWebRequest();
webRequest.SetRequestHeader("Content-Type", "multipart/form-data; boundary=--");
webRequest.uri = new Uri(url);
Expand Down
20 changes: 19 additions & 1 deletion Runtime/Client/LootLockerEndPoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,25 @@ public class LootLockerEndPoints
public static EndPointClass getEquipableContextbyCharacter = new EndPointClass("v1/player/character/{0}/contexts", LootLockerHTTPMethod.GET);
public static EndPointClass createCharacter = new EndPointClass("v1/player/character", LootLockerHTTPMethod.POST);
public static EndPointClass listCharacterTypes = new EndPointClass("v1/player/character/types", LootLockerHTTPMethod.GET);

public static EndPointClass listPlayerCharacters = new EndPointClass("v1/player/character/list", LootLockerHTTPMethod.GET);

// Hero
[Header("Hero")]
public static EndPointClass getGameHeroes = new EndPointClass("v1/heroes", LootLockerHTTPMethod.GET);
public static EndPointClass getHero = new EndPointClass("v1/player/heroes/{0}", LootLockerHTTPMethod.GET);
public static EndPointClass listPlayerHeroes = new EndPointClass("v1/player/heroes", LootLockerHTTPMethod.GET);
public static EndPointClass listOtherPlayersHeroesBySteamID64 = new EndPointClass("v1/heroes/list/{0}", LootLockerHTTPMethod.GET);
public static EndPointClass getOtherPlayersDefaultHeroBySteamID64 = new EndPointClass("v1/player/heroes/default/{0}", LootLockerHTTPMethod.GET);
public static EndPointClass createHero = new EndPointClass("v1/player/heroes/", LootLockerHTTPMethod.POST);
public static EndPointClass updateHero = new EndPointClass("v1/player/heroes/{0}", LootLockerHTTPMethod.PUT);
public static EndPointClass deleteHero = new EndPointClass("v1/player/heroes/{0}", LootLockerHTTPMethod.DELETE);
public static EndPointClass getHeroInventory = new EndPointClass("v1/player/heroes/{0}/inventory", LootLockerHTTPMethod.GET);
public static EndPointClass getHeroLoadout = new EndPointClass("v1/player/heroes/{0}/loadout", LootLockerHTTPMethod.GET);
public static EndPointClass getOtherPlayersHeroLoadout = new EndPointClass("v1/heroes/{0}/loadout", LootLockerHTTPMethod.GET);
public static EndPointClass addAssetToHeroLoadout = new EndPointClass("v1/player/heroes/{0}/loadout", LootLockerHTTPMethod.POST);
public static EndPointClass addAssetVariationToHeroLoadout = new EndPointClass("v1/player/heroes/{0}/loadout", LootLockerHTTPMethod.POST);
public static EndPointClass removeAssetFromHeroLoadout = new EndPointClass("v1/player/heroes/{0}/loadout/{1}", LootLockerHTTPMethod.DELETE);

// Character Progressions
[Header("Character Progressions")]
public static EndPointClass getAllCharacterProgressions = new EndPointClass("player/characters/{0}/progressions", LootLockerHTTPMethod.GET);
Expand Down
Loading
Loading