aarondcoleman / Fitbit.NET

Fitbit .NET API Client Library
MIT License
193 stars 138 forks source link

Custom Headers on Request #291

Open DanStockham opened 3 years ago

DanStockham commented 3 years ago

We need a way to set the Accept-Language headers before a request is sent out the Fitbit Web API. In particular, these support forum questions is what I am referring to:

https://community.fitbit.com/t5/Web-API-Development/Does-not-get-the-exact-weight-if-in-lbs/td-p/1605424 https://community.fitbit.com/t5/Web-API-Development/Unit-and-Value-doesnt-match-in-the-Get-Profile-response/m-p/1385899

Basically, without setting and specifying the Accept-Language header in the request, The web api will always assume metrics. Which is fine if your configure measurements are in the metric system but any other selection in your profile the values get converted and thus truncated in the process.

There needs to be a way set the headers on the request. I thought the interceptors would allow for custom headers but from looking at the source code, it's actually passing back a "fake" response and I think this is used for testing

...
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            Task<HttpResponseMessage> interceptorFakeResponse = null;
            Debug.WriteLine("Entering Http client's request message handler. Request details: {0}", request.ToString());

            if (interceptor != null)
                interceptorFakeResponse = interceptor.InterceptRequest(request, cancellationToken, FitbitClient);

            if (interceptorFakeResponse != null) //then highjack the request pipeline and return the HttpResponse returned by interceptor. Invoke Response handler at return.
            {
                //If we are faking the response, have the courtesy of setting the original HttpRequestMessage
                interceptorFakeResponse.Result.RequestMessage = request;
                return interceptorFakeResponse.ContinueWith(
                        responseTask => ResponseHandler(responseTask, cancellationToken).Result
                    );
            }
            else //Let the base object continue with the request pipeline. Invoke Response handler at return.
            {
                return base.SendAsync(request, cancellationToken).ContinueWith(
                     responseTask => ResponseHandler(responseTask, cancellationToken).Result
                 );
            }
        }
...