Exemple #1
0
        private string MakeRequest(Uri serviceUrl, ICollection <KeyValuePair <string, object> > parameters, Action <NameValueCollection> applyExtraParameters, HttpMethod method)
        {
            var webClient       = _webClientBuilder.Create();
            var queryParameters = new NameValueCollection();
            var bodyParameters  = new Dictionary <string, object>();

            applyExtraParameters?.Invoke(queryParameters);
            if (parameters != null)
            {
                foreach (var parameter in parameters)
                {
                    if (method != HttpMethod.Get)
                    {
                        bodyParameters.Add(parameter.Key, parameter.Value);
                    }
                    else
                    {
                        var value = _parametersTranslator.ToJson(parameter);
                        queryParameters.Add(parameter.Key, value);
                    }
                }
            }
            try
            {
                return(webClient.Request(ApplyQueryParametersToUrl(serviceUrl, queryParameters), method,
                                         JsonConvert.SerializeObject(bodyParameters)));
            }
            catch (WebException ex)
            {
                throw GeneratePlatformRequestException(ex);
            }
        }
Exemple #2
0
        private T MakeRequest <T>(Uri serviceUrl, ICollection <KeyValuePair <string, object> > parameters, Action <NameValueCollection> applyExtraParameters, HttpMethod method)
        {
            var webClient             = _webClientBuilder.Create();
            var serviceParameters     = new NameValueCollection();
            var serviceParametersPost = new Dictionary <string, object>();

            if (applyExtraParameters != null)
            {
                applyExtraParameters(serviceParameters);
            }
            if (parameters != null)
            {
                foreach (var parameter in parameters)
                {
                    if (method == HttpMethod.Post)
                    {
                        serviceParametersPost.Add(parameter.Key, parameter.Value);
                    }
                    else
                    {
                        var value = _parametersTranslator.ToJson(parameter);
                        serviceParameters.Add(parameter.Key, value);
                    }
                }
            }
            try
            {
                return((method == HttpMethod.Post) ? JsonConvert.DeserializeObject <T>(webClient.UploadValues(ApplyExtraParametersToUrl(serviceUrl, serviceParameters), "POST", JsonConvert.SerializeObject(serviceParametersPost))) :
                       webClient.GetJson <T>(serviceUrl, serviceParameters));
            }
            catch (WebException ex)
            {
                throw GeneratePlatformRequestException(ex);
            }
        }