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

List only API products purchased by a team for m10n #336

Closed
wants to merge 8 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Apigee\Edge\Api\Monetization\Controller;

use Apigee\Edge\Api\Monetization\Entity\CompanyEligibleRatePlan;
use Apigee\Edge\Api\Monetization\Normalizer\EntityNormalizer;
use Apigee\Edge\ClientInterface;
use Apigee\Edge\Serializer\EntitySerializerInterface;
use Psr\Http\Message\UriInterface;

class CompanyEligibleRatePlanController extends EligibleRatePlanController
{
/**
* Name of the company.
*
* @var string
*/
protected $companyName;

/**
* CompanyEligibleRatePlanController constructor.
*
* @param string $companyName
* @param string $organization
* @param ClientInterface $client
* @param \Apigee\Edge\Serializer\EntitySerializerInterface|null $entitySerializer
*/
public function __construct(string $companyName, string $organization, ClientInterface $client, ?EntitySerializerInterface $entitySerializer = null)
{
parent::__construct($organization, $client, $entitySerializer);
$this->companyName = $companyName;
}

/**
* {@inheritdoc}
*/
protected function getBaseEndpointUri(): UriInterface
{
return $this->client->getUriFactory()->createUri("/mint/organizations/{$this->organization}/companies/{$this->companyName}/eligible-products");
}

/**
* {@inheritdoc}
*/
protected function getEntityClass(): string
{
return CompanyEligibleRatePlan::class;
}

/**
* {@inheritdoc}
*/
protected function buildContextForEntityTransformerInCreate(): array
{
$context = [];
$context[EntityNormalizer::MINT_ENTITY_REFERENCE_PROPERTY_VALUES]['developer'] = $this->companyName;

return $context;
}

/**
* {@inheritdoc}
*/
protected function getEligibleRatePlanEndpoint(): UriInterface
{
// For this API endpoint:
// https://apidocs.apigee.com/docs/monetization/1/routes/mint/organizations/%7Borg_name%7D/companies/%7Bcompany_id%7D/eligible-products/get
return $this->client->getUriFactory()->createUri("/mint/organizations/{$this->organization}/companies/{$this->companyName}/eligible-products");
}
}
171 changes: 171 additions & 0 deletions src/Api/Monetization/Controller/EligibleRatePlanController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<?php

/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Apigee\Edge\Api\Monetization\Controller;

use Apigee\Edge\Api\Monetization\Entity\AcceptedRatePlanInterface;
use Apigee\Edge\Api\Monetization\Entity\RatePlanInterface;
use Apigee\Edge\Api\Monetization\Serializer\AcceptedRatePlanSerializer;
use Apigee\Edge\ClientInterface;
use Apigee\Edge\Controller\EntityListingControllerTrait;
use Apigee\Edge\Controller\EntityLoadOperationControllerTrait;
use Apigee\Edge\Serializer\EntitySerializerInterface;
use DateTimeImmutable;
use Psr\Http\Message\UriInterface;
use ReflectionClass;

