/// <summary> /// Coroutine for sending an xAPI Statement. /// /// This version requires a constructed Statement object. /// </summary> /// <param name="statement"></param> /// <param name="callback"></param> /// <returns></returns> public static IEnumerator SendMultiStatementRoutine(Statement[] statements, Action <MultiStatementStoredResponse> callback = null) { // Serialize this thing then convert into a utf-8 byte array var payload = JsonConvert.SerializeObject(statements); var request = XAPIMessenger.BuildPostRequest(XAPIMessenger.Config.StatementEndpoint, payload); // Yield during the request yield return(request.SendWebRequest()); // Use the callback if supplied if (callback != null) { // Check for the statement IDs string[] statementKeys = null; try { statementKeys = JsonConvert.DeserializeObject <string[]>(request.downloadHandler.text); } catch (Exception ex) { Debug.LogErrorFormat("ERROR: Response Code: {0}", request.responseCode); Debug.LogErrorFormat("ERROR: Could not deserialize LRS response. Expected JSON string array, received: {0}", request.downloadHandler.text); Debug.LogErrorFormat("ERROR: Exception: {0}", ex.Message); } callback(new MultiStatementStoredResponse { Request = request, Statements = statements, StatementIDs = statementKeys }); } }
public override void OnInspectorGUI() { // Get our component this.messenger = (target as XAPIMessenger); // Tell people what it does EditorGUILayout.HelpBox( "Primary component when using this Unity xAPI Wrapper." + "\n\nThis component will send web requests using Unity's coroutine system and its cross-platform Web Request library." + "\n\nIf this component does not exist when needed, the Wrapper will create one." , MessageType.Info ); // Expose the private ConfigObject field base.OnInspectorGUI(); GUI.enabled = false; EditorGUI.indentLevel = 2; // Make sure we have a configuration if (this.messenger.LocalConfigObject != null) { // Sanity var config = this.messenger.LocalConfigObject.config; EditorGUILayout.TextField("-LRS Endpoint", config.Endpoint); EditorGUILayout.IntField("-Timeout (s)", config.Timeout); // The main area of interest is changing how we authenticate with the LRS switch (config.AuthMethod) { default: case LRSAuthMethod.Basic: EditorGUILayout.TextField("-Username", config.Username); EditorGUILayout.TextField("-Password", config.Password); break; case LRSAuthMethod.BasicEncoded: EditorGUILayout.TextField("-Basic Auth", config.BasicAuth); break; } // Just show the values based on their auth method } // Warn them if we haven't gotten a configuration yet... else { EditorGUILayout.HelpBox( "An LRS configuration has not been supplied!" + "\n\nYou must supply an LRS configuration to use this library." + "\n\nRight-click within the inspector and select:" + "\n\n Create > XAPI > New LRS Configuration" , MessageType.Error ); } GUI.enabled = true; EditorGUI.indentLevel = 0; }
/// <summary> /// Send the given statement to the configured LRS. /// </summary> /// <param name="statements">Statement to send.</param> /// <param name="callback">Optional callback for once this operation ends.</param> public static void SendStatement(Statement statement, Action <StatementStoredResponse> callback = null) { // Make sure we have a messenger. Each overload of this function eventually // uses this version, so we only need to check for a messenger here. // XAPIWrapper.CheckMessenger(); XAPIMessenger.SendStatement(statement, callback); }
/// <summary> /// Assign an LRS configuration to this wrapper. This configuration will be used for /// all statements sent after assignment. /// </summary> /// <param name="config"></param> public static void AssignConfig(Config config) { // Make sure our messenger exists XAPIWrapper.CheckMessenger(); // Relay this to it XAPIMessenger.AssignConfig(config); }
/// <summary> /// Retrieves a single statement from the configured LRS. /// /// As you can't overload a function with identical argument types, this is private with /// a slightly different name. /// </summary> /// <param name="url"></param> /// <param name="callback"></param> private static void GetStatement(StatementQuery query, Action <Statement, UnityWebRequest> callback) { // Make sure we have a messenger XAPIWrapper.CheckMessenger(); // Build the fully qualified URL string url = XAPIMessenger.Config.StatementEndpoint + query.BuildQueryString();; XAPIMessenger.GetStatement(url, callback); }
/// <summary> /// Coroutine for sending an xAPI Statement. /// /// This version requires a constructed Statement object. /// </summary> /// <param name="statement"></param> /// <param name="callback"></param> /// <returns></returns> public static IEnumerator SendStatementRoutine(Statement statement, Action <StatementStoredResponse> callback = null) { var outboundArray = new Statement[] { statement }; yield return(XAPIMessenger.SendMultiStatementRoutine(outboundArray, res => { if (callback != null) { callback(new StatementStoredResponse { Request = res.Request, Statement = res.Statements == null ? null : res.Statements[0], StatementID = res.StatementIDs == null ? null : res.StatementIDs[0] }); } })); }
public static IEnumerator GetStatementRoutine(string url, Action <Statement, UnityWebRequest> callback) { // Serialize this thing then convert into a utf-8 byte array var request = XAPIMessenger.BuildGetRequest(url); // Yield during the request yield return(request.SendWebRequest()); // Return whatever we found. Statement statement = JsonConvert.DeserializeObject <Statement>(request.downloadHandler.text); // Fix IFI statement.Actor.ifi = statement.Actor.GuessIFI(); // Make the callback callback(statement, request); }
/// <summary> /// Coroutine for retrieving xAPI statements from the configured LRS. /// </summary> /// <param name="url"></param> /// <param name="callback"></param> /// <returns></returns> public static IEnumerator GetStatementsRoutine(string url, Action <StatementResult, UnityWebRequest> callback) { // Serialize this thing then convert into a utf-8 byte array var request = XAPIMessenger.BuildGetRequest(url); yield return(request.SendWebRequest()); // Return whatever we found. StatementResult result = JsonConvert.DeserializeObject <StatementResult>(request.downloadHandler.text); // Correct Actor IFI Types for (int k = 0; k < result.StatementCount; k++) { result.Statements[k].Actor.ifi = result.Statements[k].Actor.GuessIFI(); } // Make the callback callback(result, request); }
/// <summary> /// Retrieves statements using the "more" property attached to a previous statement response. /// /// This function will remove the first parts of that "more" property to fit your LRS endpoint. /// </summary> /// <param name="moreIrl"></param> /// <param name="callback"></param> public static void GetMoreStatements(string moreIrl, Action <StatementResult, UnityWebRequest> callback) { // Make sure it's formatted correctly if (moreIrl.Contains("/more/")) { // Find out where it is int index = moreIrl.IndexOf("/more/"); string more = moreIrl.Substring(index, moreIrl.Length - index); // Build the fully qualified URL string url = XAPIMessenger.Config.Endpoint + more; // Make the request XAPIMessenger.GetStatements(url, callback); } else { throw new ArgumentException("XAPIWrapper.GetMoreStatements: moreIrl argument must contain \"/more/\""); } }
// Called when this component is first created void Awake() { // Check if we already have an instance live if (XAPIMessenger.instance != null) { GameObject.Destroy(this); return; } // If we didn't, then this is it XAPIMessenger.instance = this; GameObject.DontDestroyOnLoad(this.gameObject); // Check if we were assigned a ConfigObject if (this.configObject != null) { this.config = this.configObject.config; Debug.LogFormat("LRS Configuration loaded for: {0}", this.config.Endpoint); } else { Debug.LogErrorFormat("LRS CONFIGURATION MISSING FROM XAPI MESSENGER. YOU MUST ASSIGN A CONFIGURATION."); } }
/// <summary> /// Send an array of statements to the configured LRS. /// </summary> /// <param name="statements">Statements to send.</param> /// <param name="callback">Optional callback for once this operation ends.</param> public static void SendStatements(Statement[] statements, Action <MultiStatementStoredResponse> callback = null) { XAPIWrapper.CheckMessenger(); XAPIMessenger.SendStatements(statements, callback); }
/// <summary> /// Retrieves statements according to the StatementQuery values. /// /// The StatementResult object in the callback may contain a `MoreIRL` value. If that is the case, /// then more statements matching your query can be returned using that endpoints with your LRS. /// </summary> /// <param name="result"></param> /// <param name="callback"></param> public static void GetStatements(StatementQuery query, Action <StatementResult, UnityWebRequest> callback) { string url = XAPIMessenger.Config.StatementEndpoint + query.BuildQueryString(); XAPIMessenger.GetStatements(url, callback); }
/// <summary> /// Retrieves statements from a fully-qualified URL pointing to the configured LRS. /// /// This is the most granular version of the call and should only be made by the wrapper itself. /// </summary> /// <param name="url"></param> /// <param name="callback"></param> public static void GetStatements(string url, Action <StatementResult, UnityWebRequest> callback) { // Make sure we have a messenger XAPIWrapper.CheckMessenger(); XAPIMessenger.GetStatements(url, callback); }
/// <summary> /// Retrieve statements from an LRS. /// </summary> /// <param name="url"></param> /// <param name="callback"></param> public static void GetStatement(string url, Action <Statement, UnityWebRequest> callback) { XAPIMessenger.instance.StartCoroutine(XAPIMessenger.GetStatementRoutine(url, callback)); }
/// <summary> /// Sends an xAPI statement. This accepts a full statement as an argument. /// </summary> /// <param name="statement"></param> /// <param name="callback"></param> public static void SendStatements(Statement[] statements, Action <MultiStatementStoredResponse> callback = null) { XAPIMessenger.instance.StartCoroutine(XAPIMessenger.SendMultiStatementRoutine(statements, callback)); }