Ejemplo n.º 1
0
        /// <summary>
        /// Reads the spread sheet and callback with the results
        /// </summary>
        /// <param name="request"></param>
        /// <param name="search"></param>
        /// <param name="containsMergedCells"></param>
        /// <param name="callback"></param>
        /// <returns></returns>
        static IEnumerator Read(UnityWebRequest request, GSTU_Search search, bool containsMergedCells, UnityAction <GstuSpreadSheet> callback)
        {
            var apiKey = Config.gdr.access_token;

            if (Application.isPlaying)
            {
                // yield return new Task(CheckForRefreshToken());
                yield return(CheckForRefreshToken());
            }
#if UNITY_EDITOR
            else
            {
                yield return(EditorCoroutineRunner.StartCoroutine(CheckForRefreshToken()));
            }
#endif

            var newApiKey = Config.gdr.access_token;

            if (!apiKey.Equals(newApiKey))
            {
                request = UnityWebRequest.Get(CreateReadRequestURI(search));
            }

            using (request)
            {
                yield return(request.SendWebRequest());

                if (string.IsNullOrEmpty(request.downloadHandler.text) || request.downloadHandler.text == "{}")
                {
                    Debug.LogWarning("Unable to Retreive data from google sheets");
                    yield break;
                }

                if (request.downloadHandler.text.Contains("UNAUTHENTICATED"))
                {
                    Debug.LogWarning("Unable to Retreive data from google sheets:UNAUTHENTICATED");
                    yield break;
                }

                ValueRange rawData = LitJson.JsonMapper.ToObject <ValueRange>(request.downloadHandler.text);
                // JSON.Load(request.downloadHandler.text).Make<ValueRange>();
                var response = new GSTU_SpreadsheetResponse(rawData);

                //if it contains merged cells then process a second set of json data to know what these cells are
                if (containsMergedCells)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append("https://sheets.googleapis.com/v4/spreadsheets");
                    sb.Append("/" + search.sheetId);
                    sb.Append("?access_token=" + Config.gdr.access_token);

                    UnityWebRequest request2 = UnityWebRequest.Get(sb.ToString());

                    yield return(request2.SendWebRequest());

                    SheetsRootObject root = LitJson.JsonMapper.ToObject <SheetsRootObject>(request2.downloadHandler.text);
                    //JSON.Load(request2.downloadHandler.text).Make<SheetsRootObject>();
                    response.sheetInfo = root.sheets.FirstOrDefault(x => x.properties.title == search.worksheetName);
                }

                if (callback != null)
                {
                    var sheet = new GstuSpreadSheet(response, search.titleColumn, search.titleRow);
                    sheet.rawText = request.downloadHandler.text;
                    callback(sheet);
                }
            }
        }