Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
716572d
Add base HTTPClient
MiguelAxcar Aug 19, 2025
f8ac479
Refactor GoogleAI APIRequest to extend HTTPClient, unify auth and fil…
MiguelAxcar Aug 19, 2025
d3aab22
Simplify OpenAI APIRequest via HTTPClient, keep audio parsing for raw…
MiguelAxcar Aug 19, 2025
b38d0d2
Guard null scheduler in embeddings check to prevent fatal error
MiguelAxcar Aug 19, 2025
dd7ebc6
Migrate xAI APIRequest to HTTPClient with custom timeout handling
MiguelAxcar Aug 19, 2025
9c8cfe0
Refactor Watson APIRequest to use HTTPClient with basic auth support
MiguelAxcar Aug 19, 2025
6a1f1cf
Update Watson Classifier to import new APIRequest
MiguelAxcar Aug 19, 2025
4bd76a5
Wire Watson NLU to new APIRequest for consistent requests
MiguelAxcar Aug 19, 2025
d485b65
Switch Watson PostClassifier to new APIRequest for classification
MiguelAxcar Aug 19, 2025
a12dbe9
Point Ollama provider to Localhost APIRequest instead of OpenAI
MiguelAxcar Aug 19, 2025
0c9a252
Use Localhost APIRequest in OllamaEmbeddings to decouple from OpenAI
MiguelAxcar Aug 19, 2025
546b4c9
Use Localhost APIRequest for OllamaMultimodal provider
MiguelAxcar Aug 19, 2025
6760b4c
Migrate StableDiffusion to Localhost APIRequest
MiguelAxcar Aug 19, 2025
93f13d1
Update TogetherAI Images to use its own APIRequest
MiguelAxcar Aug 20, 2025
32810ae
Switch TogetherAI trait to its own APIRequest
MiguelAxcar Aug 20, 2025
fcc5c47
Add LocalHost Provider APIRequest
MiguelAxcar Aug 20, 2025
de170cd
Add TogetherAI Provider APIRequest
MiguelAxcar Aug 20, 2025
5d360db
Merge remote-tracking branch 'upstream/develop' into refactor/abstrac…
MiguelAxcar Aug 25, 2025
66e6c95
Adjust after merge
MiguelAxcar Aug 25, 2025
3023fc9
Remove unused code added after merge
MiguelAxcar Aug 25, 2025
7a3edea
Remove unused code added after merge
MiguelAxcar Aug 25, 2025
b12ba0b
Remove unused code added after merge
MiguelAxcar Aug 25, 2025
6821225
Skimming non-specific constructors
MiguelAxcar Aug 25, 2025
4e33acc
Linter fixes
MiguelAxcar Aug 25, 2025
136e401
Make add_headers method public to be used by unit tests
MiguelAxcar Aug 25, 2025
c98be9b
Create fresh APIRequest instances in 2 failing unit tests
MiguelAxcar Aug 25, 2025
b6644d7
Remove non-specific override
MiguelAxcar Aug 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 11 additions & 195 deletions includes/Classifai/Providers/GoogleAI/APIRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Classifai\Providers\GoogleAI;

use WP_Error;
use function Classifai\safe_wp_remote_get;
use function Classifai\safe_wp_remote_post;
use Classifai\Providers\HTTPClient;

