Example #1
0
        private async Task <byte[]> GetSerializedResponseData(object data, HttpListenerResponse httpResponse)
        {
            if (data is null)
            {
                throw new System.Exception("Data can't be NULL");
            }
            else if (data is string message)
            {
                return(Encoding.UTF8.GetBytes(message));
            }
            else if (data is ViewModel viewModel)
            {
                var bodyTask = TempleteResolver?.Resolve(viewModel);
                return(Encoding.UTF8.GetBytes(await bodyTask.ConfigureAwait(false)));
            }
            else if (data is Response response)
            {
                foreach (var key in response.Header.Keys)
                {
                    httpResponse.AppendHeader(key, response.Header[key]);
                }
                httpResponse.StatusCode = response.StatusCode;
                return(await GetSerializedResponseData(response.Model, httpResponse).ConfigureAwait(false));
            }
            else if (data is Task task)
            {
                await task.ConfigureAwait(false);

                return(await GetSerializedResponseData(TaskUtil.GetResultFromTask(task), httpResponse).ConfigureAwait(false));
            }
            else
            {
                return(Serializer.Serialize(data));
            }
        }