Skip to content

Commit

Permalink
Merge pull request #682 from Boardfy/fix/unrestricted-report-document
Browse files Browse the repository at this point in the history
Fixes use restricted data token on unrestricted report documents
  • Loading branch information
jlevers authored Apr 20, 2024
2 parents 6cf9a8c + 9572ff0 commit 6100e79
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 40 deletions.
64 changes: 33 additions & 31 deletions src/Generator/Generators/RequestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,39 +64,41 @@ protected function generateRequestClass(Endpoint $endpoint): PhpFile

$isRestricted = isset($restrictedOperations->{$path}->operations->{$httpMethod});
$isGrantless = isset($grantlessOperations->{$path}->{$httpMethod});
if ($isRestricted || $isGrantless) {
if ($isRestricted) {
$namespace->addUse(RestrictedDataToken::class);
$useGenericPath = $restrictedOperations->{$path}->genericPath;
$knownDataElements = $restrictedOperations->{$path}->operations->{$httpMethod};
$constructor->addBody(
new Literal(sprintf(
'$rdtMiddleware = new RestrictedDataToken(%s, \'%s\', %s);',
$useGenericPath ? "'$path'" : '$this->resolveEndpoint()',
strtoupper($httpMethod),
'['.implode(', ', array_map(fn ($el) => "'$el'", $knownDataElements)).']'
))
);
$constructor->addBody('$this->middleware()->onRequest($rdtMiddleware);');
} elseif ($isGrantless) {
$namespace
->addUse(Grantless::class)
->addUse(GrantlessScope::class);
$scope = GrantlessScope::from($grantlessOperations->{$path}->{$httpMethod});

$constructor->addBody(
new Literal(sprintf(
'$this->middleware()->onRequest(new Grantless(GrantlessScope::%s));',
$scope->name
))
);
}
}

$requestMiddleware = $middleware->{$path}->{$httpMethod}->request ?? [];
foreach ($requestMiddleware as $cls) {
$namespace->addUse(PACKAGE_NAMESPACE."\\Middleware\\$cls");
$constructor->addBody(new Literal(sprintf('$this->middleware()->onRequest(new %s);', $cls)));
if ($requestMiddleware) {
// If there is request middleware besides the RDT or Grantless middlewares applied to a request
// that also is grantless or uses an RDT, the other middleware needs to invoke the RDT and/or
// Grantless middleware, otherwise it will not be applied
foreach ($requestMiddleware as $cls) {
$namespace->addUse(PACKAGE_NAMESPACE."\\Middleware\\$cls");
$constructor->addBody(new Literal(sprintf('$this->middleware()->onRequest(new %s);', $cls)));
}
} elseif ($isRestricted) {
$namespace->addUse(RestrictedDataToken::class);
$useGenericPath = $restrictedOperations->{$path}->genericPath;
$knownDataElements = $restrictedOperations->{$path}->operations->{$httpMethod};
$constructor->addBody(
new Literal(sprintf(
'$rdtMiddleware = new RestrictedDataToken(%s, \'%s\', %s);',
$useGenericPath ? "'$path'" : '$this->resolveEndpoint()',
strtoupper($httpMethod),
'['.implode(', ', array_map(fn ($el) => "'$el'", $knownDataElements)).']'
))
);
$constructor->addBody('$this->middleware()->onRequest($rdtMiddleware);');
} elseif ($isGrantless) {
$namespace
->addUse(Grantless::class)
->addUse(GrantlessScope::class);
$scope = GrantlessScope::from($grantlessOperations->{$path}->{$httpMethod});

$constructor->addBody(
new Literal(sprintf(
'$this->middleware()->onRequest(new Grantless(GrantlessScope::%s));',
$scope->name
))
);
}

// Remove the constructor if it's not being used
Expand Down
11 changes: 8 additions & 3 deletions src/Middleware/RestrictedReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ public function __invoke(PendingRequest $pendingRequest)

$connector = $pendingRequest->getConnector();
if (
! $reports[$reportType]['restricted']
|| Endpoint::isSandbox(Endpoint::tryFrom($connector->endpoint))
$reports[$reportType]['restricted']
&& ! Endpoint::isSandbox($connector->endpoint)
) {
$pendingRequest->authenticate($connector->lwaAuth());
$rdtMiddleware = new RestrictedDataToken(
$pendingRequest->getRequest()->resolveEndpoint(),
$pendingRequest->getMethod()->value,
[]
);
$rdtMiddleware($pendingRequest);
}

// The reportType key is not part of the actual API request. We added it to make it
Expand Down
3 changes: 0 additions & 3 deletions src/Seller/ReportsV20210630/Requests/GetReportDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;
use SellingPartnerApi\Middleware\RestrictedDataToken;
use SellingPartnerApi\Middleware\RestrictedReport;
use SellingPartnerApi\Seller\ReportsV20210630\Responses\ErrorList;
use SellingPartnerApi\Seller\ReportsV20210630\Responses\ReportDocument;
Expand All @@ -26,8 +25,6 @@ public function __construct(
protected string $reportDocumentId,
protected string $reportType,
) {
$rdtMiddleware = new RestrictedDataToken($this->resolveEndpoint(), 'GET', []);
$this->middleware()->onRequest($rdtMiddleware);
$this->middleware()->onRequest(new RestrictedReport);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Traits/DownloadsDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ protected static function documentTypeInfo(string $documentTypeName): array

// Check feeds first because the file is smaller
$feedTypes = json_decode(file_get_contents(RESOURCE_DIR.'/feeds.json'), true);
if ($feedTypes[$documentTypeName]) {
if (isset($feedTypes[$documentTypeName])) {
$documentTypeInfo = [
'name' => $documentTypeName,
'contentType' => ContentType::from($feedTypes[$documentTypeName]),
Expand All @@ -200,7 +200,7 @@ protected static function documentTypeInfo(string $documentTypeName): array

if (! $documentTypeInfo) {
$reportTypes = json_decode(file_get_contents(RESOURCE_DIR.'/reports.json'), true);
if ($reportTypes[$documentTypeName]) {
if (isset($reportTypes[$documentTypeName])) {
$documentTypeInfo = $reportTypes[$documentTypeName];
$documentTypeInfo['name'] = $documentTypeName;
$documentTypeInfo['contentType'] = ContentType::from($documentTypeInfo['contentType']);
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/UploadsDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function upload(
public static function getContentType(string $feedType, ?string $charset = null): string
{
$feedTypes = json_decode(file_get_contents(RESOURCE_DIR.'/feeds.json'), true);
$contentType = $feedTypes[$feedType];
$contentType = $feedTypes[$feedType] ?? null;

if (! $contentType) {
throw new RuntimeException("Unknown feed type '{$feedType}'");
Expand Down

0 comments on commit 6100e79

Please sign in to comment.