Пример #1
0
        async void Acquire()
        {
            var exist = new ExistClient();

            try
            {
                await exist.MaybeRefreshTokens();
            }
            catch (ExistException error)
            {
                await new MessageDialog(error.Message, "Refresh tokens failed").ShowAsync();
            }

            var content = new HttpStringContent(new JsonArray()
            {
                new JsonObject()
                {
                    { "name", JsonValue.CreateStringValue("time_in_bed") },
                    { "active", JsonValue.CreateBooleanValue(true) }
                },
                new JsonObject()
                {
                    { "name", JsonValue.CreateStringValue("sleep") },
                    { "active", JsonValue.CreateBooleanValue(true) }
                },
                new JsonObject()
                {
                    { "name", JsonValue.CreateStringValue("sleep_start") },
                    { "active", JsonValue.CreateBooleanValue(true) }
                },
                new JsonObject()
                {
                    { "name", JsonValue.CreateStringValue("sleep_end") },
                    { "active", JsonValue.CreateBooleanValue(true) }
                },
                new JsonObject()
                {
                    { "name", JsonValue.CreateStringValue("sleep_awakenings") },
                    { "active", JsonValue.CreateBooleanValue(true) }
                },
            }.Stringify(), Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");

            var response = await exist.Http.PostAsync(new Uri("https://exist.io/api/1/attributes/acquire/"), content);

            if (!response.IsSuccessStatusCode)
            {
                await new MessageDialog(response.RequestMessage.RequestUri.ToString(), response.StatusCode.ToString()).ShowAsync();
                this.Frame.GoBack();
                return;
            }

            var jsonResponse = JsonValue.Parse(await response.Content.ReadAsStringAsync());
            var failed       = jsonResponse.GetObject().GetNamedArray("failed");

            if (failed.Count > 0)
            {
                await new MessageDialog(failed.ToString(), "Failed to submit some values").ShowAsync();
                this.Frame.GoBack();
                return;
            }
        }
Пример #2
0
        private async void buttonSave_Click(object sender, RoutedEventArgs e)
        {
            var exist = new ExistClient();

            try
            {
                await exist.MaybeRefreshTokens();
            }
            catch (ExistException error)
            {
                await new MessageDialog(error.Message, "Refresh tokens failed").ShowAsync();
            }

            var jsonDate = JsonValue.CreateStringValue(DateTimeOffset.Now.ToString("yyyy-MM-dd"));
            var content  = new HttpStringContent(new JsonArray()
            {
                new JsonObject()
                {
                    { "name", JsonValue.CreateStringValue("time_in_bed") },
                    { "date", jsonDate },
                    { "value", JsonValue.CreateStringValue((Awake - Bedtime).TotalMinutes.ToString("F0")) }
                },
                new JsonObject()
                {
                    { "name", JsonValue.CreateStringValue("sleep") },
                    { "date", jsonDate },
                    { "value", JsonValue.CreateStringValue((Awake - Bedtime - AwakeTime).TotalMinutes.ToString("F0")) }
                },
                new JsonObject()
                {
                    { "name", JsonValue.CreateStringValue("sleep_start") },
                    { "date", jsonDate },
                    { "value", JsonValue.CreateStringValue(((Bedtime.TimeOfDay.TotalMinutes + 12 * 60) % (24 * 60)).ToString("F0")) }
                },
                new JsonObject()
                {
                    { "name", JsonValue.CreateStringValue("sleep_end") },
                    { "date", jsonDate },
                    { "value", JsonValue.CreateStringValue(Awake.TimeOfDay.TotalMinutes.ToString("F0")) }
                },
                new JsonObject()
                {
                    { "name", JsonValue.CreateStringValue("sleep_awakenings") },
                    { "date", jsonDate },
                    { "value", JsonValue.CreateStringValue(Awakenings.ToString("F0")) }
                },
            }.Stringify(), Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");

            var response = await exist.Http.PostAsync(new Uri("https://exist.io/api/1/attributes/update/"), content);

            if (!response.IsSuccessStatusCode)
            {
                await new MessageDialog(response.RequestMessage.RequestUri.ToString(), response.StatusCode.ToString()).ShowAsync();
                return;
            }

            var jsonResponse = JsonValue.Parse(await response.Content.ReadAsStringAsync());
            var failed       = jsonResponse.GetObject().GetNamedArray("failed");

            if (failed.Count > 0)
            {
                await new MessageDialog(failed.ToString(), "Failed to submit some values").ShowAsync();
                return;
            }

            this.Frame.GoBack();
        }