Exemplo n.º 1
0
        public async Task Invoke(HttpContext context, IFhirDataBaseProvider dataBaseProvider)
        {
            if (!context.Request.Path.Value.Contains("/fhir/"))
            {
                await _next(context);

                return;
            }

            using (var requestBodyStream = new MemoryStream())
            {
                using (var responseBodyStream = new MemoryStream())
                {
                    var originalRequestBody = context.Request.Body;

                    var originalResponseBody = context.Response.Body;
                    var requestBodyText      = "";
                    var responseBodyText     = "";

                    await context.Request.Body.CopyToAsync(requestBodyStream);

                    requestBodyStream.Seek(0, SeekOrigin.Begin);

                    requestBodyText = Convert.ToBase64String(requestBodyStream.ToArray());

                    requestBodyStream.Seek(0, SeekOrigin.Begin);
                    context.Request.Body = requestBodyStream;


                    context.Response.Body = responseBodyStream;


                    await _next(context);


                    responseBodyStream.Seek(0, SeekOrigin.Begin);
                    responseBodyText = Convert.ToBase64String(responseBodyStream.ToArray());

                    responseBodyStream.Seek(0, SeekOrigin.Begin);

                    await responseBodyStream.CopyToAsync(originalResponseBody);



                    dataBaseProvider.WithConnection <object>("server_event.create_server_event", new NpgsqlParameter
                    {
                        NpgsqlDbType = NpgsqlDbType.Jsonb,
                        Value        = JsonConvert.SerializeObject(
                            BuildActionMetadata(context, responseBodyText, requestBodyText))
                    });

                    context.Request.Body  = originalRequestBody;
                    context.Response.Body = originalResponseBody;
                }
            }
        }
        private Resource ExecuteFunction(string name, params NpgsqlParameter[] parameters)
        {
            var resource = _dataBaseProvider.WithConnection(name, parameters);

            OperationOutcome operationOutcome = resource as OperationOutcome;

            if (operationOutcome != null)
            {
                throw new FhirHttpResponseException(operationOutcome);
            }

            return(resource);
        }