Skip to content
This repository was archived by the owner on May 30, 2025. It is now read-only.

HttpClient: Separate Client from Transports #19

Merged
merged 10 commits into from
May 29, 2025
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
6 changes: 3 additions & 3 deletions components/Blueprints/DataReference/DataReferenceResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
use WordPress\Git\GitFilesystem;
use WordPress\Git\GitRepository;
use WordPress\HttpClient\ByteStream\SeekableRequestReadStream;
use WordPress\HttpClient\Client\SocketClient;
use WordPress\HttpClient\Client;

use function WordPress\Filesystem\wp_join_unix_paths;
use function WordPress\Filesystem\wp_unix_sys_get_temp_dir;

class DataReferenceResolver {
/**
* @var SocketClient
* @var Client
*/
private $client;
/**
Expand Down Expand Up @@ -47,7 +47,7 @@ class DataReferenceResolver {
*/
private $tmpRoot;

public function __construct( SocketClient $client, ?string $tmpRoot = null ) {
public function __construct( Client $client, ?string $tmpRoot = null ) {
$this->client = $client;
$this->tmpRoot = $tmpRoot ?: wp_unix_sys_get_temp_dir();
}
Expand Down
6 changes: 3 additions & 3 deletions components/Blueprints/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
use WordPress\Filesystem\InMemoryFilesystem;
use WordPress\Filesystem\LocalFilesystem;
use WordPress\HttpClient\ByteStream\RequestReadStream;
use WordPress\HttpClient\Client\SocketClient;
use WordPress\HttpClient\Client;
use WordPress\Zip\ZipFilesystem;

use function WordPress\Encoding\utf8_is_valid_byte_stream;
Expand All @@ -65,7 +65,7 @@ class Runner {
private $configuration;
// TODO: Rename httpClient
/**
* @var SocketClient
* @var Client
*/
private $client;
/**
Expand Down Expand Up @@ -113,7 +113,7 @@ public function __construct( RunnerConfiguration $configuration ) {
$this->configuration = $configuration;
$this->validateConfiguration( $configuration );

$this->client = new SocketClient();
$this->client = new Client();
$this->mainTracker = new Tracker();

// Set up progress logging
Expand Down
8 changes: 4 additions & 4 deletions components/Blueprints/Runtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use WordPress\ByteStream\WriteStream\FileWriteStream;
use WordPress\Filesystem\Filesystem;
use WordPress\Filesystem\LocalFilesystem;
use WordPress\HttpClient\Client\SocketClient;
use WordPress\HttpClient\Client;

use function WordPress\Filesystem\pipe_stream;
use function WordPress\Filesystem\wp_join_unix_paths;
Expand Down Expand Up @@ -47,7 +47,7 @@ class Runtime {
*/
private $assets;
/**
* @var SocketClient
* @var Client
*/
private $client;
/**
Expand All @@ -67,7 +67,7 @@ public function __construct(
Filesystem $targetFs,
RunnerConfiguration $configuration,
DataReferenceResolver $assets,
SocketClient $client,
Client $client,
array $blueprint,
string $tempRoot,
DataReference $wpCliReference
Expand All @@ -81,7 +81,7 @@ public function __construct(
$this->wpCliReference = $wpCliReference;
}

public function getHttpClient(): SocketClient {
public function getHttpClient(): Client {
return $this->client;
}

Expand Down
4 changes: 2 additions & 2 deletions components/Blueprints/SiteResolver/NewSiteResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use WordPress\Blueprints\Progress\Tracker;
use WordPress\Blueprints\Runtime;
use WordPress\Blueprints\VersionStrings\VersionConstraint;
use WordPress\HttpClient\Client\SocketClient;
use WordPress\HttpClient\Client;
use WordPress\Zip\ZipFilesystem;

use function WordPress\Filesystem\copy_between_filesystems;
Expand Down Expand Up @@ -143,7 +143,7 @@ static public function resolve( Runtime $runtime, Tracker $progress, ?VersionCon
$progress->finish();
}

static private function resolveWordPressZipUrl( SocketClient $client, string $version_string ): string {
static private function resolveWordPressZipUrl( Client $client, string $version_string ): string {
if ( $version_string === 'latest' ) {
return 'https://wordpress.org/latest.zip';
}
Expand Down
4 changes: 2 additions & 2 deletions components/Blueprints/Steps/SetSiteLanguageStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Exception;
use WordPress\Blueprints\Progress\Tracker;
use WordPress\Blueprints\Runtime;
use WordPress\HttpClient\Client\SocketClient;
use WordPress\HttpClient\Client;
use WordPress\HttpClient\Request;
use WordPress\Zip\ZipFilesystem;

Expand Down Expand Up @@ -226,7 +226,7 @@ function(\$theme) {
*
* @return string|false
*/
private function getWordPressTranslationUrl( Runtime $runtime, string $wpVersion, string $language, SocketClient $client ) {
private function getWordPressTranslationUrl( Runtime $runtime, string $wpVersion, string $language, Client $client ) {
try {
$api_url = "https://api.wordpress.org/translations/core/1.0/?version={$wpVersion}";
$translations_data = $client->fetch( $api_url )->json();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
use WordPress\ByteStream\MemoryPipe;
use WordPress\ByteStream\ReadStream\ByteReadStream;
use WordPress\Filesystem\Filesystem;
use WordPress\HttpClient\Client\SocketClient;
use WordPress\HttpClient\Client;

class DataReferenceResolverTest extends TestCase {
/** @var SocketClient&MockObject */
/** @var Client&MockObject */
protected $client;
protected $resolver;
/** @var Filesystem&MockObject */
Expand All @@ -30,7 +30,7 @@ class DataReferenceResolverTest extends TestCase {

protected function setUp(): void {
// @TODO: Don't mock. Just test actual resolution.
$this->client = new SocketClient();
$this->client = new Client();
$this->resolver = new DataReferenceResolver( $this->client );
$this->executionContext = $this->createMock( Filesystem::class );
$this->tracker = $this->createMock( Tracker::class );
Expand Down
12 changes: 6 additions & 6 deletions components/DataLiberation/Importer/AttachmentDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Exception;
use WordPress\Filesystem\Filesystem;
use WordPress\HttpClient\Client\SocketClient;
use WordPress\HttpClient\Client;
use WordPress\HttpClient\Request;

use function WordPress\Filesystem\wp_join_unix_paths;
Expand All @@ -25,7 +25,7 @@ class AttachmentDownloader {
private $progress = array();

public function __construct( $output_root, $options = array() ) {
$this->client = new SocketClient();
$this->client = new Client();
$this->output_root = $output_root;
$this->source_from_filesystem = $options['source_from_filesystem'] ?? null;
}
Expand Down Expand Up @@ -181,7 +181,7 @@ public function poll() {
*/

switch ( $event ) {
case SocketClient::EVENT_GOT_HEADERS:
case Client::EVENT_GOT_HEADERS:
if ( ! $request->is_redirected() ) {
if ( file_exists( $this->output_paths[ $original_request_id ] . '.partial' ) ) {
unlink( $this->output_paths[ $original_request_id ] . '.partial' );
Expand All @@ -196,7 +196,7 @@ public function poll() {
}
}
break;
case SocketClient::EVENT_BODY_CHUNK_AVAILABLE:
case Client::EVENT_BODY_CHUNK_AVAILABLE:
$chunk = $this->client->get_response_body_chunk();
if ( ! fwrite( $this->fps[ $original_request_id ], $chunk ) ) {
// @TODO: Don't echo the error message. Attach it to the import session instead for the user to review later on.
Expand All @@ -205,10 +205,10 @@ public function poll() {
}
$this->progress[ $original_url ]['received'] += strlen( $chunk );
break;
case SocketClient::EVENT_FAILED:
case Client::EVENT_FAILED:
$this->on_failure( $original_url, $original_request_id, $request->error );
break;
case SocketClient::EVENT_FINISHED:
case Client::EVENT_FINISHED:
if ( ! $request->is_redirected() ) {
// Only process if this was the last request in the chain.
$is_success = (
Expand Down
6 changes: 3 additions & 3 deletions components/Git/GitRemote.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
use WordPress\Git\Model\TreeEntry;
use WordPress\Git\Protocol\GitProtocolEncoderPipe;
use WordPress\Git\Protocol\Parser\GitProtocolDecoder;
use WordPress\HttpClient\Client\SocketClient;
use WordPress\HttpClient\Client;
use WordPress\HttpClient\Request;


class GitRemote {
/**
* @var SocketClient
* @var Client
*/
private $http_client;
/**
Expand All @@ -36,7 +36,7 @@ class GitRemote {
public function __construct( GitRepository $repository, $remote_name, $options = array() ) {
$this->remote_name = $remote_name;
$this->repository = $repository;
$this->http_client = $options['http_client'] ?? new SocketClient(
$this->http_client = $options['http_client'] ?? new Client(
array(
'timeout_ms' => 300000,
)
Expand Down
28 changes: 14 additions & 14 deletions components/HttpClient/ByteStream/RequestReadStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use WordPress\ByteStream\ByteStreamException;
use WordPress\ByteStream\ReadStream\BaseByteReadStream;
use WordPress\HttpClient\Client\SocketClient;
use WordPress\HttpClient\Client;
use WordPress\HttpClient\HttpClientException;
use WordPress\HttpClient\Request;
use WordPress\HttpClient\Response;
Expand All @@ -15,7 +15,7 @@
class RequestReadStream extends BaseByteReadStream {

/**
* @var SocketClient
* @var Client
*/
private $client;
/**
Expand Down Expand Up @@ -43,7 +43,7 @@ public function __construct( $request, $options = array() ) {
if ( is_string( $request ) ) {
$request = new Request( $request );
}
$this->client = $options['client'] ?? new SocketClient();
$this->client = $options['client'] ?? new Client();
$this->request = $request;
if ( isset( $options['buffer_size'] ) ) {
$this->buffer_size = $options['buffer_size'];
Expand Down Expand Up @@ -87,18 +87,18 @@ protected function internal_pull( $max_bytes = 8096 ): string {
return $this->pull_until_event(
array(
'max_bytes' => $max_bytes,
'event' => SocketClient::EVENT_BODY_CHUNK_AVAILABLE,
'event' => Client::EVENT_BODY_CHUNK_AVAILABLE,
)
);
}

private function pull_until_event( $options = array() ) {
$stop_at_event = $options['event'] ?? SocketClient::EVENT_BODY_CHUNK_AVAILABLE;
$stop_at_event = $options['event'] ?? Client::EVENT_BODY_CHUNK_AVAILABLE;
$this->ensure_is_enqueued();

while ( $this->client->await_next_event(
array(
'requests' => array( $this->request ),
'requests' => array( $this->request->latest_redirect() ),
)
) ) {
$request = $this->client->get_request();
Expand All @@ -113,7 +113,7 @@ private function pull_until_event( $options = array() ) {
continue;
}
switch ( $this->client->get_event() ) {
case SocketClient::EVENT_GOT_HEADERS:
case Client::EVENT_GOT_HEADERS:
$this->response = $response;
$content_length = $response->get_header( 'Content-Length' );
if ( null !== $content_length ) {
Expand All @@ -126,12 +126,12 @@ private function pull_until_event( $options = array() ) {
*/
$this->remote_file_length = (int) $content_length;
}
if ( $stop_at_event === SocketClient::EVENT_GOT_HEADERS ) {
if ( $stop_at_event === Client::EVENT_GOT_HEADERS ) {
return true;
}
break;
case SocketClient::EVENT_BODY_CHUNK_AVAILABLE:
if ( $stop_at_event === SocketClient::EVENT_BODY_CHUNK_AVAILABLE ) {
case Client::EVENT_BODY_CHUNK_AVAILABLE:
if ( $stop_at_event === Client::EVENT_BODY_CHUNK_AVAILABLE ) {
$body_chunk = $this->client->get_response_body_chunk();

if ( $this->progress_tracker ) {
Expand All @@ -144,7 +144,7 @@ private function pull_until_event( $options = array() ) {
return $body_chunk;
}
break;
case SocketClient::EVENT_FINISHED:
case Client::EVENT_FINISHED:
/**
* If the server did not provide a Content-Length header,
* backfill the file length with the number of downloaded
Expand All @@ -155,7 +155,7 @@ private function pull_until_event( $options = array() ) {
}

return '';
case SocketClient::EVENT_FAILED:
case Client::EVENT_FAILED:
// TODO: Think through error handling. Errors are expected when working with
// the network. Should we auto retry? Make it easy for the caller to retry?
// Something else?
Expand All @@ -174,7 +174,7 @@ public function await_response() {
if ( ! $this->response ) {
$this->pull_until_event(
array(
'event' => SocketClient::EVENT_GOT_HEADERS,
'event' => Client::EVENT_GOT_HEADERS,
)
);
}
Expand All @@ -188,7 +188,7 @@ public function await_response() {
protected function internal_reached_end_of_data(): bool {
return (
Request::STATE_FINISHED === $this->request->latest_redirect()->state &&
! $this->client->has_pending_event( $this->request, SocketClient::EVENT_BODY_CHUNK_AVAILABLE ) &&
! $this->client->has_pending_event( $this->request, Client::EVENT_BODY_CHUNK_AVAILABLE ) &&
strlen( $this->buffer ) === $this->offset_in_current_buffer
);
}
Expand Down
Loading
Loading