Skip to content

Commit

Permalink
Merge pull request #13 from wit-ai/pullrequest-removeidetoken
Browse files Browse the repository at this point in the history
Move to using server access token instead of IDE token
  • Loading branch information
yolanother authored Jul 13, 2021
2 parents 504f6b0 + 21170fc commit 1df8d09
Show file tree
Hide file tree
Showing 12 changed files with 263 additions and 80 deletions.
4 changes: 2 additions & 2 deletions Scripts/Editor/BaseWitWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected void RefreshConfigList()
protected virtual void OnGUI()
{
minSize = new Vector2(450, 300);
DrawHeader();
DrawHeader(WindowStyle == WindowStyles.Themed ? WitStyles.BackgroundWhite : WitStyles.BackgroundWitDark);

if (WindowStyle == WindowStyles.Themed)
{
Expand All @@ -84,7 +84,7 @@ protected virtual void OnGUI()

protected void DrawHeader(GUIStyle headerBackground = null)
{
GUILayout.BeginVertical(null == headerBackground ? WitStyles.BackgroundWhite25P : headerBackground);
GUILayout.BeginVertical(null == headerBackground ? WitStyles.BackgroundWhite : headerBackground);
GUILayout.Space(16);
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
Expand Down
17 changes: 5 additions & 12 deletions Scripts/Editor/Configuration/WitConfigurationEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using System.Collections.Generic;
using System.Linq;
using com.facebook.witai;
using com.facebook.witai.data;
using UnityEditor;
using UnityEngine;
Expand Down Expand Up @@ -48,6 +49,7 @@ public class WitConfigurationEditor : Editor

public void OnEnable()
{
WitAuthUtility.InitEditorTokens();
configuration = target as WitConfiguration;
configuration?.UpdateData(() =>
{
Expand Down Expand Up @@ -92,22 +94,13 @@ public override void OnInspectorGUI()
GUILayout.EndHorizontal();


if (configuration && GUILayout.Button("Get Configuration from IDE Token"))
if (configuration && GUILayout.Button("Get Configuration"))
{
configuration.clientAccessToken = WitAuthUtility.ClientToken;
if (WitAuthUtility.AppId != configuration.application.id)
{
configuration.application = new WitApplication()
{
id = WitAuthUtility.AppId,
witConfiguration = configuration
};
}
configuration.UpdateData(() =>
configuration.FetchAppConfigFromServerToken(() =>
{
Repaint();
appConfigurationFoldout = false;
});
appConfigurationFoldout = false;
}
}

Expand Down
33 changes: 15 additions & 18 deletions Scripts/Editor/Configuration/WitWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace com.facebook.witai.configuration
{
public class WitWindow : BaseWitWindow
{
protected override WindowStyles WindowStyle => WitAuthUtility.IsIDETokenValid
protected override WindowStyles WindowStyle => WitAuthUtility.IsServerTokenValid
? WindowStyles.Editor
: WindowStyles.Themed;

Expand All @@ -34,7 +34,7 @@ public static void ShowWindow()

protected override void OnDrawContent()
{
if (!WitAuthUtility.IsIDETokenValid)
if (!WitAuthUtility.IsServerTokenValid)
{
DrawWelcome();
}
Expand All @@ -46,6 +46,8 @@ protected override void OnDrawContent()

protected override void OnEnable()
{
WitAuthUtility.InitEditorTokens();

if (witConfiguration)
{
witEditor = (WitConfigurationEditor) Editor.CreateEditor(witConfiguration);
Expand All @@ -59,12 +61,13 @@ private void DrawWit()
{
// Recommended max size based on EditorWindow.maxSize doc for resizable window.
maxSize = new Vector2(4000, 4000);
titleContent = new GUIContent("Wit Configuration");

GUILayout.BeginVertical(EditorStyles.helpBox);
var token = EditorGUILayout.PasswordField("IDE Token", WitAuthUtility.IDEToken);
if (token != WitAuthUtility.IDEToken)
var token = EditorGUILayout.PasswordField("Server Access Token", WitAuthUtility.ServerToken);
if (token != WitAuthUtility.ServerToken)
{
WitAuthUtility.IDEToken = token;
WitAuthUtility.ServerToken = token;
RefreshContent();
}

Expand All @@ -76,7 +79,7 @@ private void DrawWit()
}
GUILayout.EndHorizontal();

if (configChanged && witConfiguration)
if (witConfiguration && (configChanged || !witEditor))
{
witEditor = (WitConfigurationEditor) Editor.CreateEditor(witConfiguration);
witEditor.OnEnable();
Expand All @@ -95,13 +98,7 @@ private void CreateConfiguration()
{
WitConfiguration asset = ScriptableObject.CreateInstance<WitConfiguration>();

asset.application = new WitApplication()
{
id = WitAuthUtility.AppId,
witConfiguration = asset
};
asset.application.UpdateData();
asset.clientAccessToken = WitAuthUtility.ClientToken;
asset.FetchAppConfigFromServerToken(Repaint);

path = path.Substring(Application.dataPath.Length - 6);
AssetDatabase.CreateAsset(asset, path);
Expand Down Expand Up @@ -129,17 +126,17 @@ private void DrawWelcome()
BeginCenter(296);

GUILayout.BeginHorizontal();
GUILayout.Label("Paste your IDE Token here", WitStyles.Label);
GUILayout.Label("Paste your Server Access Token here", WitStyles.Label);
GUILayout.FlexibleSpace();
if (GUILayout.Button(WitStyles.PasteIcon, WitStyles.Label))
{
WitAuthUtility.IDEToken = EditorGUIUtility.systemCopyBuffer;
WitAuthUtility.ServerToken = EditorGUIUtility.systemCopyBuffer;
}
GUILayout.EndHorizontal();
var token = EditorGUILayout.PasswordField(WitAuthUtility.IDEToken, WitStyles.TextField);
if (token != WitAuthUtility.IDEToken)
var token = EditorGUILayout.PasswordField(WitAuthUtility.ServerToken, WitStyles.TextField);
if (token != WitAuthUtility.ServerToken)
{
WitAuthUtility.IDEToken = token;
WitAuthUtility.ServerToken = token;
}
EndCenter();

Expand Down
12 changes: 10 additions & 2 deletions Scripts/Editor/WitStyles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ public class WitStyles
public static Texture2D TextureWitBlueBg;
public static Texture2D TextureFBBlue;
public static Texture2D TextureTextField;

public static Texture2D TextureWitDark;
public static GUIStyle BackgroundWhite;
public static GUIStyle BackgroundWhite25P;
public static GUIStyle BackgroundBlack25P;
public static GUIStyle BackgroundWitBlue;
public static GUIStyle BackgroundFBBlue;
public static GUIStyle BackgroundWitDark;

public static GUIStyle LabelHeader;
public static GUIStyle LabelHeader2;
Expand Down Expand Up @@ -77,6 +78,10 @@ static WitStyles()
TextureTextField.SetPixel(0, 0, new Color(.85f, .85f, .95f));
TextureTextField.Apply();

TextureWitDark = new Texture2D(1, 1);
TextureWitDark.SetPixel(0,0, new Color(0.267f, 0.286f, 0.31f));
TextureWitDark.Apply();

BackgroundWhite = new GUIStyle();
BackgroundWhite.normal.background = TextureWhite;

Expand All @@ -90,6 +95,9 @@ static WitStyles()
BackgroundBlack25P.normal.background = TextureBlack25P;
BackgroundBlack25P.normal.textColor = Color.white;

BackgroundWitDark = new GUIStyle();
BackgroundWitDark.normal.background = TextureWitDark;

FacebookButton = new GUIStyle(EditorStyles.miniButton);
FacebookButton.normal.background = TextureWitBlueBg;

Expand All @@ -99,7 +107,7 @@ static WitStyles()
Label.wordWrap = true;

LabelHeader = new GUIStyle(Label);
LabelHeader.fontSize = 64;
LabelHeader.fontSize = 48;

LabelHeader2 = new GUIStyle(Label);
LabelHeader2.fontSize = 32;
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Runtime/Configuration/WitApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected override WitRequest OnCreateRequest()
return witConfiguration.GetAppRequest(id);
}

protected override void UpdateData(WitResponseNode appWitResponse)
public override void UpdateData(WitResponseNode appWitResponse)
{
id = appWitResponse["id"].Value;
name = appWitResponse["name"].Value;
Expand Down
73 changes: 73 additions & 0 deletions Scripts/Runtime/Configuration/WitConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,78 @@ private void UpdateEntityList(WitResponseNode entityListWitResponse)
entity.UpdateData();
}
}

#if UNITY_EDITOR
/// <summary>
/// Gets the app info and client id that is associated with the server token being used
/// </summary>
/// <param name="action"></param>
public void FetchAppConfigFromServerToken(Action action)
{
FetchApplicationFromServerToken(() =>
{
FetchClientToken(() =>
{
UpdateData(action);
});
});
}

private void FetchApplicationFromServerToken(Action response)
{
var listRequest = this.ListAppsRequest(10000);
listRequest.onResponse = (r) =>
{
if (r.StatusCode == 200)
{
var applications = r.ResponseData.AsArray;
for (int i = 0; i < applications.Count; i++)
{
if (applications[i]["is_app_for_token"].AsBool)
{
if (null != application)
{
application.UpdateData(applications[i]);
}
else
{
application = WitApplication.FromJson(applications[i]);
}

break;
}
}

response?.Invoke();
}
else
{
Debug.LogError(r.StatusDescription);
}
};
listRequest.Request();
}

private void FetchClientToken(Action action)
{
if (!string.IsNullOrEmpty(application?.id))
{
var tokenRequest = this.GetClientToken(application.id);
tokenRequest.onResponse = (r) =>
{
if (r.StatusCode == 200)
{
clientAccessToken = r.ResponseData["client_token"];
action?.Invoke();
}
else
{
Debug.LogError(r.StatusDescription);
}
};
tokenRequest.Request();
}
}
#endif
}
}
2 changes: 1 addition & 1 deletion Scripts/Runtime/Configuration/WitConfigurationData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ private void OnUpdateData(WitRequest request, Action onUpdateComplete)
onUpdateComplete?.Invoke();
}

protected abstract void UpdateData(WitResponseNode data);
public abstract void UpdateData(WitResponseNode data);
}
}
2 changes: 1 addition & 1 deletion Scripts/Runtime/Configuration/WitEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected override WitRequest OnCreateRequest()
return witConfiguration.GetEntityRequest(name);
}

protected override void UpdateData(WitResponseNode entityWitResponse)
public override void UpdateData(WitResponseNode entityWitResponse)
{
id = entityWitResponse["id"].Value;
name = entityWitResponse["name"].Value;
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Runtime/Configuration/WitIntent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected override WitRequest OnCreateRequest()
return witConfiguration.GetIntentRequest(name);
}

protected override void UpdateData(WitResponseNode entityWitResponse)
public override void UpdateData(WitResponseNode entityWitResponse)
{
id = entityWitResponse["id"].Value;
name = entityWitResponse["name"].Value;
Expand Down
Loading

0 comments on commit 1df8d09

Please sign in to comment.