Skip to content

Commit

Permalink
New feature for Apigee X monetization phase2 (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
divya-intelli authored Oct 18, 2021
1 parent daa25ab commit e86bd31
Show file tree
Hide file tree
Showing 25 changed files with 1,224 additions and 0 deletions.
93 changes: 93 additions & 0 deletions src/Api/ApigeeX/Controller/BillingTypeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

/*
* Copyright 2021 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\ApigeeX\Controller;

use Apigee\Edge\Api\ApigeeX\Entity\BillingTypeInterface;
use Apigee\Edge\Api\ApigeeX\Serializer\BillingTypeSerializer;
use Apigee\Edge\Api\Monetization\Controller\EntityController;
use Apigee\Edge\ClientInterface;
use Apigee\Edge\Controller\EntityListingControllerTrait;
use Apigee\Edge\Controller\EntityLoadOperationControllerTrait;
use Apigee\Edge\Serializer\EntitySerializerInterface;

abstract class BillingTypeController extends EntityController implements BillingTypeControllerInterface
{
use EntityListingControllerTrait;
use EntityLoadOperationControllerTrait;
use PaginatedListingHelperTrait;

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

/**
* {@inheritdoc}
*/
public function getAllBillingDetails(): BillingTypeInterface
{
return $this->getDeveloperBillingType();
}

/**
* {@inheritdoc}
*/
public function updateBillingType($billingtype): BillingTypeInterface
{
$response = $this->client->put(
$this->getBaseEndpointUri(),
(string) json_encode((object) [
'billingType' => $billingtype,
])
);

return $this->entitySerializer->deserialize(
(string) $response->getBody(),
$this->getEntityClass(),
'json'
);
}

/**
* Helper function for getting the billing type.
*
* @return \Apigee\Edge\Api\ApigeeX\Entity\BillingTypeInterface
*/
private function getDeveloperBillingType(): BillingTypeInterface
{
$item = $this->getRawSingleList($this->getBaseEndpointUri());

/** @var \Apigee\Edge\Entity\EntityInterface $tmp */
$tmp = $this->getEntitySerializer()->denormalize(
$item,
BillingTypeInterface::class,
'json'
);

return $tmp;
}
}
48 changes: 48 additions & 0 deletions src/Api/ApigeeX/Controller/BillingTypeControllerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* Copyright 2021 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\ApigeeX\Controller;

use Apigee\Edge\Api\ApigeeX\Entity\BillingTypeInterface;
use Apigee\Edge\Controller\EntityControllerInterface;
use Apigee\Edge\Controller\EntityLoadOperationControllerInterface;

/**
* Interface BillingTypeControllerInterface.
*
* TODO: Add reference documentation link
*/
interface BillingTypeControllerInterface extends
EntityControllerInterface,
EntityLoadOperationControllerInterface
{
/**
* Gets the billingtype.
*
* @return \Apigee\Edge\Api\ApigeeX\Entity\BillingTypeInterface
*/
public function getAllBillingDetails(): BillingTypeInterface;

/**
* Update the billing type of the developer.
*
* @param $billingtype
* Billing type that is switched to.
*/
public function updateBillingType($billingtype): BillingTypeInterface;
}
66 changes: 66 additions & 0 deletions src/Api/ApigeeX/Controller/DeveloperBillingTypeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/*
* Copyright 2021 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\ApigeeX\Controller;

use Apigee\Edge\Api\ApigeeX\Entity\DeveloperBillingType;
use Apigee\Edge\ClientInterface;
use Apigee\Edge\Serializer\EntitySerializerInterface;
use Psr\Http\Message\UriInterface;

class DeveloperBillingTypeController extends BillingTypeController
{
/**
* Email address of a developer.
*
* @var string
*/
protected $developer;

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

/**
* {@inheritdoc}
*/
protected function getBaseEndpointUri(): UriInterface
{
$developerId = rawurlencode($this->developer);

return $this->client->getUriFactory()->createUri("/organizations/{$this->organization}/developers/{$developerId}/monetizationConfig");
}

/**
* {@inheritdoc}
*/
protected function getEntityClass(): string
{
return DeveloperBillingType::class;
}
}
67 changes: 67 additions & 0 deletions src/Api/ApigeeX/Controller/DeveloperPrepaidBalanceController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/*
* Copyright 2021 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\ApigeeX\Controller;

use Apigee\Edge\ClientInterface;
use Apigee\Edge\Serializer\EntitySerializerInterface;
use Psr\Http\Message\UriInterface;

class DeveloperPrepaidBalanceController extends PrepaidBalanceController implements DeveloperPrepaidBalanceControllerInterface
{
/**
* Email address of a developer.
*
* @var string
*/
protected $developerId;

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

/**
* {@inheritdoc}
*/
protected function getBaseEndpointUri(): UriInterface
{
$developerId = rawurlencode($this->developerId);

return $this->client->getUriFactory()->createUri("/organizations/{$this->organization}/developers/{$developerId}/balance:credit");
}

/**
* {@inheritdoc}
*/
protected function getPrepaidBalanceEndpoint(): UriInterface
{
$developerId = rawurlencode($this->developerId);

return $this->client->getUriFactory()->createUri("/organizations/{$this->organization}/developers/{$developerId}/balance");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* Copyright 2021 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\ApigeeX\Controller;

/**
* Interface DeveloperPrepaidBalanceControllerInterface.
*/
interface DeveloperPrepaidBalanceControllerInterface extends PrepaidBalanceControllerInterface
{
}
18 changes: 18 additions & 0 deletions src/Api/ApigeeX/Controller/ListingHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ protected function listEntities(UriInterface $uri): array
protected function getRawList(UriInterface $uri): array
{
$response = $this->getClient()->get($uri);

$responseArray = $this->responseToArray($response);

//ApigeeX can return empty array.
Expand All @@ -54,6 +55,23 @@ protected function getRawList(UriInterface $uri): array
return reset($responseArray);
}

/**
* Returns a raw API response as an array when there is single value in api response.
*
* @param \Psr\Http\Message\UriInterface $uri
* URI of the endpoint where the request should be sent.
*
* @return array
* API response as an array.
*/
protected function getRawSingleList(UriInterface $uri): array
{
$response = $this->getClient()->get($uri);
$responseArray = $this->responseToArray($response);

return $responseArray;
}

abstract protected function responseToArray(ResponseInterface $response, bool $expandCompatibility = false): array;

abstract protected function responseArrayToArrayOfEntities(array $responseArray, string $keyGetter = 'id'): array;
Expand Down
Loading

0 comments on commit e86bd31

Please sign in to comment.