public void Merge(RequestParametersContainer pairs)
 {
     foreach (KeyValuePair <string, string> keyValuePair in pairs)
     {
         Add(keyValuePair);
     }
 }
        private static async Task <AsanaResponse <T> > GetResponse <T>(string resource, Action <HttpRequestMessage, RequestParametersContainer> action = null) where T : class, new()
        {
            var handler = new HttpClientHandler()
            {
                AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
            };

            var client = new HttpClient(handler);

            client.BaseAddress = new Uri(_asanaBaseUrl);

            var userAgent = string.Format("Mozilla/5.0 (WindowsPhone {0}; {1} {2}) Gecko/20100101 CCW/1.0",
                                          DeviceHelper.GetOSVersion(),
                                          DeviceHelper.GetSystemManufacturer(),
                                          DeviceHelper.GetSystemProductName());

            //  client.DefaultRequestHeaders.UserAgent.ParseAdd(string.Format("Mozilla/5.0 (WindowsPhone {0}; {1}) Gecko/20100101", DeviceHelper.GetOSVersion(), DeviceHelper.GetModelName()));

            client.DefaultRequestHeaders.UserAgent.ParseAdd(userAgent);

            var request = new HttpRequestMessage(HttpMethod.Get, resource);


            if (AsanaStateService.IsSetAuthToken)
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AsanaStateService.AuthToken);
            }

            var container = new RequestParametersContainer();


            if (action != null)
            {
                action(request, container);
            }

            HttpResponseMessage response = null;

            try
            {
                response = await client.SendAsync(request);

                return(new AsanaResponse <T>
                {
                    Content = await response.Content.ReadAsStringAsync(),
                    StatusCode = response.StatusCode,
                    RequestUri = resource,
                });
            }
            catch (Exception e)
            {
                return(new AsanaResponse <T>()
                {
                    ErrorException = e, ErrorMessage = e.Message, StatusCode = HttpStatusCode.NotFound
                });
            }
        }
        private static async Task <T> SendRequest <T>(string resource, HttpMethod method = null, RequestParametersContainer parameters = null) where T : AResponse, new()
        {
            var client = new HttpClient(new HttpClientHandler()
            {
                AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip
            });

            client.Timeout = TimeSpan.FromSeconds(20);
            client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
            //TODO:user agent
            //client.UserAgent = string.Format("Mozilla/5.0 (WindowsPhone {0}; {1}) Gecko/20100101", Environment.OSVersion.Version, GetDeviceModel());

            var request = new HttpRequestMessage();

            if (method == null)
            {
                method = HttpMethod.Get;
            }

            request.Method = method;


            if (parameters != null)
            {
                if (method == HttpMethod.Post)
                {
                    request.Content = new FormUrlEncodedContent(parameters);
                }
                if (method == HttpMethod.Get)
                {
                    resource += "?" + parameters.ToStringData();
                }
            }
            request.RequestUri = new Uri(_asanaBaseUrl + resource);


            //TODO: key
            request.Headers.Authorization = new AuthenticationHeaderValue(AsanaConstants.ApiKeys.TestAccount1);

            try
            {
                var response = await client.SendAsync(request);

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

                var data = JsonConvert.DeserializeObject <T>(content);

                //TODO: process errors

                return(data);
            }
            catch (Exception)
            {
                //TODO process
            }

            return(null);


            //var request = new RestRequest(resource);
            //if(AsanaStateService.IsSetApiKey)
            //{
            //    client.Authenticator = new HttpBasicAuthenticator(AsanaStateService.ApiKey, string.Empty);
            //}
            //else if (AsanaStateService.IsSetAuthToken)
            //{
            //    request.AddHeader(System.Net.HttpRequestHeader.Authorization.ToString(),
            //                      string.Format("Bearer {0}", AsanaStateService.AuthToken));
            //}
        }