コード例 #1
0
        /// <summary>
        /// Does the request.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="method">The method.</param>
        /// <param name="parameters">The parameters.</param>
        /// <param name="headers">The headers.</param>
        /// <param name="contentType">Type of the content.</param>
        /// <returns>Response deserialized as an instance of T.</returns>
        /// <exception cref="FulfillmentException">Token expired. Please logout and login again.</exception>
        public async Task <T> DoRequest(string url, string method, Dictionary <string, object> parameters, Dictionary <string, object> headers = null, string contentType = "application/json")
        {
            try
            {
                this.logger?.Info("Call Rest Service : {0}" + JsonSerializer.Serialize(new { url = url, method = method, parameters = parameters, headers = headers, contentType = contentType }));

                var accessTokenResult = await ADAuthenticationHelper.GetAccessToken(this.clientConfiguration).ConfigureAwait(false);

                if (headers == null)
                {
                    headers = new Dictionary <string, object>();
                }

                // Add bearer token
                headers.Add("Authorization", string.Format($"Bearer {accessTokenResult.AccessToken}"));

                var webRequestHelper = new WebRequestHelper(url, method, contentType);
                await webRequestHelper.PrepareDataForRequest(parameters)
                .FillHeaders(headers)
                .DoRequestAsync().ConfigureAwait(false);

                return(await webRequestHelper.BuildResultFromResponse <T>().ConfigureAwait(false));
            }
            catch (WebException ex)
            {
                this.logger?.Error(string.Format($"An error occurred while processing request to the url : {url} - {ex.ToString()}"));
                return(this.ProcessErrorResponse(url, ex));
            }
        }
        public async Task CheckAuthentication()
        {
            var accessTokenResult = await ADAuthenticationHelper.GetAccessToken(this.configuration).ConfigureAwait(false);

            Assert.IsNotNull(accessTokenResult);
            Assert.IsNotNull(accessTokenResult?.AccessToken);
        }
コード例 #3
0
        /// <summary>
        /// Does the request.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="method">The method.</param>
        /// <param name="parameters">The parameters.</param>
        /// <param name="headers">The headers.</param>
        /// <param name="contentType">Type of the content.</param>
        /// <returns>
        /// Does the request
        /// </returns>
        /// <exception cref="FulfillmentException">Token expired. Please logout and login again.</exception>
        public async Task <T> DoRequest(string url, string method, Dictionary <string, object> parameters, Dictionary <string, object> headers = null, string contentType = "application/json")
        {
            try
            {
                this.logger?.Info("Call Rest Service : {0}" + JsonConvert.SerializeObject(new { url = url, method = method, parameters = parameters, headers = headers, contentType = contentType }));
                var accessTokenResult = await ADAuthenticationHelper.GetAccessToken(this.clientConfiguration).ConfigureAwait(false);

                string formattedParams = string.Empty;
                if ((string.Equals(HttpMethods.GET.ToString(), method) || string.Equals(HttpMethods.DELETE.ToString(), method)) && parameters != null && parameters.Count() > 0)
                {
                    formattedParams = string.Join("&", parameters.Select(x => x.Key + "=" + System.Net.WebUtility.UrlEncode(x.Value.ToString())));
                    url             = string.Format("{0}?{1}", url, formattedParams);
                }
                else if ((string.Equals(HttpMethods.POST.ToString(), method) || string.Equals(HttpMethods.PUT.ToString(), method) || string.Equals(HttpMethods.PATCH.ToString(), method)) && parameters != null && parameters.Count() > 0)
                {
                    if (parameters != null && parameters.Count() > 0)
                    {
                        if ("application/json".Equals(contentType))
                        {
                            formattedParams = JsonConvert.SerializeObject(parameters);
                        }
                        else
                        {
                            formattedParams = string.Join("&", parameters.Select(x => x.Key + "=" + x.Value));
                        }
                    }
                }

                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
                request.Method = method;
                request.Accept = "application/json";

                FillHeaders(headers, accessTokenResult, request);
                await DoRequest(method, parameters, contentType, formattedParams, request).ConfigureAwait(false);

                return(await BuildResultFromResponse(request).ConfigureAwait(false));
            }
            catch (WebException ex)
            {
                return(ProcessErrorResponse(url, ex));
            }
        }