コード例 #1
0
        public virtual async Task <T> ExecuteAsync()
        {
            var response = await ExecuteUnparsedAsync();

            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));
            }
            // 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;
                }
            }

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

                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,
                    ex.Message,
                    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));
            }
        }