Skip to content

Commit

Permalink
- Added new methods to Report API
Browse files Browse the repository at this point in the history
  • Loading branch information
bhdnb committed Jan 23, 2025
1 parent f6d85b6 commit 9c2c53a
Show file tree
Hide file tree
Showing 24 changed files with 2,123 additions and 212 deletions.
91 changes: 24 additions & 67 deletions src/CrowdinApiClient/Api/AbstractApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use CrowdinApiClient\Model\ModelInterface;

/**
* Class AbstractApi
* @package Crowdin\Api
* @internal
*/
Expand All @@ -24,74 +23,31 @@ abstract class AbstractApi implements ApiInterface
*/
protected $headers = [];

/**
* @param Crowdin $client
*/
public function __construct(Crowdin $client)
{
$this->client = $client;
$this->setHeader('Content-Type', 'application/json');
}

public function addHeader($header, $value)
{
$this->headers[strtolower($header)] = $value;

return $this;
}

public function addHeaders(array $headers)
{
foreach ($headers as $key => $value) {
$this->addHeader($key, $value);
}

return $this;
}

public function getHeader($header)
{
return $this->headers[strtolower($header)];
}

public function getHeaders(): array
protected function getHeaders(): array
{
return $this->headers;
}

public function setHeader($header, $value)
protected function setHeader(string $header, string $value): void
{
unset($this->headers[strtolower($header)]);
$this->removeHeader($header);
$this->addHeader($header, $value);

return $this;
}

public function setHeaders(array $headers)
{
$this->clearHeaders();
foreach ($headers as $key => $value) {
$this->addHeader($key, $value);
}

return $this;
}

public function hasHeader($header): bool
{
return isset($this->headers[strtolower($header)]);
}

public function clearHeaders()
protected function addHeader(string $header, string $value): void
{
$this->headers = [];
$this->headers[strtolower($header)] = $value;
}

public function removeHeader($header)
protected function removeHeader(string $header): void
{
unset($this->headers[strtolower($header)]);

return $this;
}

