diff --git a/modules/apigee_edge_debug/apigee_edge_debug.services.yml b/modules/apigee_edge_debug/apigee_edge_debug.services.yml index 51d6c631..0b054a5b 100644 --- a/modules/apigee_edge_debug/apigee_edge_debug.services.yml +++ b/modules/apigee_edge_debug/apigee_edge_debug.services.yml @@ -22,3 +22,9 @@ services: arguments: ['@config.factory', '@logger.channel.apigee_edge_debug', '@plugin.manager.apigee_edge_debug.debug_message_formatter'] tags: - { name: http_client_middleware } + + apigee_edge_debug.devel_client_profiler: + class: Drupal\apigee_edge_debug\HttpClientMiddleware\DevelKintApiClientProfiler + arguments: ['@config.factory', '@plugin.manager.apigee_edge_debug.debug_message_formatter', '@current_user', '@module_handler', '@messenger'] + tags: + - { name: http_client_middleware } diff --git a/modules/apigee_edge_debug/src/HttpClientMiddleware/DevelKintApiClientProfiler.php b/modules/apigee_edge_debug/src/HttpClientMiddleware/DevelKintApiClientProfiler.php new file mode 100644 index 00000000..446311a7 --- /dev/null +++ b/modules/apigee_edge_debug/src/HttpClientMiddleware/DevelKintApiClientProfiler.php @@ -0,0 +1,153 @@ +get('apigee_edge_debug.settings')->get('formatter'); + if ($formatter_plugin_id) { + $this->formatter = $debug_message_formatter_plugin->createInstance($formatter_plugin_id); + } + $this->currentUser = $currentUser; + $this->moduleHandler = $module_handler; + $this->messenger = $messenger; + } + + /** + * {@inheritdoc} + */ + public function __invoke() { + return function ($handler) { + return function (RequestInterface $request, array $options) use ($handler) { + // If devel kint module is enabled and the user has devel kint permission. + if ($this->moduleHandler->moduleExists('kint') && $this->currentUser->hasPermission('access kint')) { + // If the formatter has been initialized yet then do nothing. + if (!$this->formatter) { + return $handler($request, $options); + } + $formatter = $this->formatter; + $rest_call = []; + if (isset($options[RequestOptions::ON_STATS])) { + $next = $options[RequestOptions::ON_STATS]; + } + else { + $next = function (TransferStats $stats) {}; + } + $options[RequestOptions::ON_STATS] = function (TransferStats $stats) use ($request, $next, $formatter) { + $this->messenger->addStatus(t('

Edge Calls

')); + $level = LogLevel::DEBUG; + // Do not modify the original request object in the subsequent calls. + $request_clone = clone $request; + $rest_call['Request'] = $formatter->formatRequest($request_clone); + if ($stats->hasResponse()) { + // Do not modify the original response object in the subsequent calls. + $response_clone = clone $stats->getResponse(); + $rest_call['Response'] = $formatter->formatResponse($response_clone, $request_clone); + if ($stats->getResponse()->getStatusCode() >= 400) { + $level = LogLevel::WARNING; + } + } + else { + $level = LogLevel::ERROR; + $error = $stats->getHandlerErrorData(); + if (is_object($error)) { + if (method_exists($error, '__toString')) { + $error = (string) $error; + } + else { + $error = json_encode($error); + } + } + $rest_call['Error'] = $error; + } + $next($stats); + $rest_call['Time Elapsed'] = $formatter->formatStats($stats); + $rest_call['Severity'] = isset($level) ? $level : ''; + ksm($rest_call); + }; + } + return $handler($request, $options); + }; + }; + } + +}