Esempio n. 1
0
        public async Task <HttpResponseMessage> TriggerOnActivity(string triggerState,
                                                                  [Metadata("Timezone Offset", "Timezone offset for desired return time (e.g. -7 for Pacific Daylight Time)", VisibilityType.Default)] int offset = 0)
        {
            timezoneOffset = new TimeSpan(offset, 0, 0);
            if (string.IsNullOrEmpty(triggerState))
            {
                triggerState = DateTime.UtcNow.ToString("o");
            }
            else
            {
                triggerState = DateTime.Parse(triggerState).ToUniversalTime().ToString("o");
            }

            await tokenHandler.CheckToken();

            if (authorization == null || authorization.access_token == null)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "You are not authorized. Please go to https://{url}/authorize to authorize against Microsoft Health Service.  See the GitHub repo for details."));
            }


            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authorization.access_token);
                var result = await client.GetAsync(string.Format("https://api.microsofthealth.net/v1/me/Activities?startTime={0}", DateTime.Parse(triggerState).AddDays(-1).ToUniversalTime().ToString("o")));

                string content = await result.Content.ReadAsStringAsync();

                ActivityList resultList = JsonConvert.DeserializeObject <ActivityList>(content);
                resultList.EndTimeInclusive(DateTime.Parse(triggerState).ToUniversalTime().Add(timezoneOffset), DateTime.UtcNow.Add(timezoneOffset));
                if (resultList.NoActivities())
                {
                    return(Request.EventWaitPoll(null, triggerState));
                }
                else
                {
                    return(Request.EventTriggered(FlattenResult(resultList), DateTime.UtcNow.ToString("o")));
                }
            }
        }
Esempio n. 2
0
        public async Task <HttpResponseMessage> GetActivites([Metadata("Activities After Time", "Will return all activities that ended after the time passed in.")] string activityTime,
                                                             [Metadata("Timezone Offset", "Timezone offset for desired return time (e.g. -7 for Pacific Daylight Time)", VisibilityType.Default)] int offset = 0,
                                                             [Required(AllowEmptyStrings = true)][Metadata("Activites Before Time", "An end-time cap into the activities returned", VisibilityType.Advanced)] string endTime = null)
        {
            timezoneOffset = new TimeSpan(offset, 0, 0);
            string startTime;

            if (string.IsNullOrEmpty(activityTime))
            {
                startTime = DateTime.UtcNow.AddDays(-1).ToString("o");
            }
            else
            {
                startTime = DateTime.Parse(activityTime).ToUniversalTime().AddDays(-1).ToString("o");
            }

            endTime = (string.IsNullOrEmpty(endTime)) ? DateTime.UtcNow.ToString("o") : DateTime.Parse(endTime).ToUniversalTime().ToString("o");

            await tokenHandler.CheckToken();

            if (authorization == null || authorization.access_token == null)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "You are not authorized. Please go to https://{url}/authorize to authorize against Microsoft Health Service.  See the GitHub repo for details."));
            }

            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authorization.access_token);
                var result = await client.GetAsync(string.Format("https://api.microsofthealth.net/v1/me/Activities?startTime={0}", DateTime.Parse(startTime).ToUniversalTime().ToString("o")));

                string content = await result.Content.ReadAsStringAsync();

                ActivityList resultList = JsonConvert.DeserializeObject <ActivityList>(content);
                resultList.EndTimeInclusive(DateTime.Parse(activityTime).ToUniversalTime(), DateTime.Parse(endTime).ToUniversalTime());
                return(Request.CreateResponse <ActivityResponse>(HttpStatusCode.OK, FlattenResult(resultList)));
            }
        }