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")));
                }
            }
        }