Search code examples
c#asp.netfitbit

Fitbit get distance data in miles not kilometers


I have a sample project that gets data from Fitbit but the information comes in metric system not imperial system. Here is the code that pulls a list of Dates and Strings pair using TimeSeriesDataList class, but in kilometers not miles. How can I modify this code to get the data in miles instead of kilometers? Any help is greatly appreciated.

public class TimeSeriesDataList
{
    public List<Data> DataList { get; set; }

    public class Data
    {
        public DateTime DateTime { get; set; }
        public string Value { get; set; }
    }
}

public class FitbitClient : IFitbitClient
{
    /// <summary>
    /// Get TimeSeries data for another user accessible with this user's credentials
    /// </summary>
    /// <param name="timeSeriesResourceType"></param>
    /// <param name="startDate"></param>
    /// <param name="endDate"></param>
    /// <param name="userId"></param>
    /// <returns></returns>
    private TimeSeriesDataList GetTimeSeries(TimeSeriesResourceType timeSeriesResourceType, DateTime baseDate, string endDateOrPeriod, string userId)
    {
        string userSignifier = "-"; //used for current user
        if (!string.IsNullOrWhiteSpace(userId))
            userSignifier = userId;

        // Here is where I believe I need to make a change, somehow, so that
        // data that comes in when this is string is requested, comes in metric units

        string requestUrl = string.Format("/1/user/{0}{1}/date/{2}/{3}.xml", userSignifier, StringEnum.GetStringValue(timeSeriesResourceType), baseDate.ToString("yyyy-MM-dd"), endDateOrPeriod);

        RestRequest request = new RestRequest(requestUrl);

        request.OnBeforeDeserialization = resp => {
            XDocument doc = XDocument.Parse(resp.Content);
            //IEnumerable<XElement> links = doc.Descendants("result");
            var rootElement = doc.Descendants("result").FirstOrDefault().Descendants().FirstOrDefault();

            if (rootElement != null && !string.IsNullOrWhiteSpace(rootElement.Name.LocalName))
                request.RootElement = rootElement.Name.LocalName;
        };

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

        HandleResponse(response);
        return response.Data;
    }
}

EDIT I am looking to get the data already converted by adding something along the lines of "Accept-Language" to the header, but I do not know how to utilize that concept. Using conversion crossed my mind but at this time I would like to see if there is an easier way by adding a simple header rather than creating a conversion class for every scenario, class holding the data, for each distinct region, etc.


Solution

  • I did a quick search and am assuming that RestRequest is the class from the RestSharp library. If so, according to their web site (http://restsharp.org) you would do something like:

    request.AddHeader("Accept-Language", "...");
    

    Not sure what the correct value should be, you'll need to figure that out from FitBit's API documentation.