fix for fileUpload & fileDownload
This commit is contained in:
parent
5371a9750e
commit
9bbf277d92
2
.gitignore
vendored
2
.gitignore
vendored
@ -2,6 +2,8 @@
|
|||||||
/bin
|
/bin
|
||||||
composer.lock
|
composer.lock
|
||||||
composer.phar
|
composer.phar
|
||||||
|
coverage.xml
|
||||||
|
test-report.xml
|
||||||
phpunit.xml
|
phpunit.xml
|
||||||
.idea
|
.idea
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
@ -29,8 +29,13 @@ use RetailCrm\Http\Client;
|
|||||||
*/
|
*/
|
||||||
abstract class AbstractLoader
|
abstract class AbstractLoader
|
||||||
{
|
{
|
||||||
|
/** @var string|null */
|
||||||
protected $siteCode;
|
protected $siteCode;
|
||||||
|
|
||||||
|
/** @var \RetailCrm\Http\Client */
|
||||||
protected $client;
|
protected $client;
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
protected $crmUrl;
|
protected $crmUrl;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -75,7 +75,7 @@ class Client
|
|||||||
*
|
*
|
||||||
* @return ApiResponse
|
* @return ApiResponse
|
||||||
*/
|
*/
|
||||||
public function makeRequest(
|
public function makeRawRequest(
|
||||||
$path,
|
$path,
|
||||||
$method,
|
$method,
|
||||||
array $parameters = [],
|
array $parameters = [],
|
||||||
@ -119,7 +119,7 @@ class Client
|
|||||||
curl_setopt($curlHandler, CURLOPT_POST, true);
|
curl_setopt($curlHandler, CURLOPT_POST, true);
|
||||||
|
|
||||||
if ('/files/upload' == $path) {
|
if ('/files/upload' == $path) {
|
||||||
curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $parameters['file']);
|
curl_setopt($curlHandler, CURLOPT_POSTFIELDS, file_get_contents($parameters['file']));
|
||||||
} else {
|
} else {
|
||||||
curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $parameters);
|
curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $parameters);
|
||||||
}
|
}
|
||||||
@ -143,4 +143,29 @@ class Client
|
|||||||
|
|
||||||
return new ApiResponse($statusCode, $responseBody);
|
return new ApiResponse($statusCode, $responseBody);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make HTTP request and deserialize JSON body (throws exception otherwise)
|
||||||
|
*
|
||||||
|
* @param string $path request url
|
||||||
|
* @param string $method (default: 'GET')
|
||||||
|
* @param array $parameters (default: array())
|
||||||
|
* @param bool $fullPath (default: false)
|
||||||
|
*
|
||||||
|
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
|
||||||
|
*
|
||||||
|
* @throws \InvalidArgumentException
|
||||||
|
* @throws CurlException
|
||||||
|
* @throws InvalidJsonException
|
||||||
|
*
|
||||||
|
* @return ApiResponse
|
||||||
|
*/
|
||||||
|
public function makeRequest(
|
||||||
|
$path,
|
||||||
|
$method,
|
||||||
|
array $parameters = [],
|
||||||
|
$fullPath = false
|
||||||
|
) {
|
||||||
|
return $this->makeRawRequest($path, $method, $parameters, $fullPath)->asJsonResponse();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -150,7 +150,7 @@ trait Files
|
|||||||
public function fileDownload($id)
|
public function fileDownload($id)
|
||||||
{
|
{
|
||||||
/* @noinspection PhpUndefinedMethodInspection */
|
/* @noinspection PhpUndefinedMethodInspection */
|
||||||
return $this->client->makeRequest(
|
return $this->client->makeRawRequest(
|
||||||
sprintf('/files/%s/download', $id),
|
sprintf('/files/%s/download', $id),
|
||||||
"GET"
|
"GET"
|
||||||
);
|
);
|
||||||
|
@ -51,9 +51,17 @@ class ApiResponse implements \ArrayAccess
|
|||||||
{
|
{
|
||||||
$this->statusCode = (int) $statusCode;
|
$this->statusCode = (int) $statusCode;
|
||||||
$this->rawResponse = $responseBody;
|
$this->rawResponse = $responseBody;
|
||||||
|
}
|
||||||
|
|
||||||
if (!empty($responseBody)) {
|
/**
|
||||||
$response = json_decode($responseBody, true);
|
* Deserialize JSON from raw response body
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function asJsonResponse()
|
||||||
|
{
|
||||||
|
if (!empty($this->rawResponse)) {
|
||||||
|
$response = json_decode($this->rawResponse, true);
|
||||||
|
|
||||||
if (!$response && JSON_ERROR_NONE !== ($error = json_last_error())) {
|
if (!$response && JSON_ERROR_NONE !== ($error = json_last_error())) {
|
||||||
throw new InvalidJsonException(
|
throw new InvalidJsonException(
|
||||||
@ -64,6 +72,8 @@ class ApiResponse implements \ArrayAccess
|
|||||||
|
|
||||||
$this->response = $response;
|
$this->response = $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -93,6 +93,22 @@ class ClientTest extends TestCase
|
|||||||
$response = $client->makeRequest('/orders', Client::METHOD_GET);
|
$response = $client->makeRequest('/orders', Client::METHOD_GET);
|
||||||
|
|
||||||
static::assertInstanceOf('RetailCrm\Response\ApiResponse', $response);
|
static::assertInstanceOf('RetailCrm\Response\ApiResponse', $response);
|
||||||
|
static::assertNotEmpty($response->getResponseBody());
|
||||||
|
static::assertNotEmpty($response->getResponse());
|
||||||
|
static::assertEquals(200, $response->getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group client
|
||||||
|
*/
|
||||||
|
public function testRequestSuccessNoDeserialization()
|
||||||
|
{
|
||||||
|
$client = static::getClient();
|
||||||
|
$response = $client->makeRawRequest('/orders', Client::METHOD_GET);
|
||||||
|
|
||||||
|
static::assertInstanceOf('RetailCrm\Response\ApiResponse', $response);
|
||||||
|
static::assertNotEmpty($response->getResponseBody());
|
||||||
|
static::assertEmpty($response->getResponse());
|
||||||
static::assertEquals(200, $response->getStatusCode());
|
static::assertEquals(200, $response->getStatusCode());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user