/// <returns>Success</returns>
        /// <exception cref="SwaggerException">A server side error occurred.</exception>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        public async System.Threading.Tasks.Task ApiByVersionEndpointByEndpointEventPostAsync(string endpoint, string x_Congether_SIGN, EndpointMessageQueue eventQueue, System.Threading.CancellationToken cancellationToken)
        {
            if (endpoint == null)
            {
                throw new System.ArgumentNullException("endpoint");
            }

            var urlBuilder_ = new System.Text.StringBuilder();

            urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/api/1.0/Endpoint/{endpoint}/event");
            urlBuilder_.Replace("{endpoint}", System.Uri.EscapeDataString(ConvertToString(endpoint, System.Globalization.CultureInfo.InvariantCulture)));

            var client_ = new System.Net.Http.HttpClient();

            try
            {
                using (var request_ = new System.Net.Http.HttpRequestMessage())
                {
                    if (x_Congether_SIGN != null)
                    {
                        request_.Headers.TryAddWithoutValidation("X-Congether-SIGN", ConvertToString(x_Congether_SIGN, System.Globalization.CultureInfo.InvariantCulture));
                    }
                    var content_ = new System.Net.Http.StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(eventQueue, _settings.Value));
                    content_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json");
                    request_.Content             = content_;
                    request_.Method = new System.Net.Http.HttpMethod("POST");

                    //SIGN CONTENT
                    if (x_Congether_SIGN == null)
                    {
                        request_.Headers.TryAddWithoutValidation("X-Congether-SIGN", ConvertToString(GetHMACSHA256(await content_.ReadAsStreamAsync(), _apiSecret), System.Globalization.CultureInfo.InvariantCulture));
                    }
                    //END SIGN CONTENT

                    PrepareRequest(client_, request_, urlBuilder_);
                    var url_ = urlBuilder_.ToString();
                    request_.RequestUri = new System.Uri(url_, System.UriKind.RelativeOrAbsolute);
                    PrepareRequest(client_, request_, url_);

                    var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);

                    try
                    {
                        var headers_ = System.Linq.Enumerable.ToDictionary(response_.Headers, h_ => h_.Key, h_ => h_.Value);
                        if (response_.Content != null && response_.Content.Headers != null)
                        {
                            foreach (var item_ in response_.Content.Headers)
                            {
                                headers_[item_.Key] = item_.Value;
                            }
                        }

                        ProcessResponse(client_, response_);

                        var status_ = ((int)response_.StatusCode).ToString();
                        if (status_ == "200")
                        {
                            return;
                        }
                        else
                        if (status_ != "200" && status_ != "204")
                        {
                            var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);

                            throw new SwaggerException("The HTTP status code of the response was not expected (" + (int)response_.StatusCode + ").", (int)response_.StatusCode, responseData_, headers_, null);
                        }
                    }
                    finally
                    {
                        if (response_ != null)
                        {
                            response_.Dispose();
                        }
                    }
                }
            }
            finally
            {
                if (client_ != null)
                {
                    client_.Dispose();
                }
            }
        }
        public async Task Invoke(HttpContext context)
        {
            //var apiRoute = context.Request.Path.Value;
            //Enum.TryParse(context.Request.Method, out HttpMethod httpMethod);
            //Read the Header

            var request = context.Request;

            string authHeader  = context.Request.Headers["Authorization"];
            string pandaHeader = context.Request.Headers["Panda"];

            if (string.IsNullOrEmpty(pandaHeader))
            {
                context.Response.StatusCode = 500;
                await context.Response.WriteAsync("{ errors : ['Secure Key Not found'] }");

                return;
            }

            var SecureKey = _configuration["SecureKey:Self"];

            if (SecureKey != pandaHeader)
            {
                context.Response.StatusCode = 500;
                await context.Response.WriteAsync("{ errors : ['Secure Key Invalid'] }");

                return;
            }


            ClaimsPrincipal pcp = null;

            if (!string.IsNullOrEmpty(authHeader))
            {
                authHeader = authHeader.Replace("Bearer ", "");
                pcp        = _jwtTokenService.GetPrincipalFromExpiredToken(authHeader);
                if (pcp == null)
                {
                    context.Response.StatusCode = 500;
                    await context.Response.WriteAsync("{ errors : ['Token Invalid'] }");

                    return;
                }
            }

            #region BodyModified

            if (request.Path.Value.Contains("/api") && !request.Path.Value.ToLower().Contains("value"))
            {
                var stream          = request.Body;
                var originalContent = new StreamReader(stream).ReadToEnd();
                var notModified     = true;
                try
                {
                    dynamic dataSource = JsonConvert.DeserializeObject(originalContent);
                    if (dataSource != null)
                    {
                        dataSource.Token = authHeader;
                        var json           = JsonConvert.SerializeObject(dataSource);
                        var requestContent = new System.Net.Http.StringContent(json, Encoding.UTF8, "application/json");
                        stream = await requestContent.ReadAsStreamAsync();

                        notModified = false;
                    }
                }
                catch
                {
                    notModified = true;
                }
                if (notModified)
                {
                    var requestData = Encoding.UTF8.GetBytes(originalContent);
                    stream = new MemoryStream(requestData);
                }

                request.Body = stream;
            }
            #endregion


            await _next(context);
        }