-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit cbe846b
Showing
4 changed files
with
325 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea/ | ||
composer.lock | ||
vendor/* |
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 |
---|---|---|
@@ -0,0 +1,278 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the GLAVWEB.cms Rest Client package. | ||
* | ||
* (c) Andrey Nilov <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Glavweb\CmsRestClient; | ||
|
||
use GuzzleHttp\Client; | ||
|
||
/** | ||
* Class CmsRestClient | ||
* | ||
* @package Glavweb\CmsRestClient | ||
* @author Andrey Nilov <[email protected]> | ||
*/ | ||
class CmsRestClient | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private static $validateTokenResult = []; | ||
|
||
/** | ||
* @var Client | ||
*/ | ||
private $guzzle; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $apiBaseUrl; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $apiUserName; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $apiPassword; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private static $token = false; | ||
|
||
/** | ||
* ContentBlockService constructor. | ||
* | ||
* @param Client $guzzle | ||
* @param string $apiBaseUrl | ||
* @param string $apiUserName | ||
* @param string $apiPassword | ||
*/ | ||
public function __construct(Client $guzzle, $apiBaseUrl, $apiUserName, $apiPassword) | ||
{ | ||
$this->guzzle = $guzzle; | ||
$this->apiBaseUrl = $this->prepareApiBaseUrl($apiBaseUrl); | ||
$this->apiUserName = $apiUserName; | ||
$this->apiPassword = $apiPassword; | ||
} | ||
|
||
/** | ||
* @param string $method | ||
* @param string $uri | ||
* @param array $options | ||
* @param bool|false $auth | ||
* @return mixed|\Psr\Http\Message\ResponseInterface | ||
* @throws \Exception | ||
*/ | ||
public function request($method, $uri, array $options = [], $auth = false) | ||
{ | ||
if ($auth) { | ||
if (!isset($options['headers'])) { | ||
$options['headers'] = []; | ||
} | ||
|
||
$options['headers'] = array_merge($options['headers'], $this->singIn()); | ||
} | ||
|
||
$response = $this->guzzle->request($method, $this->url($uri), $options); | ||
|
||
return $response; | ||
} | ||
|
||
/** | ||
* @param string $uri | ||
* @param array $options | ||
* @param bool|true $auth | ||
* @return mixed|\Psr\Http\Message\ResponseInterface | ||
* @throws \Exception | ||
*/ | ||
public function get($uri, array $options = [], $auth = false) | ||
{ | ||
return $this->request('GET', $uri, $options, $auth); | ||
} | ||
|
||
/** | ||
* @param string $uri | ||
* @param array $options | ||
* @param bool|true $auth | ||
* @return mixed|\Psr\Http\Message\ResponseInterface | ||
* @throws \Exception | ||
*/ | ||
public function post($uri, array $options = [], $auth = false) | ||
{ | ||
return $this->request('POST', $uri, $options, $auth); | ||
} | ||
|
||
/** | ||
* @param string $uri | ||
* @param array $options | ||
* @param bool|true $auth | ||
* @return mixed|\Psr\Http\Message\ResponseInterface | ||
* @throws \Exception | ||
*/ | ||
public function put($uri, array $options = [], $auth = false) | ||
{ | ||
return $this->request('PUT', $uri, $options, $auth); | ||
} | ||
|
||
/** | ||
* @param string $uri | ||
* @param array $options | ||
* @param bool|true $auth | ||
* @return mixed|\Psr\Http\Message\ResponseInterface | ||
* @throws \Exception | ||
*/ | ||
public function patch($uri, array $options = [], $auth = false) | ||
{ | ||
return $this->request('PATCH', $uri, $options, $auth); | ||
} | ||
|
||
/** | ||
* @param string $uri | ||
* @param array $options | ||
* @param bool|true $auth | ||
* @return mixed|\Psr\Http\Message\ResponseInterface | ||
* @throws \Exception | ||
*/ | ||
public function delete($uri, array $options = [], $auth = false) | ||
{ | ||
return $this->request('DELETE', $uri, $options, $auth); | ||
} | ||
|
||
/** | ||
* @param string $uri | ||
* @param array $options | ||
* @param bool|true $auth | ||
* @return mixed|\Psr\Http\Message\ResponseInterface | ||
* @throws \Exception | ||
*/ | ||
public function link($uri, array $options = [], $auth = false) | ||
{ | ||
return $this->request('LINK', $uri, $options, $auth); | ||
} | ||
|
||
/** | ||
* @param string $uri | ||
* @param array $options | ||
* @param bool|true $auth | ||
* @return mixed|\Psr\Http\Message\ResponseInterface | ||
* @throws \Exception | ||
*/ | ||
public function unlink($uri, array $options = [], $auth = false) | ||
{ | ||
return $this->request('UNLINK', $uri, $options, $auth); | ||
} | ||
|
||
/** | ||
* @param string $token | ||
* @param bool $cash | ||
* @return bool | ||
*/ | ||
public function validateToken($token, $cash = true) | ||
{ | ||
if (!$cash) { | ||
return $this->doValidateToken($token); | ||
} | ||
|
||
if (!isset(self::$validateTokenResult[$token])) { | ||
self::$validateTokenResult[$token] = $this->doValidateToken($token); | ||
} | ||
|
||
return self::$validateTokenResult[$token]; | ||
} | ||
|
||
/** | ||
* @param string $token | ||
* @return bool | ||
*/ | ||
public function doValidateToken($token) | ||
{ | ||
$response = $this->guzzle->request('GET', $this->url('validate-token'), [ | ||
'headers' => [ | ||
'Token' => $token | ||
], | ||
]); | ||
|
||
return $response->getStatusCode() == 200; | ||
} | ||
|
||
/** | ||
* @param string $apiBaseUrl | ||
* @return string | ||
*/ | ||
private function prepareApiBaseUrl($apiBaseUrl) | ||
{ | ||
return rtrim($apiBaseUrl, '/') . '/'; | ||
} | ||
|
||
/** | ||
* @param string $uri | ||
* @return string | ||
*/ | ||
private function url($uri) | ||
{ | ||
$scheme = parse_url($uri, PHP_URL_SCHEME); | ||
|
||
if ($scheme !== null) { | ||
return $uri; | ||
} | ||
|
||
return $this->apiBaseUrl . $uri; | ||
} | ||
|
||
/** | ||
* @return array | ||
* @throws \Exception | ||
*/ | ||
private function singIn() | ||
{ | ||
$needSingId = | ||
self::$token === false || | ||
(self::$token && !$this->validateToken(self::$token)) | ||
; | ||
|
||
if ($needSingId) { | ||
self::$token = $this->singInRequest(); | ||
} | ||
|
||
if (!self::$token) { | ||
throw new \Exception('Sing in is failed.'); | ||
} | ||
|
||
return [ | ||
'Token' => self::$token | ||
]; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
private function singInRequest() | ||
{ | ||
$response = $this->guzzle->request('POST', $this->url('sign-in'), [ | ||
'form_params' => [ | ||
'username' => $this->apiUserName, | ||
'password' => $this->apiPassword, | ||
] | ||
]); | ||
|
||
$token = null; | ||
if ($response->getStatusCode() == 200) { | ||
$body = json_decode($response->getBody(), true); | ||
$token = isset($body['Token']) ? $body['Token'] : null; | ||
} | ||
|
||
return $token; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 GLAVWEB | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "glavweb/cms-rest-client", | ||
"description": "GLAVWEB.cms Rest Client", | ||
"authors": [ | ||
{ | ||
"name": "nilov", | ||
"email": "[email protected]", | ||
"homepage": "http://glavweb.ru" | ||
}, | ||
{ | ||
"name": "GLAVWEB Community", | ||
"homepage": "https://github.com/glavweb/cms-rest-client/contributors" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.5.9", | ||
"guzzlehttp/guzzle": "^6.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { "Glavweb\\CmsRestClient\\": "" } | ||
} | ||
} |