Skip to content

Commit

Permalink
feat(http): added helper function to get a Http Guzzle Client
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalsem committed Nov 4, 2022
1 parent 535d0eb commit d2f6097
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 23 deletions.
23 changes: 2 additions & 21 deletions engine/classes/Elgg/Assets/ImageFetcherService.php
Expand Up @@ -5,7 +5,6 @@
use Elgg\Cache\SystemCache;
use Elgg\Config;
use Elgg\Exceptions\InvalidArgumentException;
use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Exception\TransferException;
use GuzzleHttp\RequestOptions;
Expand All @@ -25,7 +24,7 @@ class ImageFetcherService {
protected $cache;

/**
* @var \GuzzleHttp\Client
* @var \Elgg\Http\Client
*/
protected $client;

Expand All @@ -51,25 +50,7 @@ public function __construct(Config $config, SystemCache $cache, \ElggSession $se
$this->cache = $cache;
$this->session = $session;

$proxy_config = $this->config->proxy ?? [];

$options = [
RequestOptions::TIMEOUT => 5,
RequestOptions::HTTP_ERRORS => false,
RequestOptions::VERIFY => (bool) elgg_extract('verify_ssl', $proxy_config, true),
];

$host = elgg_extract('host', $proxy_config);
if (!empty($host)) {
$port = (int) elgg_extract('port', $proxy_config);
if ($port > 0) {
$host = rtrim($host, ':') . ":{$port}";
}

$options[RequestOptions::PROXY] = $host;
}

$this->client = new Client($options);
$this->client = elgg_get_http_client();
}

/**
Expand Down
42 changes: 42 additions & 0 deletions engine/classes/Elgg/Http/Client.php
@@ -0,0 +1,42 @@
<?php

namespace Elgg\Http;

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\RequestOptions;

/**
* Helper class to construct a Guzzle Client with the correct defaults
*
* @since 5.0
*/
class Client extends GuzzleClient {

/**
* {@inheritdoc}
*/
public function __construct(array $options = []) {

$proxy_config = (array) elgg_get_config('proxy', []);

$defaults = [
RequestOptions::TIMEOUT => 5,
RequestOptions::HTTP_ERRORS => false,
RequestOptions::VERIFY => (bool) elgg_extract('verify_ssl', $proxy_config, true),
];

$host = elgg_extract('host', $proxy_config);
if (!empty($host)) {
$port = (int) elgg_extract('port', $proxy_config);
if ($port > 0) {
$host = rtrim($host, ':') . ":{$port}";
}

$defaults[RequestOptions::PROXY] = $host;
}

$options = array_merge($defaults, $options);

parent::__construct($options);
}
}
11 changes: 11 additions & 0 deletions engine/lib/elgglib.php
Expand Up @@ -227,6 +227,17 @@ function elgg_http_validate_signed_url(string $url): bool {
return _elgg_services()->urlSigner->isValid($url);
}

/**
* Returns a Guzzle HTTP client
*
* @param array $options Options for the client
*
* @return \Elgg\Http\Client
*/
function elgg_get_http_client(array $options = []): \Elgg\Http\Client {
return new \Elgg\Http\Client($options);
}

/**
* Checks for $array[$key] and returns its value if it exists, else
* returns $default.
Expand Down
@@ -0,0 +1,26 @@
<?php

namespace Elgg\Http;

use Elgg\IntegrationTestCase;

class ClientIntegrationTest extends IntegrationTestCase {

public function testCanCreateHttpClient() {
$this->assertInstanceOf(\Elgg\Http\Client::class, elgg_get_http_client());
}

public function testCanPassOptions() {
$client = elgg_get_http_client(['foo' => 'bar']);

$reflector = new \ReflectionClass($client);
$reflector = $reflector->getParentClass();
$property = $reflector->getProperty('config');
$property->setAccessible(true);

$config = $property->getValue($client);

$this->assertArrayHasKey('foo', $config);
$this->assertEquals('bar', $config['foo']);
}
}
3 changes: 1 addition & 2 deletions mod/web_services/classes/Elgg/WebServices/ElggApiClient.php
Expand Up @@ -3,7 +3,6 @@
namespace Elgg\WebServices;

use Elgg\Traits\TimeUsing;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
Expand Down Expand Up @@ -183,7 +182,7 @@ public function executeRequest() {
$client_options['handler'] = $stack;

// create client
$client = new Client($client_options);
$client = elgg_get_http_client($client_options);

if ($this->method === 'POST') {
$result = $client->post($this->url, [
Expand Down

0 comments on commit d2f6097

Please sign in to comment.