Esempio n. 1
0
        public async Task <IntradayData> GetIntraDayTimeSeriesAsync(IntradayResourceType timeSeriesResourceType, DateTime dayAndStartTime, TimeSpan intraDayTimeSpan)
        {
            string apiCall;

            if (intraDayTimeSpan > new TimeSpan(0, 1, 0) &&                       //the timespan is greater than a minute
                dayAndStartTime.Day == dayAndStartTime.Add(intraDayTimeSpan).Day) //adding the timespan doesn't go in to the next day
            {
                apiCall = string.Format("/1/user/-{0}/date/{1}/1d/time/{2}/{3}.json",
                                        timeSeriesResourceType.GetStringValue(),
                                        dayAndStartTime.ToFitbitFormat(),
                                        dayAndStartTime.ToString("HH:mm"),
                                        dayAndStartTime.Add(intraDayTimeSpan).ToString("HH:mm"));
            }
            else //just get the today data, there was a date specified but the timerange was likely too large or negative
            {
                apiCall = string.Format("/1/user/-{0}/date/{1}/1d.json",
                                        timeSeriesResourceType.GetStringValue(),
                                        dayAndStartTime.ToFitbitFormat());
            }

            apiCall = FitbitClientHelperExtensions.ToFullUrl(apiCall);

            HttpResponseMessage response = await HttpClient.GetAsync(apiCall);

            await HandleResponse(response);

            string responseBody = await response.Content.ReadAsStringAsync();

            var serializer = new JsonDotNetSerializer {
                RootProperty = timeSeriesResourceType.ToTimeSeriesProperty()
            };

            return(serializer.GetIntradayTimeSeriesData(responseBody));
        }
Esempio n. 2
0
        public IntradayData GetIntraDayTimeSeries(IntradayResourceType timeSeriesResourceType, DateTime dayAndStartTime, TimeSpan intraDayTimeSpan)
        {
            string requestUrl = null;

            if (intraDayTimeSpan > new TimeSpan(0, 1, 0) &&                      //the timespan is greater than a minute
                dayAndStartTime.Day == dayAndStartTime.Add(intraDayTimeSpan).Day //adding the timespan doesn't go in to the next day
                )
            {
                requestUrl = string.Format("/1/user/-{0}/date/{1}/1d/time/{2}/{3}.xml",
                                           timeSeriesResourceType.GetStringValue(),
                                           dayAndStartTime.ToString("yyyy-MM-dd"),
                                           dayAndStartTime.ToString("HH:mm"),
                                           dayAndStartTime.Add(intraDayTimeSpan).ToString("HH:mm"));
            }
            else //just get the today data, there was a date specified but the timerange was likely too large or negative
            {
                requestUrl = string.Format("/1/user/-{0}/date/{1}/1d.xml",
                                           timeSeriesResourceType.GetStringValue(),
                                           dayAndStartTime.ToString("yyyy-MM-dd"));
            }
            //                /1/user/-/activities/calories/date/2011-07-05/1d/time/12:20/12:45.xml
            RestRequest request = new RestRequest(requestUrl);

            request.OnBeforeDeserialization = resp =>
            {
                XDocument doc = XDocument.Parse(resp.Content);

                //find the name of the 2nd level element that contains "-intraday" and set it as the rootElement to start parsing through
                var rootElement = doc.Descendants("result").FirstOrDefault().Descendants().Where(t => t.Name.LocalName.Contains("-intraday")).FirstOrDefault();

                //sometimes the API doesn't return that node, for isnstance a date queried before the start of an account
                if (rootElement != null && !string.IsNullOrWhiteSpace(rootElement.Name.LocalName))
                {
                    request.RootElement = rootElement.Name.LocalName;
                }
            };

            var response = restClient.Execute <IntradayData>(request);

            HandleResponse(response);

            //after the deserialization, need to set the date parts correctly
            for (int i = 0; i < response.Data.DataSet.Count; i++)
            {
                //the serializing gets the time right, but we have to set the explicit time part from passed in
                response.Data.DataSet[i].Time = new DateTime(
                    dayAndStartTime.Year,
                    dayAndStartTime.Month,
                    dayAndStartTime.Day,
                    response.Data.DataSet[i].Time.Hour,
                    response.Data.DataSet[i].Time.Minute,
                    response.Data.DataSet[i].Time.Second);
            }


            return(response.Data);
        }
