Example #1
0
        /// <summary>
        /// Uploads BatterySessionStorage data to Elasticsearch.
        ///
        /// The logic is:
        /// 1. BatterySessionStorage C# Object
        /// 2. JSON representation of BatterySessionStorage
        /// 3. Make a POST request to Elasticsearch Index API with this JSON
        ///    as the payload.
        /// </summary>
        /// <param name="batterySession">The data to store in Elasticsearch.</param>
        /// <param name="onStorage">An action to perform once the battery session is successfully stored.
        /// </param>
        /// <returns>IEnumerator so the uploading happens in the background.</returns>
        /// <remarks>
        /// In the JSON representation of BatterySessionStorage, C# enum values
        /// are represented by their string representations rather than by
        /// integers to avoid ambiguity when new abilities are added (e.g. today
        /// ability #3 could be selective visual, but tomorrow another
        /// ability added to the enum could have value #3).
        /// </remarks>
        private IEnumerator UploadBatterySession(BatterySessionStorage batterySession,
                                                 Action onStorage)
        {
            // We're looking to store a *new* BatterySession in the index.
            // The ID of the Elasticsearch document will be the same as
            // the BatterySessionId to make retrieval of the stored data easier.
            Uri uri = new Uri(string.Join("/",
                                          ELASTICSEARCH_ENDPOINT, ELASTICSEARCH_INDEX_NAME, "_doc",
                                          batterySession.BatterySessionId));

            // POSTing JSON usng UnityWebRequest requires dealing with raw bytes of JSON
            // See: https://forum.unity.com/threads/posting-raw-json-into-unitywebrequest.397871/#post-4693238
            string jsonifedBatterySession = JsonConvert.SerializeObject(batterySession);

            byte[] rawJson = Encoding.UTF8.GetBytes(jsonifedBatterySession);

            var request = new UnityWebRequest(uri, "POST",
                                              new DownloadHandlerBuffer(), new UploadHandlerRaw(rawJson));

            request.SetRequestHeader("Content-Type", "application/json");
            AuthenticateRequest(request);

            // Return to caller while waiting for BatterySession to be uploaded.
            yield return(request.SendWebRequest());

            if (request.isNetworkError || request.isHttpError)
            {
                Debug.LogError(request.error);
                Debug.LogError(request.downloadHandler.text);
            }
            else
            {
                Debug.Log("Successfully uploaded Battery Session to Elasticsearch.");
                onStorage?.Invoke();
            }
        }
Example #2
0
 /// <summary>
 /// Store the battery session information to the Elasticsearch backend.
 /// </summary>
 /// <param name="batterySession">Battery session object to be stored</param>
 public void Store(BatterySessionStorage batterySession, Action onStorage)
 {
     StartCoroutine(UploadBatterySession(batterySession, onStorage));
 }