0

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.

Craig W.
  • 17,838
  • 6
  • 49
  • 82
Lukas
  • 2,885
  • 2
  • 29
  • 31
  • 2
    Mile is 1.60934km. Does that help? – dotNET Feb 26 '15 at 18:29
  • I am trying to avoid conversion and get the data already converted because there will be lots of different data pieces. For example, there is the whole British system that uses stones and pounds, then of course there is distance, and so on. Something along the lines of adding "Accept-Language" added to the header by some means. – Lukas Feb 26 '15 at 23:00

3 Answers3

2

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.

filhit
  • 2,084
  • 1
  • 21
  • 34
Craig W.
  • 17,838
  • 6
  • 49
  • 82
  • Thank you so much! Exactly what I needed. Adding request.AddHeader("Accept-Language", "en_US"); did the trick. Upvote and checked! – Lukas Feb 27 '15 at 18:56
  • Glad to help, but honestly it took me about 10 seconds of Googling to find that answer. I think I searched for "restrequest add header" and the first result was the doc page I linked to. – Craig W. Feb 27 '15 at 18:58
  • Ironically I spend a good half an hour. Maybe I was just putting in the wrong stuff, strictly focusing on API examples instead of attacking the fundamental question at hand. Whoops! :) – Lukas Mar 02 '15 at 16:24
1

Whatever value this code provides, you can simply divide it by 1.60934 to get the same distance in miles. Hope that helps you solve it.

dotNET
  • 33,414
  • 24
  • 162
  • 251
  • Oh I know I can convert it, my goal was to get it already converted. I heard there is a way by adding an "Accept-Language" header but I do not know how to do that. – Lukas Feb 26 '15 at 22:58
0
double kilometers2 = 321.9;
double miles2 = ConvertDistance.ConvertKilometersToMiles
PeonProgrammer
  • 1,445
  • 2
  • 15
  • 27
  • I am trying to get the data already converted by means of something like adding "Accept-Language" header, somehow. – Lukas Feb 26 '15 at 22:59