Automattic\WooCommerce\Internal\CLI\Migrator\Platforms\Shopify

ShopifyClient::process_responseprivateWC 1.0

Process the API response.

Method of the class: ShopifyClient{}

No Hooks.

Returns

Object|\WP_Error. Decoded response or WP_Error.

Usage

// private - for code of main (parent) class only
$result = $this->process_response( $response, $path );
$response(array|WP_Error) (required)
The HTTP response.
$path(string) (required)
The API path for error reporting.

ShopifyClient::process_response() code WC 10.7.0

private function process_response( $response, string $path ) {
	if ( is_wp_error( $response ) ) {
		return new \WP_Error( 'api_error', 'REST request failed: ' . $response->get_error_message() );
	}

	$response_code = wp_remote_retrieve_response_code( $response );
	$response_body = wp_remote_retrieve_body( $response );

	if ( $response_code >= 300 ) {
		$error_details = json_decode( $response_body );
		$error_message = isset( $error_details->errors ) ? wp_json_encode( $error_details->errors ) : $response_body;
		return new \WP_Error(
			'api_error',
			"REST request to {$path} failed with status code {$response_code}: " . $error_message
		);
	}

	$data = json_decode( $response_body );

	if ( json_last_error() !== JSON_ERROR_NONE ) {
		return new \WP_Error( 'api_error', 'Failed to decode REST JSON response: ' . json_last_error_msg() );
	}

	return $data;
}