Skip to content

Commit

Permalink
Make Icosa urls overridable via player prefs. Clean up some indirection
Browse files Browse the repository at this point in the history
  • Loading branch information
andybak committed Dec 14, 2024
1 parent 7fcd5c8 commit b3dd3fe
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,19 +182,37 @@ public void PostWork()

public class AssetsServiceClient : MonoBehaviour
{
public static string WEB_BASE_URL = "https://icosa.gallery";
private static string API_BASE_URL = "https://api.icosa.gallery/v1";
// Defaults
public static string DEFAULT_WEB_BASE_URL = "https://icosa.gallery";
private static string DEFAULT_API_BASE_URL = "https://api.icosa.gallery/v1";

// Key names for player prefs
public static string WEB_BASE_URL_KEY = "WEB_BASE_URL";
public static string API_BASE_URL_KEY = "API_BASE_URL";

public static string WebBaseUrl
{
get => GetPlayerPrefOrDefault(WEB_BASE_URL_KEY, DEFAULT_WEB_BASE_URL);
set => PlayerPrefs.SetString(WEB_BASE_URL_KEY, value);
}
public static string ApiBaseUrl
{
get => GetPlayerPrefOrDefault(API_BASE_URL_KEY, DEFAULT_API_BASE_URL);
set => PlayerPrefs.SetString(API_BASE_URL_KEY, value);
}

public static string GetPlayerPrefOrDefault(string key, string defaultValue)
{
return PlayerPrefs.HasKey(key) ? PlayerPrefs.GetString(key) : defaultValue;
}

public static string BaseUrl() => API_BASE_URL;
// The base for the URL to be opened in a user's browser if they wish to publish.
public static string DEFAULT_PUBLISH_URL_BASE = WEB_BASE_URL + "/publish/";
public static string DEFAULT_PUBLISH_URL_BASE = WebBaseUrl + "/publish/";

public static string PublishUrl() => DEFAULT_PUBLISH_URL_BASE;
public static string PublishUrl => DEFAULT_PUBLISH_URL_BASE;
// The base for the URL to be opened in a user's browser if they have saved.
// Also used as the target for the "Your models" desktop menu
public static string DEFAULT_SAVE_URL = WEB_BASE_URL + "/uploads";

public static string SaveUrl() => DEFAULT_SAVE_URL;
public static string DEFAULT_SAVE_URL = WebBaseUrl + "/uploads";

private static string commonQueryParams
{
Expand All @@ -206,11 +224,11 @@ private static string commonQueryParams
}

// Old way
// private static string FeaturedModelsSearchUrl() => $"{BaseUrl()}/assets?&curated=true&{commonQueryParams}";
// private static string FeaturedModelsSearchUrl() => $"{ApiBaseUrl}/assets?&curated=true&{commonQueryParams}";
// New way
private static string FeaturedModelsSearchUrl() => $"{BaseUrl()}/assets?&orderBy=BEST&{commonQueryParams}";
private static string LikedModelsSearchUrl() => $"{BaseUrl()}/users/me/likedassets?{commonQueryParams}";
private static string YourModelsSearchUrl() => $"{BaseUrl()}/users/me/assets?{commonQueryParams}";
private static string FeaturedModelsSearchUrl() => $"{ApiBaseUrl}/assets?&orderBy=BEST&{commonQueryParams}";
private static string LikedModelsSearchUrl() => $"{ApiBaseUrl}/users/me/likedassets?{commonQueryParams}";
private static string YourModelsSearchUrl() => $"{ApiBaseUrl}/users/me/assets?{commonQueryParams}";

// Some regex.
private const string BOUNDARY = "!&!Peltzer12!&!Peltzer34!&!Peltzer56!&!";
Expand Down Expand Up @@ -517,7 +535,7 @@ private IEnumerator ProcessGetLikedModelsResponse(bool success, int responseCode
/// <param name="callback">A callback to which to pass the results.</param>
public void GetAsset(string assetId, System.Action<ObjectStoreEntry> callback)
{
string url = String.Format("{0}/assets/{1}", BaseUrl(), assetId);
string url = String.Format("{0}/assets/{1}", ApiBaseUrl, assetId);
UnityWebRequest request = GetRequest(url, "text/text", true); // Authentication is sometimes required
PeltzerMain.Instance.webRequestManager.EnqueueRequest(
() => { return request; },
Expand Down Expand Up @@ -663,7 +681,7 @@ public IEnumerator UpdateModel(string assetId, HashSet<string> remixIds, byte[]

public static void OpenPublishUrl(string assetId)
{
string publishUrl = PublishUrl() + assetId;
string publishUrl = PublishUrl + assetId;
PeltzerMain.Instance.paletteController.SetPublishDialogActive();
System.Diagnostics.Process.Start(publishUrl);
}
Expand All @@ -674,7 +692,7 @@ private void OpenSaveUrl()
{
return;
}
System.Diagnostics.Process.Start(SaveUrl());
System.Diagnostics.Process.Start(DEFAULT_SAVE_URL);
PeltzerMain.Instance.HasOpenedSaveUrlThisSession = true;
}

Expand Down Expand Up @@ -746,7 +764,7 @@ private IEnumerator UploadResources(byte[] objFile, byte[] triangulatedObjFile,
/// </summary>
private IEnumerator CreateNewAsset(bool saveSelected)
{
string url = $"{BaseUrl()}/assets";
string url = $"{ApiBaseUrl}/assets";
UnityWebRequest request = new UnityWebRequest();

// We wrap in a for loop so we can re-authorise if access tokens have become stale.
Expand Down Expand Up @@ -810,7 +828,7 @@ private IEnumerator FinalizeAsset(string assetId, FormatSaveData saveData, int o
{
string json = CreateJsonForAssetResources(remixIds, objPolyCount, triangulatedObjPolyCount, saveSelected: false);

string url = $"{BaseUrl()}/assets/{assetId}/blocks_finalize";
string url = $"{ApiBaseUrl}/assets/{assetId}/blocks_finalize";
UnityWebRequest request = new UnityWebRequest();

// We wrap in a for loop so we can re-authorise if access tokens have become stale.
Expand Down Expand Up @@ -870,7 +888,7 @@ private string CreateJsonForAssetResources(HashSet<string> remixIds, int objPoly
private IEnumerator AddResource(string filename, string mimeType, byte[] data, string key)
{
elementUploadStates.Add(key, UploadState.IN_PROGRESS);
string url = $"{BaseUrl()}/assets/{assetId}/blocks_format";
string url = $"{ApiBaseUrl}/assets/{assetId}/blocks_format";
UnityWebRequest request = new UnityWebRequest();

// Run this twice so we can re-authorise if access tokens have become stale.
Expand Down Expand Up @@ -987,7 +1005,7 @@ private byte[] Deflate(byte[] data)
/// </summary>
public IEnumerator DeleteAsset(string assetId)
{
string url = String.Format("{0}/assets/{1}", BaseUrl(), assetId);
string url = String.Format("{0}/assets/{1}", ApiBaseUrl, assetId);
UnityWebRequest request = new UnityWebRequest();

// We wrap in a for loop so we can re-authorise if access tokens have become stale.
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/desktop_app/DesktopMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ private void SetupMenu()
menuFeedbackHoverable.SetOnClickAction(() =>
{
// OnClick of Poly Library hoverable button opens the Your Models URL in the web browser.
Application.OpenURL(AssetsServiceClient.SaveUrl());
Application.OpenURL(AssetsServiceClient.DEFAULT_SAVE_URL);
CloseMenu();
});
}
Expand Down
8 changes: 4 additions & 4 deletions Assets/Scripts/entitlement/OAuth2Identity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,8 @@ public class UserInfo
private const string m_ClientId = "TODO";
private const string m_ClientSecret = "TODO";
//private const string m_AccessTokenUri = "https://accounts.google.com/o/oauth2/token";
private static string m_UserInfoUri = $"{AssetsServiceClient.BaseUrl()}/users/me";
private static string m_LoginUrl = $"{AssetsServiceClient.BaseUrl()}/login/device_login";
private static string m_UserInfoUri = $"{AssetsServiceClient.ApiBaseUrl}/users/me";
private static string m_LoginUrl = $"{AssetsServiceClient.ApiBaseUrl}/login/device_login";
private const string m_OAuthScope = "profile email " +
"https://www.googleapis.com/auth/plus.me " +
"https://www.googleapis.com/auth/plus.peopleapi.readwrite";
Expand Down Expand Up @@ -735,7 +735,7 @@ public void Authenticate(UnityWebRequest www)
{
// NEVER add the access token to a URL that isn't our API base url
// It will leak the token.
if (www.url.StartsWith(AssetsServiceClient.BaseUrl()))
if (www.url.StartsWith(AssetsServiceClient.ApiBaseUrl))
{
www.SetRequestHeader("Authorization", $"Bearer {m_AccessToken}");
}
Expand Down Expand Up @@ -838,7 +838,7 @@ public IEnumerator<object> Authorize(Action onSuccess, Action onFailure, bool pr
{
if (String.IsNullOrEmpty(m_RefreshToken) && promptUserIfNoToken)
{
string deviceCodeUrl = $"{AssetsServiceClient.WEB_BASE_URL}/device";
string deviceCodeUrl = $"{AssetsServiceClient.WebBaseUrl}/device";
// Something about the url makes OpenURL() not work on OSX, so use a workaround
if (Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.OSXPlayer)
{
Expand Down

0 comments on commit b3dd3fe

Please sign in to comment.