Skip to content

Commit

Permalink
Merge pull request #94 from arlina-espinoza/2.x
Browse files Browse the repository at this point in the history
Merge 2.x-hybrid branch into 2.x
  • Loading branch information
arlina-espinoza authored Dec 5, 2019
2 parents 665d18e + 5cd3373 commit 5a650bf
Show file tree
Hide file tree
Showing 54 changed files with 1,418 additions and 135 deletions.
22 changes: 5 additions & 17 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
<?php

$header = <<<HEADER
Copyright 2018 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.
HEADER;

$finder = PhpCsFixer\Finder::create()
->files()
->name('*.php')
->name('*.inc')
->in([__DIR__ . '/examples', __DIR__ . '/src', __DIR__ . '/tests']);

/**
* The 'header_comment' validation was removed because the copyright year needs to be pinned to the year the file was
* added to the repo.
* @todo: Add a copyright validation (https://github.com/apigee/apigee-client-php/issues/81).
*/
return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules([
Expand All @@ -30,7 +19,6 @@ return PhpCsFixer\Config::create()
'array_syntax' => ['syntax' => 'short'],
'class_definition' => ['singleLine' => false, 'singleItemSingleLine' => true],
'concat_space' => ['spacing' => 'one'],
'header_comment' => ['header' => $header],
'general_phpdoc_annotation_remove' => ['author'],
'ordered_class_elements' => true,
'ordered_imports' => true,
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
"require": {
"php": "^7.1",
"ext-json": "*",
"ext-openssl": "*",
"ext-reflection": "*",
"fightbulc/moment": "^1.26",
"firebase/php-jwt": "^5.0",
"league/period": "^3.4",
"php-http/client-common": "^1.9",
"php-http/client-implementation": "^1.0",
Expand Down
2 changes: 2 additions & 0 deletions src/Api/Management/Controller/AppCredentialController.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public function addProducts(string $consumerKey, array $apiProducts): AppCredent
*/
public function setApiProductStatus(string $consumerKey, string $apiProduct, string $status): void
{
$apiProduct = rawurlencode($apiProduct);
$uri = $this->getBaseEndpointUri()
->withPath("{$this->getBaseEndpointUri()}/keys/{$consumerKey}/apiproducts/{$apiProduct}")
->withQuery(http_build_query(['action' => $status]));
Expand All @@ -135,6 +136,7 @@ public function setApiProductStatus(string $consumerKey, string $apiProduct, str
*/
public function deleteApiProduct(string $consumerKey, string $apiProduct): AppCredentialInterface
{
$apiProduct = rawurlencode($apiProduct);
$uri = $this->getBaseEndpointUri()->withPath("{$this->getBaseEndpointUri()}/keys/{$consumerKey}/apiproducts/{$apiProduct}");
$response = $this->client->delete($uri);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ protected function getEntityAttributesUri(string $entityId): UriInterface
*/
protected function getEntityAttributeUri(string $entityId, string $name): UriInterface
{
$encoded = rawurlencode($name);
$uri = $this->getEntityAttributesUri($entityId)->withPath(
$this->getEntityAttributesUri($entityId) . '/' . $name
$this->getEntityAttributesUri($entityId) . '/' . $encoded
);

return $uri;
Expand All @@ -140,5 +141,5 @@ protected function getEntityAttributeUri(string $entityId, string $name): UriInt
/**
* @inheritdoc
*/
abstract protected function responseToArray(ResponseInterface $response): array;
abstract protected function responseToArray(ResponseInterface $response, bool $expandCompatibility = false): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ public function __construct(
*/
protected function getBaseEndpointUri(): UriInterface
{
return $this->client->getUriFactory()->createUri("/organizations/{$this->organization}/companies/{$this->companyName}/apps/{$this->appName}");
$appName = rawurlencode($this->appName);

return $this->client->getUriFactory()->createUri("/organizations/{$this->organization}/companies/{$this->companyName}/apps/{$appName}");
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Api/Management/Controller/CompanyMembersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ public function setMembers(CompanyMembership $members): CompanyMembership
*/
public function removeMember(string $email): void
{
$this->client->delete($this->getBaseEndpointUri()->withPath("{$this->getBaseEndpointUri()->getPath()}/{$email}"));
$encoded = rawurlencode($email);
$this->client->delete($this->getBaseEndpointUri()->withPath("{$this->getBaseEndpointUri()->getPath()}/{$encoded}"));
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Api/Management/Controller/DeveloperAppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public function __construct(
*/
protected function getBaseEndpointUri(): UriInterface
{
return $this->client->getUriFactory()->createUri("/organizations/{$this->organization}/developers/{$this->developerId}/apps");
$developerId = rawurlencode($this->developerId);

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ public function __construct(
*/
protected function getBaseEndpointUri(): UriInterface
{
return $this->client->getUriFactory()->createUri("/organizations/{$this->organization}/developers/{$this->developerId}/apps/{$this->appName}");
$developerId = rawurlencode($this->developerId);
$appName = rawurlencode($this->appName);

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

/**
Expand Down
15 changes: 15 additions & 0 deletions src/Api/Management/Controller/EnvironmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ public function __construct(
parent::__construct($organization, $client, $entitySerializer);
}

/**
* Override the getEntityIds() method, for Hybrid compatibility.
*
* Environments are not entities, so Hybrid does not support the "expand=false" query parameter.
*
* @inheritdoc
*/
public function getEntityIds(): array
{
$uri = $this->getBaseEndpointUri();
$response = $this->getClient()->get($uri);

return $this->responseToArray($response);
}

/**
* @inheritdoc
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Api/Monetization/Controller/ApiPackageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function __construct(string $organization, ClientInterface $client, ?Enti
*/
public function deleteProduct(string $apiPackageId, string $productId): void
{
$productId = rawurlencode($productId);
$this->getClient()->delete(
$this->getBaseEndpointUri()->withPath("{$this->getBaseEndpointUri()}/{$apiPackageId}/products/{$productId}")
);
Expand All @@ -64,6 +65,7 @@ public function deleteProduct(string $apiPackageId, string $productId): void
*/
public function addProduct(string $apiPackageId, string $productId): void
{
$productId = rawurlencode($productId);
$this->getClient()->post(
$this->getBaseEndpointUri()->withPath(
"{$this->getBaseEndpointUri()}/{$apiPackageId}/products/{$productId}"
Expand Down Expand Up @@ -116,6 +118,8 @@ protected function getBaseEndpointUri(): UriInterface

private function getAvailableApiPackages(string $type, string $id, bool $active = false, bool $allAvailable = true): array
{
$id = rawurlencode($id);

return $this->listEntities($this->client->getUriFactory()->createUri("/mint/organizations/{$this->organization}/{$type}/{$id}/monetization-packages")->withQuery(http_build_query([
'current' => $active ? 'true' : 'false',
'allAvailable' => $allAvailable ? 'true' : 'false',
Expand Down
1 change: 1 addition & 0 deletions src/Api/Monetization/Controller/ApiProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ protected function getBaseEndpointUri(): UriInterface
*/
private function getEligibleProducts(string $type, string $entityId): array
{
$entityId = rawurlencode($entityId);
$products = [];
foreach ($this->getRawList($this->client->getUriFactory()->createUri("/mint/organizations/{$this->organization}/{$type}/{$entityId}/eligible-products")) as $item) {
/** @var \Apigee\Edge\Api\Monetization\Entity\ApiProductInterface $product */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ protected function getBaseEndpointUri(): UriInterface
*/
protected function getActiveRatePlanForApiProductEndpoint(string $apiProductName): UriInterface
{
$apiProductName = rawurlencode($apiProductName);

return $this->client->getUriFactory()->createUri("/mint/organizations/{$this->organization}/companies/{$this->companyName}/products/{$apiProductName}/rate-plan-by-developer-product");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ public function __construct(string $developerId, string $organization, ClientInt
protected function getBaseEndpointUri(): UriInterface
{
// For these API endpoint:
$developerId = rawurlencode($this->developer);
// https://apidocs.apigee.com/monetize/apis/post/organizations/%7Borg_name%7D/developers/%7Bdeveloper_or_company_id%7D/developer-rateplans (create)
// https://apidocs.apigee.com/monetize/apis/put/organizations/%7Borg_name%7D/developers/%7Bdeveloper_id%7D/developer-rateplans/%7Bplan_id%7D (update)
// https://apidocs.apigee.com/monetize/apis/put/organizations/%7Borg_name%7D/developers/%7Bdeveloper_id%7D/developer-rateplans/%7Bplan_id%7D (load)
return $this->client->getUriFactory()->createUri("/mint/organizations/{$this->organization}/developers/{$this->developer}/developer-rateplans");
return $this->client->getUriFactory()->createUri("/mint/organizations/{$this->organization}/developers/{$developerId}/developer-rateplans");
}

/**
Expand All @@ -84,9 +85,10 @@ protected function buildContextForEntityTransformerInCreate(): array
*/
protected function getAcceptedRatePlansEndpoint(): UriInterface
{
$developerId = rawurlencode($this->developer);
// For this API endpoint:
// https://apidocs.apigee.com/monetize/apis/get/organizations/%7Borg_name%7D/developers/%7Bdeveloper_id%7D/developer-accepted-rateplans
return $this->client->getUriFactory()->createUri("/mint/organizations/{$this->organization}/developers/{$this->developer}/developer-accepted-rateplans");
return $this->client->getUriFactory()->createUri("/mint/organizations/{$this->organization}/developers/{$developerId}/developer-accepted-rateplans");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,19 @@ public function __construct(string $developerId, string $organization, ClientInt
*/
protected function getBaseEndpointUri(): UriInterface
{
return $this->client->getUriFactory()->createUri("/mint/organizations/{$this->organization}/developers/{$this->developerId}/developer-rateplans");
$developerId = rawurlencode($this->developerId);

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

/**
* @inheritDoc
*/
protected function getActiveRatePlanForApiProductEndpoint(string $apiProductName): UriInterface
{
return $this->client->getUriFactory()->createUri("/mint/organizations/{$this->organization}/developers/{$this->developerId}/products/{$apiProductName}/rate-plan-by-developer-product");
$developerId = rawurlencode($this->developerId);
$apiProductName = rawurlencode($apiProductName);

return $this->client->getUriFactory()->createUri("/mint/organizations/{$this->organization}/developers/{$developerId}/products/{$apiProductName}/rate-plan-by-developer-product");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,18 @@ public function __construct(string $developerId, string $organization, ClientInt
*/
protected function getBaseEndpointUri(): UriInterface
{
return $this->client->getUriFactory()->createUri("/mint/organizations/{$this->organization}/developers/{$this->developerId}/developer-balances");
$developerId = rawurlencode($this->developerId);

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

/**
* @inheritdoc
*/
protected function getPrepaidBalanceEndpoint(): UriInterface
{
return $this->client->getUriFactory()->createUri("/mint/organizations/{$this->organization}/developers/{$this->developerId}/prepaid-developer-balance");
$developerId = rawurlencode($this->developerId);

return $this->client->getUriFactory()->createUri("/mint/organizations/{$this->organization}/developers/{$developerId}/prepaid-developer-balance");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public function __construct(string $developerId, string $organization, ClientInt
*/
protected function getBaseEndpointUri(): UriInterface
{
return $this->client->getUriFactory()->createUri("/mint/organizations/{$this->organization}/developers/{$this->developerId}/report-definitions");
$developerId = rawurlencode($this->developerId);

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,18 @@ public function __construct(string $developerId, string $organization, ClientInt
*/
protected function getBaseEndpointUri(): UriInterface
{
return $this->client->getUriFactory()->createUri("/mint/organizations/{$this->organization}/developers/{$this->developerId}/developer-tncs");
$developerId = rawurlencode($this->developerId);

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

/**
* @inheritdoc
*/
protected function getAcceptTermsAndConditionsEndpoint(string $tncId): UriInterface
{
return $this->client->getUriFactory()->createUri("/mint/organizations/{$this->organization}/developers/{$this->developerId}/tncs/{$tncId}/developer-tncs");
$developerId = rawurlencode($this->developerId);

return $this->client->getUriFactory()->createUri("/mint/organizations/{$this->organization}/developers/{$developerId}/tncs/{$tncId}/developer-tncs");
}
}
2 changes: 1 addition & 1 deletion src/Api/Monetization/Controller/ListingHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected function getRawList(UriInterface $uri): array
return reset($responseArray);
}

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

abstract protected function responseArrayToArrayOfEntities(array $responseArray, string $keyGetter = 'id'): array;
}
8 changes: 4 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use Apigee\Edge\Exception\ApiResponseException;
use Apigee\Edge\Exception\OauthAuthenticationException;
use Apigee\Edge\HttpClient\Plugin\AddPathPlugin;
use Apigee\Edge\HttpClient\Plugin\Authentication\Oauth;
use Apigee\Edge\HttpClient\Plugin\Authentication\AbstractOauth;
use Apigee\Edge\HttpClient\Plugin\ResponseHandlerPlugin;
use Apigee\Edge\HttpClient\Plugin\RetryOauthAuthenticationPlugin;
use Apigee\Edge\HttpClient\Utility\Builder;
Expand Down Expand Up @@ -208,7 +208,7 @@ public function head($uri, array $headers = []): ResponseInterface
public function post($uri, $body = null, array $headers = []): ResponseInterface
{
if (!isset($headers['Content-Type'])) {
$headers['Content-Type'] = 'application/json; charset=utf-8';
$headers['Content-Type'] = 'application/json';
}

return $this->send('POST', $uri, $headers, $body);
Expand All @@ -220,7 +220,7 @@ public function post($uri, $body = null, array $headers = []): ResponseInterface
public function put($uri, $body = null, array $headers = []): ResponseInterface
{
if (!isset($headers['Content-Type'])) {
$headers['Content-Type'] = 'application/json; charset=utf-8';
$headers['Content-Type'] = 'application/json';
}

return $this->send('PUT', $uri, $headers, $body);
Expand Down Expand Up @@ -333,7 +333,7 @@ protected function getDefaultPlugins(): array
$middlePlugins[] = new RetryPlugin($this->retryPluginConfig);
}

if ($this->authentication instanceof Oauth) {
if ($this->authentication instanceof AbstractOauth) {
$middlePlugins[] = new RetryOauthAuthenticationPlugin($this->authentication);
}

Expand Down
17 changes: 17 additions & 0 deletions src/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,25 @@
*/
interface ClientInterface extends HttpClient
{
/**
* Default endpoint for Apigee Edge Public Cloud.
*
* @var string
*/
public const DEFAULT_ENDPOINT = 'https://api.enterprise.apigee.com/v1';

/**
* Default endpoint for Apigee Edge Hybrid Cloud.
*
* @var string
*/
public const HYBRID_ENDPOINT = 'https://apigee.googleapis.com/v1';

/**
* Library version.
*
* @var string
*/
public const VERSION = '2.0.3';

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Controller/AbstractEntityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public function __construct(ClientInterface $client, ?EntitySerializerInterface
*/
protected function getEntityEndpointUri(string $entityId): UriInterface
{
return $this->getBaseEndpointUri()->withPath("{$this->getBaseEndpointUri()}/{$entityId}");
$encoded = rawurlencode($entityId);

return $this->getBaseEndpointUri()->withPath("{$this->getBaseEndpointUri()}/{$encoded}");
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/Controller/NonPaginatedEntityIdListingControllerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace Apigee\Edge\Controller;

use Apigee\Edge\ClientInterface;
use Psr\Http\Message\ResponseInterface;

/**
Expand All @@ -40,12 +41,13 @@ public function getEntityIds(): array
];
$uri = $this->getBaseEndpointUri()->withQuery(http_build_query($query_params));
$response = $this->getClient()->get($uri);
$expandCompatibility = (ClientInterface::HYBRID_ENDPOINT === $this->getClient()->getEndpoint());

return $this->responseToArray($response);
return $this->responseToArray($response, $expandCompatibility);
}

/**
* @inheritdoc
*/
abstract protected function responseToArray(ResponseInterface $response): array;
abstract protected function responseToArray(ResponseInterface $response, bool $expandCompatibility = false): array;
}
Loading

0 comments on commit 5a650bf

Please sign in to comment.