Skip to content

统一管理guzzleClient,支持自定义handler,用以支持协程环境 #58

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
"homepage": "https://github.com/upyun/php-sdk/",
"license": "MIT",
"require": {
"php": ">=5.5.0",
"php": ">=7.0",
"ext-curl": "*",
"guzzlehttp/guzzle": "~6.0"
"guzzlehttp/guzzle": "~6.0",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
Expand Down
6 changes: 2 additions & 4 deletions src/Upyun/Api/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ public function upload($path, $stream, $params)

$policy = Util::base64Json($params);
$method = 'POST';
$signature = Signature::getBodySignature($this->config, $method, '/' . $params['service'], null, $policy);
$client = new Client([
'timeout' => $this->config->timeout,
]);
$signature = Signature::getBodySignature($this->config, $method, '/'.$params['service'], null, $policy);
$client = $this->config->getGuzzleClient();

$response = $client->request($method, $this->endpoint, array(
'multipart' => array(
Expand Down
15 changes: 6 additions & 9 deletions src/Upyun/Api/Pretreat.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Upyun\Api;

use GuzzleHttp\Client;
Expand Down Expand Up @@ -26,9 +27,7 @@ public function process($tasks, $optionalParams = array())
{
$encodedTasks = Util::base64Json($tasks);

$client = new Client([
'timeout' => $this->config->timeout,
]);
$client = $this->config->getGuzzleClient();

$params = array(
'service' => $this->config->serviceName,
Expand All @@ -42,7 +41,7 @@ public function process($tasks, $optionalParams = array())
$method = 'POST';
$signedHeaders = Signature::getHeaderSign($this->config, $method, $path);

$url = $this->config->getPretreatEndPoint() . $path;
$url = $this->config->getPretreatEndPoint().$path;
$response = $client->request($method, $url, [
'headers' => $signedHeaders,
'form_params' => $params
Expand All @@ -55,18 +54,16 @@ public function process($tasks, $optionalParams = array())

public function query($taskIds, $path)
{
$client = new Client([
'timeout' => $this->config->timeout,
]);
$client = $this->config->getGuzzleClient();

$params = array(
'service' => $this->config->serviceName,
'task_ids' => implode(',', $taskIds)
);
$path = $path . '?' . http_build_query($params);
$path = $path.'?'.http_build_query($params);

$method = 'GET';
$url = $this->config->getPretreatEndPoint() . $path;
$url = $this->config->getPretreatEndPoint().$path;
$signedHeaders = Signature::getHeaderSign($this->config, $method, $path);
$response = $client->request($method, $url, [
'headers' => $signedHeaders
Expand Down
16 changes: 7 additions & 9 deletions src/Upyun/Api/Rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ class Rest

public function __construct(Config $config)
{
$this->config = $config;
$this->endpoint = $config->getProtocol() . Config::$restApiEndPoint . '/' . $config->serviceName;
$this->config = $config;
$this->endpoint = $config->getProtocol().Config::$restApiEndPoint.'/'.$config->serviceName;
}

public function request($method, $storagePath)
{
$this->method = strtoupper($method);
$this->storagePath = '/' . ltrim($storagePath, '/');
$this->storagePath = '/'.ltrim($storagePath, '/');
return $this;
}


/**
* @param string|resource $file
* @param string|resource $file
*
* @return $this
*/
Expand All @@ -58,11 +58,9 @@ public function withFile($file)
*/
public function send()
{
$client = new Client([
'timeout' => $this->config->timeout,
]);
$client = $this->config->getGuzzleClient();

$url = $this->endpoint . $this->storagePath;
$url = $this->endpoint.$this->storagePath;
$body = null;
if ($this->file && $this->method === 'PUT') {
$body = $this->file;
Expand Down Expand Up @@ -108,7 +106,7 @@ public function withHeaders($headers)

public function toRequest()
{
$url = $this->endpoint . $this->storagePath;
$url = $this->endpoint.$this->storagePath;
$body = null;
if ($this->file && $this->method === 'PUT') {
$body = $this->file;
Expand Down
16 changes: 8 additions & 8 deletions src/Upyun/Api/SyncVideo.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
use Upyun\Signature;


class SyncVideo {
class SyncVideo
{
/**
* @var Config
*/
Expand All @@ -21,16 +22,15 @@ public function __construct(Config $config)
$this->config = $config;
}

public function process($params, $path) {
$client = new Client([
'timeout' => $this->config->timeout,
]);
public function process($params, $path)
{
$client = $this->config->getGuzzleClient();

$path = '/' . $this->config->serviceName . $path;
$path = '/'.$this->config->serviceName.$path;
$method = 'POST';
$signedHeaders = Signature::getHeaderSign($this->config, $method, $path);

$url = $this->config->getSyncVideoEndPoint() . $path;
$url = $this->config->getSyncVideoEndPoint().$path;
$response = $client->request($method, $url, [
'headers' => $signedHeaders,
'json' => $params
Expand All @@ -39,4 +39,4 @@ public function process($params, $path) {
$body = $response->getBody()->getContents();
return json_decode($body, true);
}
}
}
60 changes: 49 additions & 11 deletions src/Upyun/Config.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?php

namespace Upyun;

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;

/**
* Class Config
*
Expand Down Expand Up @@ -65,7 +69,9 @@ class Config

private $version = '3.0.0';

protected $guzzleHandler;

protected $guzzleClient;

/**
* @var string 表单 api 的秘钥
Expand All @@ -81,44 +87,76 @@ class Config
/**
* rest api 和 form api 接口请求地址,详见:http://docs.upyun.com/api/rest_api/
*/
const ED_AUTO = 'v0.api.upyun.com';
const ED_TELECOM = 'v1.api.upyun.com';
const ED_CNC = 'v2.api.upyun.com';
const ED_CTT = 'v3.api.upyun.com';
const ED_AUTO = 'v0.api.upyun.com';
const ED_TELECOM = 'v1.api.upyun.com';
const ED_CNC = 'v2.api.upyun.com';
const ED_CTT = 'v3.api.upyun.com';

/**
* 异步云处理接口地址
*/
const ED_VIDEO = 'p0.api.upyun.com';
const ED_VIDEO = 'p0.api.upyun.com';

/**
* 刷新接口地址
*/
const ED_PURGE = 'http://purge.upyun.com/purge/';
const ED_PURGE = 'http://purge.upyun.com/purge/';

/**
* 同步视频处理接口地址
*/
const ED_SYNC_VIDEO = 'p1.api.upyun.com';
const ED_SYNC_VIDEO = 'p1.api.upyun.com';

public function __construct($serviceName, $operatorName, $operatorPassword)
{
$this->serviceName = $serviceName;
$this->bucketName = $serviceName;
$this->operatorName = $operatorName;
$this->setOperatorPassword($operatorPassword);
$this->useSsl = false;
$this->useSsl = false;
self::$restApiEndPoint = self::ED_AUTO;
}

/**
* @param $guzzleHandler
* @return $this
*/
public function setGuzzleHandler($guzzleHandler)
{
$this->guzzleHandler = $guzzleHandler;

return $this;
}

/**
* @return HandlerStack|null
*/
public function getGuzzleHandler()
{
if (isset($this->guzzleHandler)) {
$guzzleHandler = $this->guzzleHandler;
return is_string($guzzleHandler) ? new $guzzleHandler() : $guzzleHandler;
}

return $this->guzzleHandler;
}

public function getGuzzleClient()
{
return $this->guzzleClient ?? $this->guzzleClient = new Client([
'timeout' => $this->timeout,
'handler' => $this->getGuzzleHandler(),
]);
}

public function setOperatorPassword($operatorPassword)
{
$this->operatorPassword = md5($operatorPassword);
}

public function getFormApiKey()
{
if (! $this->formApiKey) {
if (!$this->formApiKey) {
throw new \Exception('form api key is empty.');
}

Expand All @@ -137,12 +175,12 @@ public function getVersion()

public function getPretreatEndPoint()
{
return $this->getProtocol() . self::ED_VIDEO;
return $this->getProtocol().self::ED_VIDEO;
}

public function getSyncVideoEndPoint()
{
return $this->getProtocol() . self::ED_SYNC_VIDEO;
return $this->getProtocol().self::ED_SYNC_VIDEO;
}

public function getProtocol()
Expand Down
27 changes: 13 additions & 14 deletions src/Upyun/Uploader.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Upyun;

use Upyun\Api\Rest;
Expand Down Expand Up @@ -33,12 +34,12 @@ public function upload($path, $file, $params, $withAsyncProcess)
return $req->upload($path, $stream, $params);
}

if (! $useBlock) {
if (!$useBlock) {
$req = new Rest($this->config);
return $req->request('PUT', $path)
->withHeaders($params)
->withFile($stream)
->send();
->withHeaders($params)
->withFile($stream)
->send();
} elseif ($this->config->uploadType === 'BLOCK_PARALLEL') {
return $this->concurrentPointUpload($path, $stream, $params);
} else {
Expand All @@ -61,7 +62,7 @@ private function pointUpload($path, $stream, $params)
$headers = array();
if (is_array($params)) {
foreach ($params as $key => $val) {
$headers['X-Upyun-Meta-' . $key] = $val;
$headers['X-Upyun-Meta-'.$key] = $val;
}
}
$res = $req->request('PUT', $path)
Expand All @@ -75,10 +76,10 @@ private function pointUpload($path, $stream, $params)
throw new \Exception('init request failed when poinit upload!');
}

$init = Util::getHeaderParams($res->getHeaders());
$uuid = $init['x-upyun-multi-uuid'];
$init = Util::getHeaderParams($res->getHeaders());
$uuid = $init['x-upyun-multi-uuid'];
$blockSize = 1024 * 1024;
$partId = 0;
$partId = 0;
do {
$fileBlock = $stream->read($blockSize);
$res = $req->request('PUT', $path)
Expand All @@ -93,7 +94,7 @@ private function pointUpload($path, $stream, $params)
if ($res->getStatusCode() !== 204) {
throw new \Exception('upload request failed when poinit upload!');
}
$data = Util::getHeaderParams($res->getHeaders());
$data = Util::getHeaderParams($res->getHeaders());
$partId = $data['x-upyun-next-part-id'];
} while ($partId != -1);

Expand All @@ -116,7 +117,7 @@ private function needUseBlock($fileSize)
$this->config->uploadType === 'BLOCK_PARALLEL') {
return true;
} elseif ($this->config->uploadType === 'AUTO' &&
$fileSize >= $this->config->sizeBoundary) {
$fileSize >= $this->config->sizeBoundary) {
return true;
} else {
return false;
Expand All @@ -139,7 +140,7 @@ private function concurrentPointUpload($path, $stream, $params)
$headers = array();
if (is_array($params)) {
foreach ($params as $key => $val) {
$headers['X-Upyun-Meta-' . $key] = $val;
$headers['X-Upyun-Meta-'.$key] = $val;
}
}
$res = $req->request('PUT', $path)
Expand Down Expand Up @@ -171,9 +172,7 @@ private function concurrentPointUpload($path, $stream, $params)
->toRequest();
}
};
$client = new Client([
'timeout' => $this->config->timeout,
]);
$client = $this->config->getGuzzleClient();
$pool = new Pool($client, $requests($req, $path, $stream, $uuid), [
'concurrency' => $this->config->concurrency,
'fulfilled' => function ($res) {
Expand Down
Loading