Ejemplo n.º 1
0
        public void Add(IList <CompositeSubRequest> subRequests, IList <CompositeSubRequestResult> responses)
        {
            if (subRequests == null)
            {
                throw new ArgumentNullException(nameof(subRequests));
            }
            if (responses == null)
            {
                throw new ArgumentNullException(nameof(responses));
            }

            for (var i = 0; i < subRequests.Count; i++)
            {
                var subRequest = subRequests[i];
                _Requests.Add(subRequest);

                if (i >= responses.Count)
                {
                    break;
                }

                var response = responses[i] ?? new CompositeSubRequestResult();

                switch (subRequest.ResponseType)
                {
                case "collections":
                    try
                    {
                        if (response.Body?.Type == JTokenType.Array)
                        {
                            foreach (var(row, j) in response.Body.Select((row, j) => (row, j)))
                            {
                                var refId = $"{subRequest.ReferenceId}_{j}";

                                if (row?.Type == JTokenType.Object)
                                {
                                    if (QueryResult <JToken> .IsAssignableFrom(row))
                                    {
                                        _Queries.Add(refId, (JObject)row);
                                    }
                                    else if (SuccessResponse.IsAssignableFrom(row))
                                    {
                                        _Results.Add(refId, row);
                                    }
                                    else if (row["attributes"]?.Type == JTokenType.Object)
                                    {
                                        _Results.Add(refId, row);
                                    }
                                    else if ((bool?)row["success"] == true)
                                    {
                                        _Results.Add(refId, row);
                                    }
                                    else if (row["errors"]?.Type == JTokenType.Array)
                                    {
                                        if (row["errors"]?.Any() == true)
                                        {
                                            _Errors.Add(refId, ErrorResponse.TryCast(row["errors"]));
                                        }
                                    }
                                    else
                                    {
                                        _Errors.Add(refId, ErrorResponse.TryCast(row));
                                    }
                                }
                                else if (row?["errors"]?.Type == JTokenType.Array)
                                {
                                    if (row["errors"]?.Any() == true)
                                    {
                                        _Errors.Add(refId, ErrorResponse.TryCast(row["errors"]));
                                    }
                                }
                                else
                                {
                                    _Errors.Add(refId, ErrorResponse.TryCast(row));
                                }
                            }
                        }
                        else
                        {
                            _Errors.Add(subRequest.ReferenceId ?? string.Empty, ErrorResponse.TryCast(response.Body));
                        }
                    }
                    catch
                    {
                        _Errors.Add(subRequest.ReferenceId ?? string.Empty, ErrorResponse.TryCast(response.Body));
                    }
                    break;

                case "query":
                    try
                    {
                        if (IsSuccessStatusCode(response.HttpStatusCode))
                        {
                            if (QueryResult <JToken> .IsAssignableFrom(response.Body))
                            {
                                _Queries.Add(subRequest.ReferenceId ?? string.Empty, response.Body as JObject ?? new JObject());
                            }
                            else if (response.Body?.Type == JTokenType.Array)
                            {
                                _Results.Add(subRequest.ReferenceId ?? string.Empty, response.Body);
                            }
                            else
                            {
                                _Results.Add(subRequest.ReferenceId ?? string.Empty, new JArray {
                                    response.Body ?? new JObject()
                                });
                            }
                        }
                        else
                        {
                            _Errors.Add(subRequest.ReferenceId ?? string.Empty, ErrorResponse.TryCast(response.Body));
                        }
                    }
                    catch
                    {
                        _Errors.Add(subRequest.ReferenceId ?? string.Empty, ErrorResponse.TryCast(response.Body));
                    }
                    break;

                default:
                    try
                    {
                        if (IsSuccessStatusCode(response.HttpStatusCode))
                        {
                            _Results.Add(subRequest.ReferenceId ?? string.Empty, response.Body ?? new JObject());
                        }
                        else
                        {
                            _Errors.Add(subRequest.ReferenceId ?? string.Empty, ErrorResponse.TryCast(response.Body));
                        }
                    }
                    catch
                    {
                        _Errors.Add(subRequest.ReferenceId ?? string.Empty, ErrorResponse.TryCast(response.Body));
                    }
                    break;
                }
            }

            bool IsSuccessStatusCode(int statusCode)
            {
                return(statusCode >= 200 && statusCode <= 299);
            }
        }
Ejemplo n.º 2
0
 public IDictionary <string, SuccessResponse> SuccessResponses() =>
 _Results
 .Where(r => SuccessResponse.IsAssignableFrom(r.Value))
 .ToDictionary(
     r => r.Key,
     r => r.Value.ToObject <SuccessResponse>() ?? new SuccessResponse());