Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected function createRequest(HttpMethodEnum $method, string $path, array $he
{
return new Request(
$method,
AnthropicProvider::BASE_URI . '/' . ltrim($path, '/'),
AnthropicProvider::url($path),
$headers,
$data
);
Expand Down
14 changes: 11 additions & 3 deletions src/ProviderImplementations/Anthropic/AnthropicProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace WordPress\AiClient\ProviderImplementations\Anthropic;

use WordPress\AiClient\Common\Exception\RuntimeException;
use WordPress\AiClient\Providers\AbstractProvider;
use WordPress\AiClient\Providers\ApiBasedImplementation\AbstractApiProvider;
use WordPress\AiClient\Providers\ApiBasedImplementation\ListModelsApiBasedProviderAvailability;
use WordPress\AiClient\Providers\Contracts\ModelMetadataDirectoryInterface;
use WordPress\AiClient\Providers\Contracts\ProviderAvailabilityInterface;
Expand All @@ -19,9 +19,17 @@
*
* @since 0.1.0
*/
class AnthropicProvider extends AbstractProvider
class AnthropicProvider extends AbstractApiProvider
{
public const BASE_URI = 'https://api.anthropic.com/v1';
/**
* {@inheritDoc}
*
* @since n.e.x.t
*/
protected static function baseUrl(): string
{
return 'https://api.anthropic.com/v1';
}

/**
* {@inheritDoc}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected function createRequest(HttpMethodEnum $method, string $path, array $he
{
return new Request(
$method,
AnthropicProvider::BASE_URI . '/' . ltrim($path, '/'),
AnthropicProvider::url($path),
$headers,
$data
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected function createRequest(HttpMethodEnum $method, string $path, array $he
{
return new Request(
$method,
GoogleProvider::BASE_URI . '/openai/' . ltrim($path, '/'),
GoogleProvider::url('openai' . ltrim($path, '/')),
$headers,
$data
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected function createRequest(HttpMethodEnum $method, string $path, array $he
}
return new Request(
$method,
GoogleProvider::BASE_URI . '/' . ltrim($path, '/'),
GoogleProvider::url($path),
$headers,
$data
);
Expand Down
14 changes: 11 additions & 3 deletions src/ProviderImplementations/Google/GoogleProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace WordPress\AiClient\ProviderImplementations\Google;

use WordPress\AiClient\Common\Exception\RuntimeException;
use WordPress\AiClient\Providers\AbstractProvider;
use WordPress\AiClient\Providers\ApiBasedImplementation\AbstractApiProvider;
use WordPress\AiClient\Providers\ApiBasedImplementation\ListModelsApiBasedProviderAvailability;
use WordPress\AiClient\Providers\Contracts\ModelMetadataDirectoryInterface;
use WordPress\AiClient\Providers\Contracts\ProviderAvailabilityInterface;
Expand All @@ -19,9 +19,17 @@
*
* @since 0.1.0
*/
class GoogleProvider extends AbstractProvider
class GoogleProvider extends AbstractApiProvider
{
public const BASE_URI = 'https://generativelanguage.googleapis.com/v1beta';
/**
* {@inheritDoc}
*
* @since n.e.x.t
*/
protected static function baseUrl(): string
{
return 'https://generativelanguage.googleapis.com/v1beta';
}

/**
* {@inheritDoc}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected function createRequest(HttpMethodEnum $method, string $path, array $he
{
return new Request(
$method,
GoogleProvider::BASE_URI . '/openai/' . ltrim($path, '/'),
GoogleProvider::url('openai' . ltrim($path, '/')),
$headers,
$data
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected function createRequest(HttpMethodEnum $method, string $path, array $he
{
return new Request(
$method,
OpenAiProvider::BASE_URI . '/' . ltrim($path, '/'),
OpenAiProvider::url($path),
$headers,
$data
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected function createRequest(HttpMethodEnum $method, string $path, array $he
{
return new Request(
$method,
OpenAiProvider::BASE_URI . '/' . ltrim($path, '/'),
OpenAiProvider::url($path),
$headers,
$data
);
Expand Down
14 changes: 11 additions & 3 deletions src/ProviderImplementations/OpenAi/OpenAiProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace WordPress\AiClient\ProviderImplementations\OpenAi;

use WordPress\AiClient\Common\Exception\RuntimeException;
use WordPress\AiClient\Providers\AbstractProvider;
use WordPress\AiClient\Providers\ApiBasedImplementation\AbstractApiProvider;
use WordPress\AiClient\Providers\ApiBasedImplementation\ListModelsApiBasedProviderAvailability;
use WordPress\AiClient\Providers\Contracts\ModelMetadataDirectoryInterface;
use WordPress\AiClient\Providers\Contracts\ProviderAvailabilityInterface;
Expand All @@ -19,9 +19,17 @@
*
* @since 0.1.0
*/
class OpenAiProvider extends AbstractProvider
class OpenAiProvider extends AbstractApiProvider
{
public const BASE_URI = 'https://api.openai.com/v1';
/**
* {@inheritDoc}
*
* @since n.e.x.t
*/
protected static function baseUrl(): string
{
return 'https://api.openai.com/v1';
}

/**
* {@inheritDoc}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected function createRequest(HttpMethodEnum $method, string $path, array $he
{
return new Request(
$method,
OpenAiProvider::BASE_URI . '/' . ltrim($path, '/'),
OpenAiProvider::url($path),
$headers,
$data
);
Expand Down
53 changes: 53 additions & 0 deletions src/Providers/ApiBasedImplementation/AbstractApiProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace WordPress\AiClient\Providers\ApiBasedImplementation;

use WordPress\AiClient\Providers\AbstractProvider;

/**
* Base class for API-based providers.
*
* This abstract class provides URL construction utilities for providers that
* communicate with REST APIs. It standardizes the pattern of combining a base
* URL with endpoint paths.
*
* @since n.e.x.t
*/
abstract class AbstractApiProvider extends AbstractProvider
{
/**
* Gets the base URL for the provider's API.
*
* The base URL should include the protocol and domain, and may include
* the API version path (e.g., "https://api.example.com/v1").
*
* @since n.e.x.t
*
* @return string The base URL for the provider's API.
*/
abstract protected static function baseUrl(): string;

/**
* Constructs a full URL by combining the base URL with an optional path.
*
* This method ensures proper URL construction by:
* - Using the provider's base URL
* - Trimming leading slashes from the path to prevent double-slashes
* - Joining the base URL and path with a single forward slash
*
* @since n.e.x.t
*
* @param string $path Optional path to append to the base URL. Default empty string.
* @return string The complete URL.
*/
public static function url(string $path = ''): string
{
if ($path === '') {
return static::baseUrl();
}

return static::baseUrl() . '/' . ltrim($path, '/');
}
}
Loading