Esempio n. 1
0
        static void LoadFromCache()
        {
            try
            {
                byte[] bodyResult;
                using (FileStream reader = File.Open(Path.Combine(Application.persistentDataPath, "RemoteConfig.json"), FileMode.Open))
                {
                    bodyResult = new byte[reader.Length];
                    reader.Read(bodyResult, 0, (int)reader.Length);
                }

                byte[] headerResult;
                using (FileStream reader = File.Open(Path.Combine(Application.persistentDataPath, "RemoteConfigHeaders.json"), FileMode.Open))
                {
                    headerResult = new byte[reader.Length];
                    reader.Read(headerResult, 0, (int)reader.Length);
                }
                RawResponseReturned?.Invoke(ConfigOrigin.Cached, JsonConvert.DeserializeObject <Dictionary <string, string> >(System.Text.Encoding.ASCII.GetString(headerResult)), System.Text.Encoding.ASCII.GetString(bodyResult));
            }
            catch
            {
                RawResponseReturned?.Invoke(ConfigOrigin.Cached, null, null);
            }
        }
Esempio n. 2
0
        static void PostConfig(object userAttributes, object appAttributes)
        {
            requestStatus = ConfigRequestStatus.Pending;
            long timestamp    = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
            long rtSinceStart = (long)(Time.realtimeSinceStartup * 1000000);
            var  commonJobj   = JObject.FromObject(commonPayload);

            commonJobj.Add("t_since_start", rtSinceStart);
            JObject json = new JObject();

            json.Add("common", commonJobj);
            List <JObject> items = new List <JObject>();

            items.Add(json);
            items.Add(CreatePayloadJObjectFromValuesJObject(JObject.FromObject(deliveryPayload), "analytics.delivery.v1", timestamp));
            var deviceInfoJObj = JObject.FromObject(deviceInfoPayload);

            deviceInfoJObj.Add("t_since_start", rtSinceStart);
            items.Add(CreatePayloadJObjectFromValuesJObject(deviceInfoJObj, "analytics.deviceInfo.v1", timestamp));
            if (userAttributes == null)
            {
                items.Add(CreatePayloadJObjectFromValuesJObject(new JObject(), "analytics.deliveryUserAttributes.v1", timestamp));
            }
            else
            {
                items.Add(CreatePayloadJObjectFromValuesJObject(JObject.FromObject(userAttributes), "analytics.deliveryUserAttributes.v1", timestamp));
            }
            if (appAttributes == null)
            {
                items.Add(CreatePayloadJObjectFromValuesJObject(new JObject(), "analytics.deliveryAppAttributes.v1", timestamp));
            }
            else
            {
                items.Add(CreatePayloadJObjectFromValuesJObject(JObject.FromObject(appAttributes), "analytics.deliveryAppAttributes.v1", timestamp));
            }

            foreach (var func in requestPayloadProviders)
            {
                items.Add(func.Invoke());
            }

            var sb = new StringBuilder();

            using (var textWriter = new StringWriter(sb))
            {
                ToNewlineDelimitedJson(textWriter, items);
            }

            var jsonText = sb.ToString();

            var request = new UnityWebRequest("https://config.uca.cloud.unity3d.com", UnityWebRequest.kHttpVerbPOST);

            request.SetRequestHeader("Content-Type", "application/json");
            foreach (var headerProvider in requestHeaderProviders)
            {
                var header = headerProvider.Invoke();
                request.SetRequestHeader(header.key, header.value);
            }
            request.uploadHandler   = new UploadHandlerRaw(Encoding.UTF8.GetBytes(jsonText));
            request.downloadHandler = new DownloadHandlerBuffer();
            request.SendWebRequest().completed += (AsyncOperation op) => {
                var origin         = ConfigOrigin.Remote;
                var response       = ((UnityWebRequestAsyncOperation)op).webRequest;
                var configResponse = new ConfigResponse()
                {
                    requestOrigin = origin, status = requestStatus
                };
                if (response.isHttpError || response.isNetworkError)
                {
                    configResponse.status = ConfigRequestStatus.Failed;
                    FetchCompleted?.Invoke(configResponse);
                }
                else
                {
                    RawResponseReturned?.Invoke(origin, request.GetResponseHeaders(), request.downloadHandler.text);
                }
            };
        }