abstract class EligibleRatePlanController extends OrganizationAwareEntityController implements EligibleRatePlanControllerInterface
{
use EntityListingControllerTrait;
use EntityLoadOperationControllerTrait;
use PaginatedListingHelperTrait;

/**
* EligibleRatePlanController constructor.
*
* @param string $organization
* @param ClientInterface $client
* @param \Apigee\Edge\Serializer\EntitySerializerInterface|null $entitySerializer
*/
public function __construct(string $organization, ClientInterface $client, ?EntitySerializerInterface $entitySerializer = null)
{
$entitySerializer = $entitySerializer ?? new AcceptedRatePlanSerializer();
parent::__construct($organization, $client, $entitySerializer);
}

/**
* {@inheritdoc}
*/
public function getAllEligibleRatePlans(): array
{
return $this->getEligibleRatePlan();
}

/**
* {@inheritdoc}
*/
public function acceptRatePlan(RatePlanInterface $ratePlan, DateTimeImmutable $startDate, ?DateTimeImmutable $endDate = null, ?int $quotaTarget = null, ?bool $suppressWarning = null, ?bool $waveTerminationCharge = null): AcceptedRatePlanInterface
{
$rc = new ReflectionClass($this->getEntityClass());
/** @var AcceptedRatePlanInterface $acceptedRatePlan */
$acceptedRatePlan = $rc->newInstance(
[
'ratePlan' => $ratePlan,
'startDate' => $startDate,
]
);
if (null !== $quotaTarget) {
$acceptedRatePlan->setQuotaTarget($quotaTarget);
}
if (null !== $endDate) {
$acceptedRatePlan->setEndDate($endDate);
}
$payload = $this->getEntitySerializer()->serialize($acceptedRatePlan, 'json', $this->buildContextForEntityTransformerInCreate());
$tmp = json_decode($payload, true);
if (null !== $suppressWarning) {
$tmp['suppressWarning'] = $suppressWarning ? 'true' : 'false';
}
if (null !== $waveTerminationCharge) {
$tmp['waveTerminationCharge'] = $waveTerminationCharge ? 'true' : 'false';
}
$payload = json_encode($tmp);
$response = $this->client->post($this->getBaseEndpointUri(), $payload);
$this->getEntitySerializer()->setPropertiesFromResponse($response, $acceptedRatePlan);

return $acceptedRatePlan;
}

/**
* {@inheritdoc}
*
* @psalm-suppress PossiblyNullArgument - id is not null in this context.
*/
public function updateSubscription(AcceptedRatePlanInterface $acceptedRatePlan, ?bool $suppressWarning = null, ?bool $waveTerminationCharge = null): void
{
$payload = $this->getEntitySerializer()->serialize($acceptedRatePlan, 'json', $this->buildContextForEntityTransformerInCreate());
$tmp = json_decode($payload, true);
if (null !== $suppressWarning) {
$tmp['suppressWarning'] = $suppressWarning ? 'true' : 'false';
}
if (null !== $waveTerminationCharge) {
$tmp['waveTerminationCharge'] = $waveTerminationCharge ? 'true' : 'false';
}
$this->alterRequestPayload($tmp, $acceptedRatePlan);
$payload = json_encode($tmp);
// Update an existing entity.
$response = $this->client->put($this->getEntityEndpointUri($acceptedRatePlan->id()), $payload);
$this->getEntitySerializer()->setPropertiesFromResponse($response, $acceptedRatePlan);
}

/**
* Returns the URI for listing rate plans eligible to access.
*
* Gets the API products that a company is eligible to access, including:
* API products for which a company has accepted a rate plan.
* API products that do not have a published rate plan.
*
* @return UriInterface
*/
abstract protected function getEligibleRatePlanEndpoint(): UriInterface;

/**
* Builds context for the entity normalizer.
*
* Allows controllers to add extra metadata to the payload.
*
* @return array
*/
abstract protected function buildContextForEntityTransformerInCreate(): array;

/**
* Allows to alter payload before it gets sent to the API.
*
* @param array $payload
* API request payload.
*/
protected function alterRequestPayload(array &$payload, AcceptedRatePlanInterface $acceptedRatePlan): void
{
}

/**
* Helper function for listing eligible rate plans.
*
* @param array $query_params
* Additional query parameters.
*
* @return \Apigee\Edge\Api\Monetization\Entity\AcceptedRatePlanInterface[]
*
* @psalm-suppress PossiblyNullArrayOffset - id() does not return null here.
*/
private function getEligibleRatePlan(): array
{
$entities = [];

foreach ($this->getRawList($this->getEligibleRatePlanEndpoint()) as $item) {
/** @var \Apigee\Edge\Entity\EntityInterface $tmp */
$tmp = $this->getEntitySerializer()->denormalize(
$item,
AcceptedRatePlanInterface::class,
'json'
);
$entities[$tmp->id()] = $tmp;
}

return $entities;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Apigee\Edge\Api\Monetization\Controller;

use Apigee\Edge\Api\Monetization\Entity\AcceptedRatePlanInterface;
use Apigee\Edge\Api\Monetization\Entity\RatePlanInterface;
use Apigee\Edge\Controller\EntityControllerInterface;
use Apigee\Edge\Controller\EntityLoadOperationControllerInterface;
use DateTimeImmutable;

/**
* Interface EligibleRatePlanControllerInterface.
*
* @see https://apidocs.apigee.com/monetize/apis/
* @see https://docs.apigee.com/api-platform/monetization/subscribe-published-rate-plan-using-api
* @see https://docs.apigee.com/api-platform/monetization/view-rate-plans#viewingrateplansusingtheapi-viewingallacceptedrateplansforadeveloperusingtheapi
*/
interface EligibleRatePlanControllerInterface extends EntityControllerInterface,
EntityLoadOperationControllerInterface
{
/**
* Gets all eligible rate plans.
*
* @return \Apigee\Edge\Api\Monetization\Entity\AcceptedRatePlanInterface[]
*/
public function getAllEligibleRatePlans(): array;

/**
* Accepts a rate plan.
*
* @param RatePlanInterface $ratePlan
* The rate plan to be accepted.
* @param DateTimeImmutable $startDate
* Date when the rate plan starts.
* @param DateTimeImmutable|null $endDate
* Date when the rate plan ends.
* @param int|null $quotaTarget
* (This property is valid for adjustable notification rate plans only.)
* Target number of transactions allowed for the app developer.
* @param bool|null $suppressWarning
* Flag that specifies whether to suppress the error if the developer
* attempts to accept a rate plan that overlaps another accepted
* rate plan.
* @param bool|null $waveTerminationCharge
* Flag that specifies whether termination fees are waved when an active
* rate plan is terminated as part of activating the new rate plan.
*
* @return AcceptedRatePlanInterface
*/
public function acceptRatePlan(RatePlanInterface $ratePlan, DateTimeImmutable $startDate, ?DateTimeImmutable $endDate = null, ?int $quotaTarget = null, ?bool $suppressWarning = null, ?bool $waveTerminationCharge = null): AcceptedRatePlanInterface;

/**
* Update a rate plan that has been accepted by a developer.
*
* @param AcceptedRatePlanInterface $acceptedRatePlan
* Previously accepted rate plan that should be modified.
* @param bool|null $suppressWarning
* Flag that specifies whether to suppress the error if the developer
* attempts to accept a rate plan that overlaps another accepted
* rate plan.
* @param bool|null $waveTerminationCharge
* Flag that specifies whether termination fees are waved when an active
* rate plan is terminated as part of activating the new rate plan.
*/
public function updateSubscription(AcceptedRatePlanInterface $acceptedRatePlan, ?bool $suppressWarning = null, ?bool $waveTerminationCharge = null): void;
}
Loading
Loading