Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Guzzle in favor of WordPress core functions #446

Merged
merged 3 commits into from
Mar 7, 2022
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.DS_Store
vendor/
xdebug/*
.phpunit.result.cache
2 changes: 1 addition & 1 deletion Dockerfile.wordpress
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM wpdiaries/wordpress-xdebug:5.7-php7.4-apache
RUN apt-get update && apt-get install -y git-core vim
RUN apt-get --allow-releaseinfo-change update && apt-get install -y git-core vim
RUN curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar \
&& chmod +x wp-cli.phar \
&& mv wp-cli.phar /usr/local/bin/wp
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
"require": {
"cloudflare/cf-ip-rewrite": "^1.0.0",
"symfony/polyfill-intl-idn": "*",
"psr/log": "^1.0",
"guzzlehttp/guzzle": "~5.0"
"psr/log": "^1.0"
},
"require-dev": {
"symfony/yaml": "~2.6",
Expand Down
206 changes: 2 additions & 204 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/API/AbstractAPIClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function callAPI(Request $request)
$response = $this->getPaginatedResults($request, $response);

return $response;
} catch (RequestException $e) {
} catch (\Exception $e) {
$errorMessage = $this->getErrorMessage($e);

$this->logAPICall($this->getAPIClientName(), array(
Expand Down
1 change: 0 additions & 1 deletion src/API/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace CF\API;

use Guzzle\Http\Exception\BadResponseException;
use CF\Integration\IntegrationInterface;
use CF\WordPress\Utils;

Expand Down
50 changes: 28 additions & 22 deletions src/API/DefaultHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,73 @@
namespace CF\API;

use CF\API\Request;
use GuzzleHttp;
use GuzzleHttp\Exception\RequestException;

class DefaultHttpClient implements HttpClientInterface
{
const CONTENT_TYPE_KEY = 'Content-Type';
const APPLICATION_JSON_KEY = 'application/json';

protected $client;
protected $endpoint;

/**
* @param String $endpoint
*/
public function __construct($endpoint)
{
$this->client = new GuzzleHttp\Client(['base_url' => $endpoint]);
$this->endpoint = $endpoint;
}

/**
* @param Request $request
* @throws RequestException
* @throws \Exception
* @return Array $response
*/
public function send(Request $request)
{
$apiRequest = $this->createGuzzleRequest($request);
$requestOptions = $this->createRequestOptions($request);
$url = $this->createRequestUrl($request);

$response = $this->client->send($apiRequest)->json();
$response = wp_remote_request($url, $requestOptions);

if (is_wp_error($response)) {
throw new \Exception('Request error', $response->get_error_code);
}

$response_body = json_decode($response['body']);

if (json_last_error() !== JSON_ERROR_NONE) {
throw new RequestException('Error decoding client API JSON', $response);
throw new \Exception('Error decoding client API JSON', json_last_error());
}

return $response;
return $response_body;
}

/**
* @param Request $request
* @return GuzzleHttp\Message\RequestInterface $request
* @return array $requestOptions
*/
public function createGuzzleRequest(Request $request)
public function createRequestOptions(Request $request)
{
$bodyType = 'body';
if (isset($request->getHeaders()[self::CONTENT_TYPE_KEY]) && $request->getHeaders()[self::CONTENT_TYPE_KEY] === self::APPLICATION_JSON_KEY) {
$bodyType = 'json';
}

$requestOptions = array(
'method' => $request->getMethod(),
'headers' => $request->getHeaders(),
'query' => $request->getParameters(),
$bodyType => $request->getBody(),
'body' => $request->getBody(),
);

return $this->client->createRequest($request->getMethod(), $request->getUrl(), $requestOptions);
return $requestOptions;
}

/**
* @param GuzzleHttpClient $client
* @param Request $request
* @return string $url
*/
public function setClient($client)
public function createRequestUrl(Request $request)
{
$this->client = $client;
$url = $this->endpoint . $request->getUrl();
foreach ($request->getParameters() as $key => $value) {
$url = add_query_arg($key, $value, $url);
}

return $url;
}
}
3 changes: 0 additions & 3 deletions src/Test/API/AbstractAPIClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace CF\API\Test;

use GuzzleHttp;
use GuzzleHttp\Message\RequestInterface;
use GuzzleHttp\Message\ResponseInterface;
use CF\Integration\DefaultIntegration;
use CF\Integration\DefaultLogger;
use CF\Integration\DataStoreInterface;
Expand Down
Loading