Ejemplo n.º 1
0
        /// <summary>
        /// Set the Goals for the current logged in user; individual goals, multiple or all can be specified in one call.</summary>
        /// <param name="caloriesOut"></param>
        /// <param name="distance"></param>
        /// <param name="floors"></param>
        /// <param name="steps"></param>
        /// <param name="activeMinutes"></param>
        /// <returns></returns>
        public async Task <ActivityGoals> SetGoalsAsync(int caloriesOut = default(int), decimal distance = default(decimal), int floors = default(int), int steps = default(int), int activeMinutes = default(int))
        {
            // parameter checking; at least one needs to be specified
            if ((caloriesOut == default(int)) &&
                (distance == default(decimal)) &&
                (floors == default(int)) &&
                (steps == default(int)) &&
                (activeMinutes == default(int)))
            {
                throw new ArgumentException("Unable to call SetGoalsAsync without specifying at least one goal parameter to set.");
            }

            var messageContentParameters = new Dictionary <string, string>();

            if (caloriesOut != default(int))
            {
                messageContentParameters.Add("caloriesOut", caloriesOut.ToString());
            }

            if (distance != default(decimal))
            {
                messageContentParameters.Add("distance", distance.ToString());
            }

            if (floors != default(int))
            {
                messageContentParameters.Add("floors", floors.ToString());
            }

            if (steps != default(int))
            {
                messageContentParameters.Add("steps", steps.ToString());
            }

            if (activeMinutes != default(int))
            {
                messageContentParameters.Add("activeMinutes", activeMinutes.ToString());
            }

            var apiCall = FitbitClientHelperExtensions.ToFullUrl("/1/user/-/activities/goals/daily.json");

            //caloriesOut=100&distance=1.0&floors=1&steps=8000&activeMinutes=10
            HttpResponseMessage response = await HttpClient.PostAsync(apiCall, new FormUrlEncodedContent(messageContentParameters));

            await HandleResponse(response);

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

            var seralizer = new JsonDotNetSerializer {
                RootProperty = "goals"
            };

            return(seralizer.Deserialize <ActivityGoals>(responseBody));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// GetFriends has to do some custom manipulation with the returned representation
        /// </summary>
        /// <param name="serializer"></param>
        /// <param name="friendsJson"></param>
        /// <returns></returns>
        internal static List <UserProfile> GetFriends(this JsonDotNetSerializer serializer, string friendsJson)
        {
            if (string.IsNullOrWhiteSpace(friendsJson))
            {
                throw new ArgumentNullException(nameof(friendsJson), "friendsJson can not be empty, null or whitespace.");
            }

            serializer.RootProperty = "user";
            var friends = JToken.Parse(friendsJson)["friends"];

            return(friends.Children().Select(serializer.Deserialize <UserProfile>).ToList());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// GetWeight has to doe some custom manipulation with the returned representation
        /// </summary>
        /// <param name="serializer"></param>
        /// <param name="weightJson"></param>
        /// <returns></returns>
        internal static Weight GetWeight(this JsonDotNetSerializer serializer, string weightJson)
        {
            if (string.IsNullOrWhiteSpace(weightJson))
            {
                throw new ArgumentNullException(nameof(weightJson), "weightJson can not be empty, null or whitespace");
            }

            var weightlogs = JToken.Parse(weightJson)["weight"];
            var weight     = new Weight();

            weight.Weights = weightlogs.Children().Select(serializer.Deserialize <WeightLog>).ToList();
            return(weight);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Requests the lifetime activity details of the encoded user id or none supplied the current logged in user
        /// </summary>
        /// <param name="encodedUserId">encoded user id, can be null for current logged in user</param>
        /// <returns></returns>
        public async Task <ActivitiesStats> GetActivitiesStatsAsync(string encodedUserId = null)
        {
            string apiCall = FitbitClientHelperExtensions.ToFullUrl("/1/user/{0}/activities.json", encodedUserId);
            HttpResponseMessage response = await HttpClient.GetAsync(apiCall);

            await HandleResponse(response);

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

            var serializer = new JsonDotNetSerializer();

            return(serializer.Deserialize <ActivitiesStats>(responseBody));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Get the set body measurements for the date value and user specified
        /// </summary>
        /// <param name="date"></param>
        /// <param name="encodedUserId"></param>
        /// <returns></returns>
        public async Task <BodyMeasurements> GetBodyMeasurementsAsync(DateTime date, string encodedUserId = default(string))
        {
            string apiCall = FitbitClientHelperExtensions.ToFullUrl("/1/user/{0}/body/date/{1}.json", encodedUserId, date.ToFitbitFormat());
            HttpResponseMessage response = await HttpClient.GetAsync(apiCall);

            await HandleResponse(response);

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

            var serializer = new JsonDotNetSerializer();

            return(serializer.Deserialize <BodyMeasurements>(responseBody));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// GetFat has to doe some custom manipulation with the returned representation
        /// </summary>
        /// <param name="serializer"></param>
        /// <param name="fatJson"></param>
        /// <returns></returns>
        internal static Fat GetFat(this JsonDotNetSerializer serializer, string fatJson)
        {
            if (string.IsNullOrWhiteSpace(fatJson))
            {
                throw new ArgumentNullException(nameof(fatJson), "fatJson can not be empty, null or whitespace");
            }

            var fatlogs = JToken.Parse(fatJson)["fat"];
            var fat     = new Fat();

            fat.FatLogs = fatlogs.Children().Select(serializer.Deserialize <FatLog>).ToList();
            return(fat);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Gets the water date for the specified date - https://wiki.fitbit.com/display/API/API-Get-Water
        /// </summary>
        /// <remarks>
        /// GET https://api.fitbit.com/1/user/-/foods/log/water/date/yyyy-mm-dd.json
        /// </remarks>
        /// <param name="date"></param>
        /// <returns></returns>
        public async Task <WaterData> GetWaterAsync(DateTime date)
        {
            string apiCall = FitbitClientHelperExtensions.ToFullUrl("/1/user/{0}/foods/log/water/date/{1}.json", args: date.ToFitbitFormat());

            HttpResponseMessage response = await HttpClient.GetAsync(apiCall);

            await HandleResponse(response);

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

            var serializer = new JsonDotNetSerializer();

            return(serializer.Deserialize <WaterData>(responseBody));
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Requests the friends of the encoded user id or if none supplied the current logged in user
        /// </summary>
        /// <param name="encodedUserId">encoded user id, can be null for current logged in user</param>
        /// <returns>List of <see cref="UserProfile"/></returns>
        public async Task <List <UserProfile> > GetFriendsAsync(string encodedUserId = default(string))
        {
            string apiCall = FitbitClientHelperExtensions.ToFullUrl("/1/user/{0}/friends.json", encodedUserId);

            HttpResponseMessage response = await HttpClient.GetAsync(apiCall);

            await HandleResponse(response);

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

            var serializer = new JsonDotNetSerializer();

            return(serializer.GetFriends(responseBody));
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Requests the devices for the current logged in user
        /// </summary>
        /// <returns>List of <see cref="Device"/></returns>
        public async Task <List <Device> > GetDevicesAsync()
        {
            var apiCall = FitbitClientHelperExtensions.ToFullUrl("/1/user/-/devices.json");

            HttpResponseMessage response = await HttpClient.GetAsync(apiCall);

            await HandleResponse(response);

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

            var serializer = new JsonDotNetSerializer();

            return(serializer.Deserialize <List <Device> >(responseBody));
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Requests the activity details of the encoded user id or if none supplied the current logged in user for the specified date
        /// </summary>
        /// <param name="activityDate"></param>
        /// <param name="encodedUserId">encoded user id, can be null for current logged in user</param>
        /// <returns>FitbitResponse of <see cref="ActivitySummary"/></returns>
        public async Task <Activity> GetDayActivityAsync(DateTime activityDate, string encodedUserId = null)
        {
            string apiCall = FitbitClientHelperExtensions.ToFullUrl("/1/user/{0}/activities/date/{1}.json", encodedUserId, activityDate.ToFitbitFormat());

            HttpResponseMessage response = await HttpClient.GetAsync(apiCall);

            await HandleResponse(response);

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

            var serializer = new JsonDotNetSerializer();

            return(serializer.Deserialize <Activity>(responseBody));
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Gets a list of the current subscriptions for the current logged in user
        /// </summary>
        /// <returns></returns>
        public async Task <List <ApiSubscription> > GetSubscriptionsAsync()
        {
            string apiCall = FitbitClientHelperExtensions.ToFullUrl("/1/user/-/apiSubscriptions.json");
            HttpResponseMessage response = await HttpClient.GetAsync(apiCall);

            await HandleResponse(response);

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

            var serializer = new JsonDotNetSerializer {
                RootProperty = "apiSubscriptions"
            };

            return(serializer.Deserialize <List <ApiSubscription> >(responseBody));
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Get TimeSeries data for another user accessible with this user's credentials
        /// </summary>
        /// <param name="timeSeriesResourceType"></param>
        /// <param name="baseDate"></param>
        /// <param name="endDateOrPeriod"></param>
        /// <param name="encodedUserId"></param>
        /// <returns></returns>
        private async Task <TimeSeriesDataListInt> GetTimeSeriesIntAsync(TimeSeriesResourceType timeSeriesResourceType, DateTime baseDate, string endDateOrPeriod, string encodedUserId)
        {
            var apiCall = FitbitClientHelperExtensions.ToFullUrl("/1/user/{0}{1}/date/{2}/{3}.json", encodedUserId, timeSeriesResourceType.GetStringValue(), baseDate.ToFitbitFormat(), endDateOrPeriod);

            HttpResponseMessage response = await HttpClient.GetAsync(apiCall);

            await HandleResponse(response);

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

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

            return(serializer.GetTimeSeriesDataListInt(responseBody));
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Requests the user profile of the encoded user id or if none specified the current logged in user
        /// </summary>
        /// <param name="encodedUserId"></param>
        /// <returns><see cref="UserProfile"/></returns>
        public async Task <UserProfile> GetUserProfileAsync(string encodedUserId = default(string))
        {
            string apiCall = FitbitClientHelperExtensions.ToFullUrl("/1/user/{0}/profile.json", encodedUserId);

            HttpResponseMessage response = await HttpClient.GetAsync(apiCall);

            await HandleResponse(response);

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

            var serializer = new JsonDotNetSerializer {
                RootProperty = "user"
            };

            return(serializer.Deserialize <UserProfile>(responseBody));
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Requests the sleep data for the specified date for the logged in user
        /// </summary>
        /// <param name="sleepDate"></param>
        /// <returns></returns>
        public async Task <SleepData> GetSleepAsync(DateTime sleepDate)
        {
            string apiCall = FitbitClientHelperExtensions.ToFullUrl("/1/user/{0}/sleep/date/{1}.json", args: sleepDate.ToFitbitFormat());

            HttpResponseMessage response = await HttpClient.GetAsync(apiCall);

            await HandleResponse(response);

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

            var serializer = new JsonDotNetSerializer();
            var data       = serializer.Deserialize <SleepData>(responseBody);

            FitbitClientExtensions.ProcessSleepData(data);
            return(data);
        }
Ejemplo n.º 15
0
        public static OAuth2AccessToken ParseAccessTokenResponse(string responseString)
        {
            // assumption is the errors json will return in usual format eg. errors array
            JObject responseObject = JObject.Parse(responseString);

            var error = responseObject["errors"];

            if (error != null)
            {
                var errors = new JsonDotNetSerializer().ParseErrors(responseString);
                throw new FitbitException($"Unable to parse token response in method -- {nameof(ParseAccessTokenResponse)}.", errors);
            }

            var deserializer = new JsonDotNetSerializer();

            return(deserializer.Deserialize <OAuth2AccessToken>(responseString));
        }
Ejemplo n.º 16
0
        /// <summary>
        /// GetTimeSeriesDataList has to do some custom manipulation with the returned representation
        /// </summary>
        /// <param name="serializer"></param>
        /// <param name="timeSeriesDataJson"></param>
        /// <returns></returns>
        internal static TimeSeriesDataList GetTimeSeriesDataList(this JsonDotNetSerializer serializer, string timeSeriesDataJson)
        {
            if (string.IsNullOrWhiteSpace(timeSeriesDataJson))
            {
                throw new ArgumentNullException(nameof(timeSeriesDataJson), "timeSeriesDataJson can not be empty, null or whitespace.");
            }

            var dataPoints = JToken.Parse(timeSeriesDataJson)[serializer.RootProperty];
            var result     = new TimeSeriesDataList
            {
                DataList = (from item in dataPoints
                            select new TimeSeriesDataList.Data
                {
                    DateTime = DateTime.Parse(item["dateTime"].ToString()),
                    Value = item["value"].ToString()
                }).ToList()
            };

            return(result);
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Logs the specified WaterLog item for the current logged in user - https://wiki.fitbit.com/display/API/API-Log-Water
        /// </summary>
        /// <param name="date"></param>
        /// <param name="log"></param>
        /// <returns></returns>
        public async Task <WaterLog> LogWaterAsync(DateTime date, WaterLog log)
        {
            string apiCall = FitbitClientHelperExtensions.ToFullUrl("/1/user/-/foods/log/water.json");

            var items = new Dictionary <string, string>();

            items.Add("amount", log.Amount.ToString());
            items.Add("date", date.ToFitbitFormat());
            apiCall = string.Format("{0}?{1}", apiCall, string.Join("&", items.Select(x => string.Format("{0}={1}", x.Key, x.Value))));

            HttpResponseMessage response = await HttpClient.PostAsync(apiCall, new StringContent(string.Empty));

            await HandleResponse(response);

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

            var serializer = new JsonDotNetSerializer {
                RootProperty = "waterLog"
            };

            return(serializer.Deserialize <WaterLog>(responseBody));
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Add subscription
        /// </summary>
        /// <param name="apiCollectionType"></param>
        /// <param name="uniqueSubscriptionId"></param>
        /// <param name="subscriberId"></param>
        /// <returns></returns>
        public async Task <ApiSubscription> AddSubscriptionAsync(APICollectionType apiCollectionType, string uniqueSubscriptionId, string subscriberId = default(string))
        {
            string path     = FormatKey(apiCollectionType, Constants.Formatting.TrailingSlash);
            string resource = FormatKey(apiCollectionType, Constants.Formatting.LeadingDash);

            string url     = "/1/user/-/{1}apiSubscriptions/{3}{2}.json";
            string apiCall = FitbitClientHelperExtensions.ToFullUrl(url, args: new object[] { path, resource, uniqueSubscriptionId });

            if (!string.IsNullOrWhiteSpace(subscriberId))
            {
                HttpClient.DefaultRequestHeaders.Add(Constants.Headers.XFitbitSubscriberId, subscriberId);
            }

            HttpResponseMessage response = await HttpClient.PostAsync(apiCall, new StringContent(string.Empty));

            await HandleResponse(response);

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

            var serializer = new JsonDotNetSerializer();

            return(serializer.Deserialize <ApiSubscription>(responseBody));
        }
Ejemplo n.º 19
0
        /// <summary>
        /// General error checking of the response before specific processing is done.
        /// </summary>
        /// <param name="response"></param>
        private async Task HandleResponse(HttpResponseMessage response)
        {
            if (!response.IsSuccessStatusCode)
            {
                // assumption is error response from fitbit in the 4xx range
                var errors = new JsonDotNetSerializer().ParseErrors(await response.Content.ReadAsStringAsync());

                // rate limit hit
                if (429 == (int)response.StatusCode)
                {
                    // not sure if we can use 'RetryConditionHeaderValue' directly as documentation is minimal for the header
                    var retryAfterHeader = response.Headers.GetValues("Retry-After").FirstOrDefault();
                    if (retryAfterHeader != null)
                    {
                        int retryAfter;
                        if (int.TryParse(retryAfterHeader, out retryAfter))
                        {
                            throw new FitbitRateLimitException(retryAfter, errors);
                        }
                    }
                }

                // request exception parsing
                switch (response.StatusCode)
                {
                case HttpStatusCode.BadRequest:
                case HttpStatusCode.Unauthorized:
                case HttpStatusCode.Forbidden:
                case HttpStatusCode.NotFound:
                    throw new FitbitRequestException(response, errors);
                }

                // if we've got here then something unexpected has occured
                throw new FitbitException($"An error has occured. Please see error list for details - {response.StatusCode}", errors);
            }
        }
        private bool IsTokenStale(string responseBody)
        {
            var errors = new JsonDotNetSerializer().ParseErrors(responseBody);

            return(errors.Any(error => error.ErrorType == "expired_token"));
        }