Action uncanny-automator

automator_api_response

Fires after an API response is generated, providing access to the response, request, and parameters.

add_action( 'automator_api_response', $callback, 10, 3 );

Description

Fires after an API request is made. Developers can access the API response, the request details, and original parameters to perform actions like logging, custom error handling, or modifying subsequent processing. The `$last_response`, `$request`, and `$params` are provided.


Usage

add_action( 'automator_api_response', 'your_function_name', 10, 3 );

Parameters

$last_response (mixed)
This parameter holds the response from the immediately preceding API call made by the Automator.
$request (mixed)
This parameter contains the response from the last API request made.
$params (mixed)
This parameter contains the details of the incoming API request, including the method and URL.

Examples

<?php
/**
 * Example of how to hook into the 'automator_api_response' action.
 * This example logs the API response status code and body if it's an error.
 *
 * @param WP_Error|array $last_response The response from wp_remote_request.
 * @param array          $request       The arguments used for wp_remote_request.
 * @param array          $params        Additional parameters passed to the API call, including the URL.
 */
function my_automator_api_response_handler( $last_response, $request, $params ) {
	// Check if the response is an error.
	if ( is_wp_error( $last_response ) ) {
		// Log the WP_Error object for debugging.
		error_log( 'Automator API Error: ' . $last_response->get_error_message() . ' for URL: ' . $params['url'] );
	} else {
		// Check the HTTP status code.
		$status_code = wp_remote_retrieve_response_code( $last_response );

		// If the status code indicates an error (e.g., 4xx or 5xx).
		if ( $status_code >= 400 ) {
			$response_body = wp_remote_retrieve_body( $last_response );
			// Log the error status code and response body.
			error_log(
				sprintf(
					'Automator API Error: Received status code %d for URL %s. Response body: %s',
					$status_code,
					$params['url'],
					$response_body // Be cautious with logging sensitive data from response bodies.
				)
			);
		}
	}
}

// Hook into the action with the correct number of arguments (3).
add_action( 'automator_api_response', 'my_automator_api_response_handler', 10, 3 );
?>

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/core/classes/class-api-server.php:461

public static function call( $params ) {

		$api = self::get_instance();

		if ( empty( $params['method'] ) ) {
			throw new Exception( 'Request method is required', 500 );
		}

		if ( empty( $params['url'] ) ) {
			throw new Exception( 'URL is required', 500 );
		}

		$request = array();

		$request = $api->maybe_add_optional_params( $request, $params );

		$request = apply_filters( 'automator_call', $request, $params );

		$time_before = microtime( true );

		self::$last_response = wp_remote_request(
			$params['url'],
			$request
		);

		self::$last_response = apply_filters( 'automator_api_last_response', self::$last_response, $request, $params );

		do_action( 'automator_api_response', self::$last_response, $request, $params );

		$time_spent = round( ( microtime( true ) - $time_before ) * 1000 );

		$params['time_spent'] = $time_spent;

		$api_log_id = $api->maybe_log_action( $params, $request, self::$last_response );

		if ( is_wp_error( self::$last_response ) ) {
			throw new Exception( esc_html( 'WordPress was not able to make a request: ' . self::$last_response->get_error_message() ), 500 );
		}

		self::$last_response['api_log_id'] = $api_log_id;

		return self::$last_response;
	}


Scroll to Top