/**
* The APIRequest class is a low level class to make Google AI API
Expand All @@ -18,214 +16,32 @@
* $request = new Classifai\Providers\GoogleAI\APIRequest();
* $request->post( $googleai_url, $options );
*/
class APIRequest {
class APIRequest extends HTTPClient {

/**
* The Google AI API key.
* Get the filter prefix for this provider.
*
* @var string
*/
public $api_key;

/**
* The feature name.
*
* @var string
*/
public $feature;

/**
* Google AI APIRequest constructor.
*
* @param string $api_key Google AI API key.
* @param string $feature Feature name.
*/
public function __construct( string $api_key = '', string $feature = '' ) {
$this->api_key = $api_key;
$this->feature = $feature;
}

/**
* Makes an authorized GET request.
*
* @param string $url The Google AI API url
* @param array $options Additional query params
* @return array|WP_Error
*/
public function get( string $url, array $options = [] ) {
/**
* Filter the URL for the get request.
*
* @since 3.0.0
* @hook classifai_googleai_api_request_get_url
*
* @param string $url The URL for the request.
* @param array $options The options for the request.
* @param string $this->feature The feature name.
*
* @return string The URL for the request.
*/
$url = apply_filters( 'classifai_googleai_api_request_get_url', $url, $options, $this->feature );

/**
* Filter the options for the get request.
*
* @since 3.0.0
* @hook classifai_googleai_api_request_get_options
*
* @param array $options The options for the request.
* @param string $url The URL for the request.
* @param string $this->feature The feature name.
*
* @return array The options for the request.
*/
$options = apply_filters( 'classifai_googleai_api_request_get_options', $options, $url, $this->feature );

$this->add_headers( $options );

/**
* Filter the response from Google AI for a get request.
*
* @since 3.0.0
* @hook classifai_googleai_api_response_get
*
* @param array|\WP_Error $response API response.
* @param string $url Request URL.
* @param array $options Request body options.
* @param string $this->feature Feature name.
*
* @return array API response.
*/
return apply_filters(
'classifai_googleai_api_response_get',
$this->get_result( safe_wp_remote_get( $url, $options ) ),
$url,
$options,
$this->feature
);
}

/**
* Makes an authorized POST request.
*
* @param string $url The Google AI API URL.
* @param array $options Additional query params.
* @return array|WP_Error
*/
public function post( string $url = '', array $options = [] ) {
$options = wp_parse_args(
$options,
[
'timeout' => 90, // phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout
]
);

/**
* Filter the URL for the post request.
*
* @since 3.0.0
* @hook classifai_googleai_api_request_post_url
*
* @param string $url The URL for the request.
* @param array $options The options for the request.
* @param string $this->feature The feature name.
*
* @return string The URL for the request.
*/
$url = apply_filters( 'classifai_googleai_api_request_post_url', $url, $options, $this->feature );

/**
* Filter the options for the post request.
*
* @since 3.0.0
* @hook classifai_googleai_api_request_post_options
*
* @param array $options The options for the request.
* @param string $url The URL for the request.
* @param string $this->feature The feature name.
*
* @return array The options for the request.
*/
$options = apply_filters( 'classifai_googleai_api_request_post_options', $options, $url, $this->feature );

$this->add_headers( $options );

/**
* Filter the response from Google AI for a post request.
*
* @since 3.0.0
* @hook classifai_googleai_api_response_post
*
* @param array|\WP_Error $response API response.
* @param string $url Request URL.
* @param array $options Request body options.
* @param string $this->feature Feature name.
*
* @return array API response.
*/
return apply_filters(
'classifai_googleai_api_response_post',
$this->get_result( safe_wp_remote_post( $url, $options ) ),
$url,
$options,
$this->feature
);
}

/**
* Get results from the response.
*
* @param object $response The API response.
* @return array|WP_Error
* @return string
*/
public function get_result( $response ) {
if ( ! is_wp_error( $response ) ) {
$body = wp_remote_retrieve_body( $response );
$code = wp_remote_retrieve_response_code( $response );
$json = json_decode( $body, true );

if ( json_last_error() === JSON_ERROR_NONE ) {
if ( empty( $json['error'] ) ) {
return $json;
} else {
$message = $json['error']['message'] ?? esc_html__( 'An error occurred', 'classifai' );
return new WP_Error( $code, $message );
}
} elseif ( ! empty( wp_remote_retrieve_response_message( $response ) ) ) {
return new WP_Error( $code, wp_remote_retrieve_response_message( $response ) );
} else {
return new WP_Error( 'Invalid JSON: ' . json_last_error_msg(), $body );
}
} else {
return $response;
}
protected function get_filter_prefix(): string {
return 'classifai_googleai';
}

/**
* Add the headers.
* Add authentication header.
*
* @param array $options The header options, passed by reference.
*/
public function add_headers( array &$options = [] ) {
if ( empty( $options['headers'] ) ) {
$options['headers'] = [];
}

if ( ! isset( $options['headers']['x-goog-api-key'] ) ) {
$options['headers']['x-goog-api-key'] = $this->get_auth_header();
}

if ( ! isset( $options['headers']['Content-Type'] ) ) {
$options['headers']['Content-Type'] = 'application/json';
}
protected function add_auth_header( array &$options ) {
$options['headers']['x-goog-api-key'] = $this->get_auth_header();
}

/**
* Get the auth header.
*
* @return string
*/
public function get_auth_header() {
public function get_auth_header(): string {
return $this->get_api_key();
}

Expand All @@ -234,7 +50,7 @@ public function get_auth_header() {
*
* @return string
*/
public function get_api_key() {
public function get_api_key(): string {
return $this->api_key;
}
}
Loading