Exemple #1
0
        private static async Task SetSuccessResponseAsync(HttpResponse response, HttpResponseMessage httpResponse,
                                                          ExecutionData executionData)
        {
            const string responseDataKey = "response.data";
            var          content         = await httpResponse.Content.ReadAsStringAsync();

            var onSuccess = executionData.Route.OnSuccess;

            if (onSuccess == null)
            {
                await response.WriteAsync(content);

                return;
            }

            response.StatusCode = onSuccess.Code > 0 ? onSuccess.Code : 200;
            if (onSuccess.Data is string dataText && dataText.StartsWith(responseDataKey))
            {
                var dataKey = dataText.Replace(responseDataKey, string.Empty);
                if (string.IsNullOrWhiteSpace(dataKey))
                {
                    await response.WriteAsync(content);

                    return;
                }

                dataKey = dataKey.Substring(1, dataKey.Length - 1);
                dynamic data = new ExpandoObject();
                JsonConvert.PopulateObject(content, data);
                var dictionary = (IDictionary <string, object>)data;
                if (!dictionary.TryGetValue(dataKey, out var dataValue))
                {
                    return;
                }

                switch (dataValue)
                {
                case JObject jObject:
                    await response.WriteAsync(jObject.ToString());

                    return;

                case JArray jArray:
                    await response.WriteAsync(jArray.ToString());

                    return;

                default:
                    await response.WriteAsync(dataValue.ToString());

                    break;
                }
            }

            if (onSuccess.Data != null)
            {
                await response.WriteAsync(onSuccess.Data.ToString());
            }
        }
Exemple #2
0
        private static async Task WriteResponseAsync(HttpResponse response, HttpResponseMessage httpResponse,
                                                     ExecutionData executionData)
        {
            if (!httpResponse.IsSuccessStatusCode)
            {
                SetErrorResponse(response, httpResponse, executionData);
                return;
            }

            await SetSuccessResponseAsync(response, httpResponse, executionData);
        }
Exemple #3
0
        private static void SetErrorResponse(HttpResponse response, HttpResponseMessage httpResponse,
                                             ExecutionData executionData)
        {
            var onError = executionData.Route.OnError;

            if (onError == null)
            {
                response.StatusCode = (int)httpResponse.StatusCode;

                return;
            }

            response.StatusCode = onError.Code > 0 ? onError.Code : 400;
        }
Exemple #4
0
        private async Task <bool> IsPayloadValidAsync(ExecutionData executionData, HttpResponse httpResponse)
        {
            if (executionData.IsPayloadValid)
            {
                return(true);
            }

            var response = new { errors = executionData.ValidationErrors };
            var payload  = JsonConvert.SerializeObject(response);

            httpResponse.ContentType = "application/json";
            await httpResponse.WriteAsync(payload);

            return(false);
        }