Ejemplo n.º 1
0
        /// <summary>
        /// Executes this request async and parses the result.
        /// </summary>
        /// <returns>The async request.</returns>
        public async Task <KinveyAuthResponse> ExecuteAsync()
        {
            string json = null;
            HttpResponseMessage response = null;

            try
            {
                response = await ExecuteUnparsedAsync().ConfigureAwait(false);

                json = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                return(JsonConvert.DeserializeObject <KinveyAuthResponse>(json));
            }
            catch (JsonException ex)
            {
                KinveyException kinveyException = new KinveyException(
                    EnumErrorCategory.ERROR_DATASTORE_NETWORK,
                    EnumErrorCode.ERROR_JSON_PARSE,
                    HelperMethods.GetCustomParsingJsonErrorMessage(json, response?.RequestMessage.RequestUri.ToString(), typeof(KinveyAuthResponse).FullName),
                    null,
                    ex
                    )
                {
                    RequestID = HelperMethods.getRequestID(response)
                };
                throw kinveyException;
            }
            catch (KinveyException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new KinveyException(
                          EnumErrorCategory.ERROR_USER,
                          EnumErrorCode.ERROR_USER_LOGIN_ATTEMPT,
                          "Error deserializing response content.",
                          ex
                          );
            }
        }
        /// <summary>
        /// Executes this request asynchronously.
        /// </summary>
        /// <returns> The async task with the type of the response. </returns>
        public virtual async Task <T> ExecuteAsync()
        {
            var response = await ExecuteUnparsedAsync().ConfigureAwait(false);

            if (OverrideRedirect)
            {
                string newLoc = string.Empty;
                foreach (var header in response.Headers)
                {
                    if (header.Key.ToLower().Equals("location"))
                    {
                        newLoc = header.Value.FirstOrDefault();
                        break;
                    }
                }
                //string newlocation = response.Headers.FirstOrDefault(stringToCheck => stringToCheck.ToString().Equals("Location")).ToString();
                return(await onRedirectAsync(newLoc).ConfigureAwait(false));
            }
            // special case to handle void or empty responses
            if (response.Content == null)
            {
                return(default(T));
            }

            string path = response.RequestMessage.RequestUri.AbsolutePath;

            if (path != null &&
                path.Contains(Constants.STR_PATH_CUSTOM_ENDPOINT) &&
                (((int)response.StatusCode) < 200 || ((int)response.StatusCode) > 302))
            {
                // Seems like only Custom Endpoint/BL would result in having a successful response
                // without having a successful status code.  The BL executed successfully, but did
                // produce a successsful outcome.
                try
                {
                    response.EnsureSuccessStatusCode();
                }
                catch (Exception ex)
                {
                    var ke = new KinveyException(
                        EnumErrorCategory.ERROR_CUSTOM_ENDPOINT,
                        EnumErrorCode.ERROR_CUSTOM_ENDPOINT_ERROR,
                        response,
                        ex
                        );
                    throw ke;
                }
            }

            if (path != null &&
                path.Contains(Constants.STR_PATH_REALTIME_STREAM) &&
                (((int)response.StatusCode) < 200 || ((int)response.StatusCode) > 302))
            {
                // Appears as though there is a stream error.  A stream error could result in having a successful response
                // without having a successful status code, such as a 401.  The request was successful, but the response
                // indicates that there is an issue with what was being requested
                try
                {
                    response.EnsureSuccessStatusCode();
                }
                catch (Exception ex)
                {
                    var ke = new KinveyException(
                        EnumErrorCategory.ERROR_REALTIME,
                        EnumErrorCode.ERROR_REALTIME_ERROR,
                        response,
                        ex
                        );
                    throw ke;
                }
            }

            if (((int)response.StatusCode) < 200 || ((int)response.StatusCode) > 302)
            {
                try
                {
                    response.EnsureSuccessStatusCode();
                }
                catch (Exception ex)
                {
                    var kinveyException = new KinveyException(
                        EnumErrorCategory.ERROR_BACKEND,
                        EnumErrorCode.ERROR_JSON_RESPONSE,
                        response,
                        ex
                        )
                    {
                        RequestID = HelperMethods.getRequestID(response)
                    };
                    throw kinveyException;
                }
            }

            string json = null;

            try
            {
                json = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                var result = JsonConvert.DeserializeObject <T>(json);

                RequestStartTime = HelperMethods.GetRequestStartTime(response);

                return(result);
            }
            catch (JsonException ex)
            {
                KinveyException kinveyException = new KinveyException(
                    EnumErrorCategory.ERROR_DATASTORE_NETWORK,
                    EnumErrorCode.ERROR_JSON_PARSE,
                    HelperMethods.GetCustomParsingJsonErrorMessage(json, response.RequestMessage.RequestUri.ToString(), typeof(T).FullName),
                    null,
                    ex
                    )
                {
                    RequestID = HelperMethods.getRequestID(response)
                };
                throw kinveyException;
            }
            catch (ArgumentException ex)
            {
                Logger.Log(ex.Message);
                return(default(T));
            }
            catch (NullReferenceException ex)
            {
                Logger.Log(ex.Message);
                return(default(T));
            }
        }