From ca0c25208808729c68ff097d47ab0b7c5579040c Mon Sep 17 00:00:00 2001 From: Nyholm Date: Mon, 13 Aug 2018 20:42:23 +0200 Subject: [PATCH] Added support for PSR18 --- composer.json | 3 ++- src/Client.php | 14 ++++++++++++-- src/Exception/ConnectException.php | 3 ++- src/Exception/InvalidRequestException.php | 23 +++++++++++++++++++++++ src/Exception/RequestException.php | 2 +- 5 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 src/Exception/InvalidRequestException.php diff --git a/composer.json b/composer.json index 1f328e308..7f12fbafa 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ } ], "require": { - "php": ">=5.5", + "php": "^7.0", + "psr/http-client": "^0.1", "guzzlehttp/psr7": "^1.4", "guzzlehttp/promises": "^1.0" }, diff --git a/src/Client.php b/src/Client.php index 80417918d..17562e6b9 100644 --- a/src/Client.php +++ b/src/Client.php @@ -2,6 +2,7 @@ namespace GuzzleHttp; use GuzzleHttp\Cookie\CookieJar; +use GuzzleHttp\Exception\InvalidRequestException; use GuzzleHttp\Promise; use GuzzleHttp\Psr7; use Psr\Http\Message\UriInterface; @@ -106,6 +107,15 @@ public function send(RequestInterface $request, array $options = []) return $this->sendAsync($request, $options)->wait(); } + public function sendRequest(RequestInterface $request): ResponseInterface + { + $options[RequestOptions::SYNCHRONOUS] = true; + $options[RequestOptions::ALLOW_REDIRECTS] = false; + $options[RequestOptions::HTTP_ERRORS] = false; + + return $this->sendAsync($request, $options)->wait(); + } + public function requestAsync($method, $uri = '', array $options = []) { $options = $this->prepareDefaults($options); @@ -371,7 +381,7 @@ private function applyOptions(RequestInterface $request, array &$options) $value = http_build_query($value, null, '&', PHP_QUERY_RFC3986); } if (!is_string($value)) { - throw new \InvalidArgumentException('query must be a string or array'); + throw new InvalidRequestException($request, 'query must be a string or array'); } $modify['query'] = $value; unset($options['query']); @@ -381,7 +391,7 @@ private function applyOptions(RequestInterface $request, array &$options) if (isset($options['sink'])) { // TODO: Add more sink validation? if (is_bool($options['sink'])) { - throw new \InvalidArgumentException('sink must not be a boolean'); + throw new InvalidRequestException($request, 'sink must not be a boolean'); } } diff --git a/src/Exception/ConnectException.php b/src/Exception/ConnectException.php index d33b0cc19..1c3ca5e44 100644 --- a/src/Exception/ConnectException.php +++ b/src/Exception/ConnectException.php @@ -1,6 +1,7 @@ request = $request; + parent::__construct($message); + } + + + public function getRequest(): RequestInterface + { + return $this->request; + } +} diff --git a/src/Exception/RequestException.php b/src/Exception/RequestException.php index f38ca8646..5bbdcc312 100644 --- a/src/Exception/RequestException.php +++ b/src/Exception/RequestException.php @@ -175,7 +175,7 @@ private static function obfuscateUri($uri) * * @return RequestInterface */ - public function getRequest() + public function getRequest(): RequestInterface { return $this->request; }