Skip to content
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

Add BatchGetTraversableTest and update CHANGELOG #394

Merged
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
6 changes: 2 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@

* fix [typehint for Bitrix24 User entity with field ID](https://github.com/mesilov/bitrix24-php-sdk/issues/382)
* fix [default arguments for Bitrix24 User get method](https://github.com/mesilov/bitrix24-php-sdk/issues/381)
* fix [limit argument not worked in batch list and read model](https://github.com/mesilov/bitrix24-php-sdk/issues/389)

## 2.0-beta.2 — 1.04.2024

Expand Down Expand Up @@ -214,10 +215,7 @@
* fix [typehint at ContactItemResult](https://github.com/mesilov/bitrix24-php-sdk/issues/320)
* fix [return types in DealCategoryItemResult](https://github.com/mesilov/bitrix24-php-sdk/issues/322)
* fix [add auth node in telephony voximplant events requests](https://github.com/mesilov/bitrix24-php-sdk/issues/331)
*

fix [add helper metods isError for registerCallResult fortelephony](https://github.com/mesilov/bitrix24-php-sdk/issues/335)

* fix [add helper metods isError for registerCallResult fortelephony](https://github.com/mesilov/bitrix24-php-sdk/issues/335)
* fix [add return type for crm multifields phone, email, im](https://github.com/mesilov/bitrix24-php-sdk/issues/338)
* fix errors in `ShowFieldsDescriptionCommand` metadata reader CLI command
* fix errors for `ApplicationProfile` with empty scope
Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ test-integration-scope-telephony:
test-integration-scope-workflows:
vendor/bin/phpunit --testsuite integration_tests_scope_workflows
test-integration-scope-user:
vendor/bin/phpunit --testsuite integration_tests_scope_user
vendor/bin/phpunit --testsuite integration_tests_scope_user
test-integration-core:
vendor/bin/phpunit --testsuite integration_tests_core
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<testsuite name="integration_tests">
<directory>./tests/Integration</directory>
</testsuite>
<testsuite name="integration_tests_core">
<directory>./tests/Integration/Core/</directory>
</testsuite>
<testsuite name="integration_tests_scope_telephony">
<directory>./tests/Integration/Services/Telephony/</directory>
</testsuite>
Expand Down
1 change: 1 addition & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

return RectorConfig::configure()
->withPaths([
__DIR__ . '/src/Core/',
__DIR__ . '/src/Services/Telephony',
__DIR__ . '/tests/Integration/Services/Telephony',
__DIR__ . '/src/Services/User',
Expand Down
46 changes: 17 additions & 29 deletions src/Core/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,31 @@

class ApiClient implements ApiClientInterface
{
protected HttpClientInterface $client;
protected LoggerInterface $logger;
protected Credentials $credentials;
protected RequestIdGeneratorInterface $requestIdGenerator;
/**
* @const string
*/
protected const BITRIX24_OAUTH_SERVER_URL = 'https://oauth.bitrix.info';

/**
* @const string
*/
protected const SDK_VERSION = '2.0.0';

protected const SDK_USER_AGENT = 'bitrix24-php-sdk';

/**
* ApiClient constructor.
*
* @param Credentials $credentials
* @param HttpClientInterface $client
* @param RequestIdGeneratorInterface $requestIdGenerator
* @param LoggerInterface $logger
*/
public function __construct(
Credentials $credentials,
HttpClientInterface $client,
RequestIdGeneratorInterface $requestIdGenerator,
LoggerInterface $logger)
protected Credentials $credentials,
protected HttpClientInterface $client,
protected RequestIdGeneratorInterface $requestIdGenerator,
protected LoggerInterface $logger)
{
$this->credentials = $credentials;
$this->client = $client;
$this->requestIdGenerator = $requestIdGenerator;
$this->logger = $logger;
$this->logger->debug(
'ApiClient.init',
[
'httpClientType' => get_class($client),
'httpClientType' => $this->client::class,
]
);
}
Expand All @@ -72,16 +61,12 @@ protected function getDefaultHeaders(): array
];
}

/**
* @return Credentials
*/
public function getCredentials(): Credentials
{
return $this->credentials;
}

/**
* @return RenewedAccessToken
* @throws InvalidArgumentException
* @throws TransportExceptionInterface
* @throws TransportException
Expand All @@ -92,10 +77,11 @@ public function getNewAccessToken(): RenewedAccessToken
$this->logger->debug('getNewAccessToken.start', [
'requestId' => $requestId
]);
if ($this->getCredentials()->getApplicationProfile() === null) {
if (!$this->getCredentials()->getApplicationProfile() instanceof \Bitrix24\SDK\Core\Credentials\ApplicationProfile) {
throw new InvalidArgumentException('application profile not set');
}
if ($this->getCredentials()->getAccessToken() === null) {

if (!$this->getCredentials()->getAccessToken() instanceof \Bitrix24\SDK\Core\Credentials\AccessToken) {
throw new InvalidArgumentException('access token in credentials not set');
}

Expand Down Expand Up @@ -132,20 +118,20 @@ public function getNewAccessToken(): RenewedAccessToken
]);
return $newAccessToken;
}

if ($response->getStatusCode() === StatusCodeInterface::STATUS_BAD_REQUEST) {
$this->logger->warning('getNewAccessToken.badRequest',[
'url'=> $url
]);
throw new TransportException(sprintf('getting new access token failure: %s', $responseData['error']));
}

throw new TransportException('getting new access token failure with unknown http-status code %s', $response->getStatusCode());
}

/**
* @param string $apiMethod
* @param array<mixed> $parameters
*
* @return ResponseInterface
* @throws TransportExceptionInterface
* @throws InvalidArgumentException
*/
Expand All @@ -163,16 +149,18 @@ public function getResponse(string $apiMethod, array $parameters = []): Response
);

$method = 'POST';
if ($this->getCredentials()->getWebhookUrl() !== null) {
if ($this->getCredentials()->getWebhookUrl() instanceof \Bitrix24\SDK\Core\Credentials\WebhookUrl) {
$url = sprintf('%s/%s/', $this->getCredentials()->getWebhookUrl()->getUrl(), $apiMethod);
} else {
$url = sprintf('%s/rest/%s', $this->getCredentials()->getDomainUrl(), $apiMethod);

if ($this->getCredentials()->getAccessToken() === null) {
throw new InvalidArgumentException(sprintf('access token in credentials not found '));
if (!$this->getCredentials()->getAccessToken() instanceof \Bitrix24\SDK\Core\Credentials\AccessToken) {
throw new InvalidArgumentException('access token in credentials not found ');
}

$parameters['auth'] = $this->getCredentials()->getAccessToken()->getAccessToken();
}

// duplicate request id in query string for current version of bitrix24 api
// vendor don't use request id from headers =(
$url .= '?' . $this->requestIdGenerator->getQueryStringParameterName() . '=' . $requestId;
Expand Down
13 changes: 8 additions & 5 deletions src/Core/ApiLevelErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,19 @@
*/
class ApiLevelErrorHandler
{
protected LoggerInterface $logger;
protected const ERROR_KEY = 'error';

protected const RESULT_KEY = 'result';

protected const RESULT_ERROR_KEY = 'result_error';

protected const ERROR_DESCRIPTION_KEY = 'error_description';

/**
* ApiLevelErrorHandler constructor.
*
* @param LoggerInterface $logger
*/
public function __construct(LoggerInterface $logger)
public function __construct(protected LoggerInterface $logger)
{
$this->logger = $logger;
}

/**
Expand Down Expand Up @@ -84,14 +83,17 @@ private function handleError(array $responseBody, ?string $batchCommandId = null
if ($batchCommandId !== null) {
$batchErrorPrefix = sprintf(' batch command id: %s', $batchCommandId);
}

// todo send issues to bitrix24
// fix errors without error_code responses
if ($errorCode === '' && strtolower($errorDescription) === strtolower('You can delete ONLY templates created by current application')) {
$errorCode = 'bizproc_workflow_template_access_denied';
}

if ($errorCode === '' && strtolower($errorDescription) === strtolower('No fields to update.')) {
$errorCode = 'bad_request_no_fields_to_update';
}

if ($errorCode === '' && strtolower($errorDescription) === strtolower('User is not found or is not active')) {
$errorCode = 'user_not_found_or_is_not_active';
}
Expand Down Expand Up @@ -119,6 +121,7 @@ private function handleError(array $responseBody, ?string $batchCommandId = null
default:
throw new BaseException(sprintf('%s - %s %s', $errorCode, $errorDescription, $batchErrorPrefix));
}

// switch (strtoupper(trim($apiResponse['error']))) {
// case 'EXPIRED_TOKEN':
// throw new Bitrix24TokenIsExpiredException($errorMsg);
Expand Down
Loading
Loading