Exemplo n.º 1
0
        /// <summary>
        /// Invokes as j token.
        /// </summary>
        /// <param name="realm">The realm.</param>
        /// <param name="version">The version.</param>
        /// <param name="httpMethod">The HTTP method.</param>
        /// <param name="resourceName">Name of the resource.</param>
        /// <param name="resourceAction">The resource action.</param>
        /// <param name="key">The key.</param>
        /// <param name="queryString">The query string.</param>
        /// <param name="bodyJson">The body json.</param>
        /// <param name="timeout">The timeout.</param>
        /// <param name="methodNameForTrace">The method name for trace.</param>
        /// <returns>
        /// JToken.
        /// </returns>
        protected JToken InvokeAsJToken(string realm, string version, string httpMethod, string resourceName, string resourceAction, string key = null, Dictionary <string, string> queryString = null, string bodyJson = null, int?timeout = null, [CallerMemberName] string methodNameForTrace = null)
        {
            BaseException exception = null;

            try
            {
                ApiTraceContext.Enter("RestApiClient", methodNameForTrace);

                var httpRequestRaw = CreateHttpRequestRaw(realm, version, httpMethod, resourceName, resourceAction, key, queryString);
                if (httpMethod.IsInString(StringComparison.OrdinalIgnoreCase, HttpConstants.HttpMethod.Post, HttpConstants.HttpMethod.Put))
                {
                    httpRequestRaw.Body = Encoding.UTF8.GetBytes(bodyJson.SafeToString());
                }

                ApiTraceContext.WriteHttpRequestRaw(httpRequestRaw);
                var httpRequest = httpRequestRaw.ToHttpWebRequest();
                if (timeout.HasValue)
                {
                    httpRequest.Timeout = timeout.Value;
                }

                var response = httpRequest.ReadResponseAsObject <JToken>();

                if (response.HttpStatusCode == HttpStatusCode.NoContent)
                {
                    return(null);
                }

                ApiTraceContext.WriteHttpResponseRaw(response);
                return(response.Body);
            }
            catch (HttpOperationException httpEx)
            {
                ApiTraceContext.WriteHttpResponseRaw(new HttpActionResult <string> {
                    Body = httpEx.ExceptionReference.ResponseText
                });

                ExceptionInfo externalExceptionInfo = JsonExtension.TryConvertJsonToObject <ExceptionInfo>(httpEx.ExceptionReference.ResponseText);

                exception = httpEx.Handle(new { httpMethod, resourceName, resourceAction, key, queryString, externalExceptionInfo });
                //Reset key to new one so that in log system, this exception can be identified correctly.
                exception.ResetNewKey();
                throw exception;
            }
            catch (Exception ex)
            {
                exception = ex.Handle(new { httpMethod, resourceName, resourceAction, key, queryString });
                throw exception;
            }
            finally
            {
                ApiTraceContext.Exit(exception?.Key);
            }
        }