/**
Expand All @@ -101,26 +57,32 @@ public function removeHeader($header)
*/
protected function _update(string $path, ModelInterface $model)
{
$dataModel = $model->getProperties();
$_data = [];
$properties = $model->getProperties();
$data = [];

foreach ($model->getData() as $key => $value) {
$property = $properties[$key] ?? null;
// TODO: Introduce a new interface?
if (is_object($property)) {
$property = $property->toArray();
}

foreach ($model->getData() as $key => $val) {
if (isset($dataModel[$key]) && $dataModel[$key] != $val) {
$_data[] = [
if (isset($property) && $property != $value) {
$data[] = [
'op' => 'replace',
'path' => '/' . $key,
'value' => $dataModel[$key]
'value' => $property,
];
}
}

if (empty($_data)) {
if ($data === []) {
return $model;
}

$options = [
'body' => json_encode($_data),
'headers' => $this->getHeaders()
'body' => json_encode($data),
'headers' => $this->getHeaders(),
];

return $this->client->apiRequest('patch', $path, new ResponseModelDecorator(get_class($model)), $options);
Expand Down Expand Up @@ -151,12 +113,7 @@ protected function _list(string $path, string $modelName, array $params = [])
*/
protected function _create(string $path, string $modelName, array $data)
{
$options = [
'body' => json_encode($data),
'headers' => $this->getHeaders()
];

return $this->client->apiRequest('post', $path, new ResponseModelDecorator($modelName), $options);
return $this->_post($path, $modelName, $data);
}

/**
Expand Down Expand Up @@ -200,7 +157,7 @@ protected function _put(string $path, string $modelName, array $data)
{
$options = [
'body' => json_encode($data),
'headers' => $this->getHeaders()
'headers' => $this->getHeaders(),
];

return $this->client->apiRequest('put', $path, new ResponseModelDecorator($modelName), $options);
Expand Down Expand Up @@ -234,7 +191,7 @@ protected function _patch(string $path, string $modelName, array $body, array $p
$options = [
'body' => json_encode($body),
'headers' => $this->getHeaders(),
'params' => $params
'params' => $params,
];

return $this->client->apiRequest('patch', $path, new ResponseModelDecorator($modelName), $options);
Expand Down
80 changes: 70 additions & 10 deletions src/CrowdinApiClient/Api/ReportApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use CrowdinApiClient\Model\DownloadFile;
use CrowdinApiClient\Model\Report;
use CrowdinApiClient\Model\ReportSettingsTemplate;
use CrowdinApiClient\ModelCollection;

/**
* Reports help to estimate costs, calculate translation costs, and identify the top members.
Expand Down Expand Up @@ -36,10 +38,6 @@ public function generate(int $projectId, array $data): ?Report
* Check Report Generation Status
* @link https://developer.crowdin.com/api/v2/#operation/api.projects.reports.get API Documentation
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.reports.get API Documentation Enterprise
*
* @param int $projectId
* @param string $reportId
* @return Report|null
*/
public function get(int $projectId, string $reportId): ?Report
{
Expand All @@ -49,16 +47,78 @@ public function get(int $projectId, string $reportId): ?Report

/**
* Download Report
* @link https://developer.crowdin.com/api/v2/#operation/api.projects.reports.download.get API Documentation
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.reports.download.get API Documentation Enterprise
*
* @param int $projectId
* @param string $reportId
* @return DownloadFile|null
* @link https://developer.crowdin.com/api/v2/#operation/api.projects.reports.download.download API Documentation
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.projects.reports.download.download API Documentation Enterprise
*/
public function download(int $projectId, string $reportId): ?DownloadFile
{
$path = sprintf('projects/%d/reports/%s/download', $projectId, $reportId);
return $this->_get($path, DownloadFile::class);
}

/**
* List Report Settings Templates
* @link https://developer.crowdin.com/api/v2/#operation/api.projects.reports.settings-templates.getMany API Documentation
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.reports.settings-templates.getMany API Documentation Enterprise
*/
public function listReportSettingsTemplates(int $projectId): ModelCollection
{
$path = sprintf('projects/%d/reports/settings-templates', $projectId);
return $this->_list($path, ReportSettingsTemplate::class);
}

/**
* Get Report Settings Template
* @link https://developer.crowdin.com/api/v2/#operation/api.projects.reports.settings-templates.get API Documentation
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.reports.settings-templates.get API Documentation Enterprise
*/
public function getReportSettingsTemplate(int $projectId, int $reportSettingsTemplateId): ?ReportSettingsTemplate
{
$path = sprintf('projects/%d/reports/settings-templates/%d', $projectId, $reportSettingsTemplateId);
return $this->_get($path, ReportSettingsTemplate::class);
}

/**
* Add Report Settings Template
* @link https://developer.crowdin.com/api/v2/#operation/api.projects.reports.settings-templates.post API Documentation
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.reports.settings-templates.post API Documentation Enterprise
*/
public function createReportSettingsTemplate(int $projectId, array $data): ?ReportSettingsTemplate
{
// TODO: Dont forget about isGlobal and isPublic fields
$forbiddenKeys = ['id', 'createdAt', 'updatedAt'];
$path = sprintf('projects/%d/reports/settings-templates', $projectId);

return $this->_post(
$path,
ReportSettingsTemplate::class,
array_filter($data, static function (string $key) use ($forbiddenKeys): bool {
return !in_array($key, $forbiddenKeys, true);
}, ARRAY_FILTER_USE_KEY)
);
}

/**
* TODO: Dont forget to test me! This method is broken right now
* Edit Report Settings Template
* @link https://developer.crowdin.com/api/v2/#operation/api.projects.reports.settings-templates.patch API Documentation
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.reports.settings-templates.patch API Documentation Enterprise
*/
public function updateReportSettingsTemplate(
int $projectId,
ReportSettingsTemplate $reportSettingsTemplate
): ?ReportSettingsTemplate {
$path = sprintf('projects/%d/reports/settings-templates/%d', $projectId, $reportSettingsTemplate->getId());
return $this->_update($path, $reportSettingsTemplate);
}

/**
* Delete Report Settings Template
* @link https://developer.crowdin.com/api/v2/#operation/api.projects.reports.settings-templates.delete API Documentation
* @link https://developer.crowdin.com/enterprise/api/v2/#operation/api.reports.settings-templates.delete API Documentation Enterprise
*/
public function deleteReportSettingsTemplate(int $projectId, int $reportSettingsTemplateId): void
{
$this->_delete(sprintf('projects/%d/reports/settings-templates/%d', $projectId, $reportSettingsTemplateId));
}
}
32 changes: 12 additions & 20 deletions src/CrowdinApiClient/Crowdin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use UnexpectedValueException;

/**
* Class Crowdin
* @package Crowdin
*
* @property \CrowdinApiClient\Api\StorageApi $storage
Expand Down Expand Up @@ -114,7 +113,7 @@ class Crowdin
'bundle',
'notification',
'organizationWebhook',
'reportArchive'
'reportArchive',
];

protected $servicesEnterprise = [
Expand Down Expand Up @@ -151,7 +150,7 @@ class Crowdin
'bundle',
'notification',
'organizationWebhook',
'reportArchive'
'reportArchive',
];

/**
Expand All @@ -167,7 +166,7 @@ public function __construct(array $config)
'http_client_handler' => null,
'response_error_handler' => null,
'access_token' => null,
'organization' => null
'organization' => null,
], $config);

$this->accessToken = $config['access_token'];
Expand All @@ -179,16 +178,15 @@ public function __construct(array $config)
}

/**
* @param string $method
* @param string $path
* @param ResponseDecoratorInterface|null $decorator
* @param array $options
* @return mixed
*/
public function apiRequest(string $method, string $path, ResponseDecoratorInterface $decorator = null, array $options = [])
{
public function apiRequest(
string $method,
string $path,
?ResponseDecoratorInterface $decorator = null,
array $options = []
) {
$response = $this->request($method, $this->getFullUrl($path), $options);

$response = json_decode($response, true);

$this->responseErrorHandler->check($response);
Expand All @@ -209,9 +207,9 @@ public function apiRequest(string $method, string $path, ResponseDecoratorInterf
}

/**
* @internal
* @param $name
* @return mixed
* @internal
*/
public function __get($name)
{
Expand Down Expand Up @@ -272,6 +270,7 @@ public function setOrganization(?string $organization): void
{
$this->organization = $organization;
$this->isEnterprise = (bool)$organization;

$this->updateBaseUri();
}

Expand All @@ -283,15 +282,11 @@ protected function updateBaseUri(): self
}

/**
* @param string $method
* @param string $uri
* @param array $options
* @return mixed
*/
protected function request(string $method, string $uri, array $options = [])
{
$options['body'] = $options['body'] ?? null;

$options['headers'] = array_merge([
'Authorization' => 'Bearer ' . $this->accessToken,
], $options['headers'] ?? []);
Expand All @@ -300,15 +295,12 @@ protected function request(string $method, string $uri, array $options = [])
$uri .= '?' . http_build_query($options['params']);
$options['body'] = null;
}
$response = $this->client->request($method, $uri, $options);

return $response;
return $this->client->request($method, $uri, $options);
}

/**
* @internal
* @param string $name
* @return ApiInterface
*/
protected function getApi(string $name): ApiInterface
{
Expand Down
Loading

0 comments on commit 9c2c53a

Please sign in to comment.