-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
71 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,53 +9,35 @@ | |
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
/** | ||
* Http.php. | ||
* | ||
* This file is part of the wechat-components. | ||
* | ||
* (c) overtrue <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace EasyWeChat\Applications\Base\Core; | ||
namespace EasyWeChat\Support; | ||
|
||
use EasyWeChat\Exceptions\HttpException; | ||
use EasyWeChat\Support\Log; | ||
use GuzzleHttp\Client as HttpClient; | ||
use GuzzleHttp\Client; | ||
use GuzzleHttp\HandlerStack; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
/** | ||
* Class Http. | ||
* Trait HasHttpRequests. | ||
* | ||
* @author overtrue <[email protected]> | ||
*/ | ||
class Http | ||
trait HasHttpRequests | ||
{ | ||
/** | ||
* Used to identify handler defined by client code | ||
* Maybe useful in the future. | ||
* @var \GuzzleHttp\Client | ||
*/ | ||
const USER_DEFINED_HANDLER = 'userDefined'; | ||
protected $httpClient; | ||
|
||
/** | ||
* Http client. | ||
* | ||
* @var HttpClient | ||
* @var array | ||
*/ | ||
protected $client; | ||
protected $middlewares = []; | ||
|
||
/** | ||
* The middlewares. | ||
* | ||
* @var array | ||
* @var \GuzzleHttp\HandlerStack | ||
*/ | ||
protected $middlewares = []; | ||
protected $handlerStack; | ||
|
||
/** | ||
* Guzzle client default settings. | ||
* | ||
* @var array | ||
*/ | ||
protected static $defaults = [ | ||
|
@@ -116,37 +98,36 @@ public function post($url, $options = []) | |
return $this->request($url, 'POST', [$key => $options]); | ||
} | ||
|
||
/** | ||
* JSON request. | ||
* | ||
* @param string $url | ||
* @param string|array $options | ||
* @param array $queries | ||
* @param int $encodeOption | ||
* | ||
* @return ResponseInterface | ||
* | ||
* @throws HttpException | ||
*/ | ||
public function json($url, $options = [], $encodeOption = JSON_UNESCAPED_UNICODE, $queries = []) | ||
{ | ||
is_array($options) && $options = json_encode($options, $encodeOption); | ||
|
||
return $this->request($url, 'POST', ['query' => $queries, 'body' => $options, 'headers' => ['content-type' => 'application/json']]); | ||
} | ||
/** | ||
* JSON request. | ||
* | ||
* @param string $url | ||
* @param string|array $options | ||
* @param array $query | ||
* @param int $encodeOption | ||
* | ||
* @return ResponseInterface | ||
* | ||
* @throws HttpException | ||
*/ | ||
public function postJson($url, $options = [], $encodeOption = JSON_UNESCAPED_UNICODE, $query = []) | ||
{ | ||
is_array($options) && $options = json_encode($options, $encodeOption); | ||
|
||
return $this->request($url, 'POST', ['query' => $query, 'body' => $options, 'headers' => ['content-type' => 'application/json']]); | ||
} | ||
|
||
/** | ||
* Upload file. | ||
* | ||
* @param string $url | ||
* @param array $files | ||
* @param array $form | ||
* @param array $query | ||
* | ||
* @return ResponseInterface | ||
* | ||
* @throws HttpException | ||
* @return \Psr\Http\Message\ResponseInterface | ||
*/ | ||
public function upload($url, array $files = [], array $form = [], array $queries = []) | ||
public function upload($url, array $files = [], array $form = [], array $query = []) | ||
{ | ||
$multipart = []; | ||
|
||
|
@@ -161,19 +142,19 @@ public function upload($url, array $files = [], array $form = [], array $queries | |
$multipart[] = compact('name', 'contents'); | ||
} | ||
|
||
return $this->request($url, 'POST', ['query' => $queries, 'multipart' => $multipart]); | ||
return $this->request($url, 'POST', ['query' => $query, 'multipart' => $multipart]); | ||
} | ||
|
||
/** | ||
* Set GuzzleHttp\Client. | ||
* | ||
* @param \GuzzleHttp\Client $client | ||
* @param \GuzzleHttp\Client $httpClient | ||
* | ||
* @return Http | ||
*/ | ||
public function setClient(HttpClient $client) | ||
public function setHttpClient(Client $httpClient) | ||
{ | ||
$this->client = $client; | ||
$this->httpClient = $httpClient; | ||
|
||
return $this; | ||
} | ||
|
@@ -183,13 +164,13 @@ public function setClient(HttpClient $client) | |
* | ||
* @return \GuzzleHttp\Client | ||
*/ | ||
public function getClient() | ||
public function getHttpClient() | ||
{ | ||
if (!($this->client instanceof HttpClient)) { | ||
$this->client = new HttpClient(); | ||
if (!($this->httpClient instanceof Client)) { | ||
$this->httpClient = new Client(); | ||
} | ||
|
||
return $this->client; | ||
return $this->httpClient; | ||
} | ||
|
||
/** | ||
|
@@ -211,7 +192,7 @@ public function addMiddleware(callable $middleware) | |
* | ||
* @return array | ||
*/ | ||
public function getMiddlewares() | ||
public function getMiddlewares(): array | ||
{ | ||
return $this->middlewares; | ||
} | ||
|
@@ -235,9 +216,9 @@ public function request($url, $method = 'GET', $options = []) | |
|
||
Log::debug('Client Request:', compact('url', 'method', 'options')); | ||
|
||
$options['handler'] = $this->getHandler(); | ||
$options['handler'] = $this->getHandlerStack(); | ||
|
||
$response = $this->getClient()->request($method, $url, $options); | ||
$response = $this->getHttpClient()->request($method, $url, $options); | ||
|
||
Log::debug('API response:', [ | ||
'Status' => $response->getStatusCode(), | ||
|
@@ -262,7 +243,6 @@ public function parseJSON($body) | |
$body = $body->getBody(); | ||
} | ||
|
||
// XXX: json maybe contains special chars. So, let's FUCK the WeChat API developers ... | ||
$body = $this->fuckTheWeChatInvalidJSON($body); | ||
|
||
if (empty($body)) { | ||
|
@@ -281,34 +261,46 @@ public function parseJSON($body) | |
} | ||
|
||
/** | ||
* Filter the invalid JSON string. | ||
* | ||
* @param \Psr\Http\Message\StreamInterface|string $invalidJSON | ||
* @param \GuzzleHttp\HandlerStack $handlerStack | ||
* | ||
* @return string | ||
* @return $this | ||
*/ | ||
protected function fuckTheWeChatInvalidJSON($invalidJSON) | ||
public function setHandlerStack(HandlerStack $handlerStack) | ||
{ | ||
return preg_replace('/[\x00-\x1F\x80-\x9F]/u', '', trim($invalidJSON)); | ||
$this->handlerStack = $handlerStack; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Build a handler. | ||
* Build a handler stack. | ||
* | ||
* @return HandlerStack | ||
* @return \GuzzleHttp\HandlerStack | ||
*/ | ||
protected function getHandler() | ||
public function getHandlerStack() | ||
{ | ||
$stack = HandlerStack::create(); | ||
if ($this->handlerStack) { | ||
return $this->handlerStack; | ||
} | ||
|
||
$this->handlerStack = HandlerStack::create(); | ||
|
||
foreach ($this->middlewares as $middleware) { | ||
$stack->push($middleware); | ||
$this->handlerStack->push($middleware); | ||
} | ||
|
||
if (isset(static::$defaults['handler']) && is_callable(static::$defaults['handler'])) { | ||
$stack->push(static::$defaults['handler'], self::USER_DEFINED_HANDLER); | ||
} | ||
return $this->handlerStack; | ||
} | ||
|
||
return $stack; | ||
/** | ||
* Filter the invalid JSON string. | ||
* | ||
* @param \Psr\Http\Message\StreamInterface|string $invalidJSON | ||
* | ||
* @return string | ||
*/ | ||
protected function fuckTheWeChatInvalidJSON($invalidJSON) | ||
{ | ||
return preg_replace('/[\x00-\x1F\x80-\x9F]/u', '', trim($invalidJSON)); | ||
} | ||
} |