Esempio n. 3
0
        public async Task <IntradayData> GetIntraDayTimeSeriesAsync(IntradayResourceType timeSeriesResourceType, DateTime dayAndStartTime, TimeSpan intraDayTimeSpan)
        {
            string apiCall;

            if (intraDayTimeSpan > new TimeSpan(0, 1, 0) &&                       //the timespan is greater than a minute
                dayAndStartTime.Day == dayAndStartTime.Add(intraDayTimeSpan).Day) //adding the timespan doesn't go in to the next day
            {
                apiCall = string.Format("/1/user/-{0}/date/{1}/1d/time/{2}/{3}.json",
                                        timeSeriesResourceType.GetStringValue(),
                                        dayAndStartTime.ToFitbitFormat(),
                                        dayAndStartTime.ToString("HH:mm"),
                                        dayAndStartTime.Add(intraDayTimeSpan).ToString("HH:mm"));
            }
            else //just get the today data, there was a date specified but the timerange was likely too large or negative
            {
                apiCall = string.Format("/1/user/-{0}/date/{1}/1d.json",
                                        timeSeriesResourceType.GetStringValue(),
                                        dayAndStartTime.ToFitbitFormat());
            }

            apiCall = FitbitClientHelperExtensions.ToFullUrl(apiCall);

            HttpResponseMessage response = await HttpClient.GetAsync(apiCall);

            await HandleResponse(response);

            string responseBody = await response.Content.ReadAsStringAsync();

            if (string.IsNullOrWhiteSpace(responseBody))
            {
                throw new FitbitRequestException(response, null, "The Intraday data response body was null");
            }

            var serializer = new JsonDotNetSerializer {
                RootProperty = timeSeriesResourceType.ToTimeSeriesProperty()
            };

            IntradayData data = null;

            try
            {
                data = serializer.GetIntradayTimeSeriesData(responseBody);
            }
            catch (Exception ex)
            {
                FitbitRequestException fEx = new FitbitRequestException(response, null, "Serialization Error in GetIntradayTimeSeriesData", ex);
                throw fEx;
            }

            return(data);
        }
        /// <summary>
        /// Converts the timeseries resource into the property to acces the data
        /// </summary>
        /// <param name="timeseriesResource"></param>
        /// <returns></returns>
        internal static string ToTimeSeriesProperty(this IntradayResourceType timeseriesResource)
        {
            var timeSeriesResourceDisplay = timeseriesResource.GetStringValue();
            if (timeSeriesResourceDisplay.StartsWith("/"))
            {
                timeSeriesResourceDisplay = timeSeriesResourceDisplay.TrimStart(new[] { '/' });
            }

            return timeSeriesResourceDisplay.Replace("/", "-");
        }
Esempio n. 5
0
        public IntradayData GetIntraDayTimeSeries(IntradayResourceType timeSeriesResourceType, DateTime dayAndStartTime, TimeSpan intraDayTimeSpan)
        {

            string requestUrl = null;

            if (intraDayTimeSpan > new TimeSpan(0, 1, 0) && //the timespan is greater than a minute
                dayAndStartTime.Day == dayAndStartTime.Add(intraDayTimeSpan).Day //adding the timespan doesn't go in to the next day
            )
            { 
                requestUrl = string.Format("/1/user/-{0}/date/{1}/1d/time/{2}/{3}.xml", 
                                        timeSeriesResourceType.GetStringValue(), 
                                        dayAndStartTime.ToString("yyyy-MM-dd"), 
                                        dayAndStartTime.ToString("HH:mm"), 
                                        dayAndStartTime.Add(intraDayTimeSpan).ToString("HH:mm"));
            }
            else //just get the today data, there was a date specified but the timerange was likely too large or negative
            {
                requestUrl = string.Format("/1/user/-{0}/date/{1}/1d.xml", 
                                        timeSeriesResourceType.GetStringValue(), 
                                        dayAndStartTime.ToString("yyyy-MM-dd"));
            }
            //                /1/user/-/activities/calories/date/2011-07-05/1d/time/12:20/12:45.xml
            RestRequest request = new RestRequest(requestUrl);

            request.OnBeforeDeserialization = resp =>
            {
                XDocument doc = XDocument.Parse(resp.Content);
                
                //find the name of the 2nd level element that contains "-intraday" and set it as the rootElement to start parsing through
                var rootElement = doc.Descendants("result").FirstOrDefault().Descendants().Where(t => t.Name.LocalName.Contains("-intraday")).FirstOrDefault();

                //sometimes the API doesn't return that node, for isnstance a date queried before the start of an account 
                if(rootElement != null && !string.IsNullOrWhiteSpace(rootElement.Name.LocalName))
                    request.RootElement = rootElement.Name.LocalName;

            };

            var response = restClient.Execute<IntradayData>(request);

            HandleResponse(response);

            //after the deserialization, need to set the date parts correctly
            for(int i=0; i < response.Data.DataSet.Count; i++)
            {
                //the serializing gets the time right, but we have to set the explicit time part from passed in
                response.Data.DataSet[i].Time = new DateTime(
                    dayAndStartTime.Year,
                    dayAndStartTime.Month,
                    dayAndStartTime.Day,
                    response.Data.DataSet[i].Time.Hour,
                    response.Data.DataSet[i].Time.Minute,
                    response.Data.DataSet[i].Time.Second);
            }


            return response.Data;

        }