Skip to content
Merged
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
37 changes: 37 additions & 0 deletions src/Api/Management/Controller/AppByOwnerController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* 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.
*/

namespace Apigee\Edge\Api\Management\Controller;

use Apigee\Edge\Controller\CpsLimitEntityController;

/**
* Common parent class for company- and developer app controllers.
*/
abstract class AppByOwnerController extends CpsLimitEntityController implements AppByOwnerControllerInterface
{
/**
* String that should be sent to the API to change the status of an app to approved.
*/
public const STATUS_APPROVE = 'approve';

/**
* String that should be sent to the API to change the status of an app to revoked.
*/
public const STATUS_REVOKE = 'revoke';
}
38 changes: 38 additions & 0 deletions src/Api/Management/Controller/AppByOwnerControllerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* 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.
*/

namespace Apigee\Edge\Api\Management\Controller;

use Apigee\Edge\Controller\CpsLimitEntityControllerInterface;
use Apigee\Edge\Controller\CpsListingEntityControllerInterface;
use Apigee\Edge\Controller\EntityControllerInterface;
use Apigee\Edge\Controller\EntityCrudOperationsControllerInterface;
use Apigee\Edge\Controller\StatusAwareEntityControllerInterface;

/**
* Describes common operations for company- and developer apps.
*/
interface AppByOwnerControllerInterface extends
AttributesAwareEntityControllerInterface,
EntityControllerInterface,
EntityCrudOperationsControllerInterface,
CpsLimitEntityControllerInterface,
CpsListingEntityControllerInterface,
StatusAwareEntityControllerInterface
{
}
202 changes: 202 additions & 0 deletions src/Api/Management/Controller/AppCredentialController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
<?php

/*
* 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.
*/

namespace Apigee\Edge\Api\Management\Controller;

use Apigee\Edge\Api\Management\Entity\AppCredential;
use Apigee\Edge\Api\Management\Entity\AppCredentialInterface;
use Apigee\Edge\Api\Management\Normalizer\AppCredentialNormalizer;
use Apigee\Edge\Controller\EntityController;
use Apigee\Edge\Controller\EntityCrudOperationsControllerTrait;
use Apigee\Edge\Controller\StatusAwareEntityControllerTrait;
use Apigee\Edge\Denormalizer\AttributesPropertyDenormalizer;
use Apigee\Edge\Denormalizer\CredentialProductDenormalizer;
use Apigee\Edge\Entity\EntityInterface;
use Apigee\Edge\Normalizer\CredentialProductNormalizer;
use Apigee\Edge\Structure\AttributesProperty;

/**
* Common implementation for company- and developer app credential controllers.
*/
abstract class AppCredentialController extends EntityController implements AppCredentialControllerInterface
{
use EntityCrudOperationsControllerTrait {
// These methods are not supported on this endpoint in the same way as on the others so do not allow to
// use them here.
create as private privateCreate;
update as private privateUpdate;
}
use StatusAwareEntityControllerTrait;

/**
* String that should be sent to the API to change the status of a credential to approved.
*/
public const STATUS_APPROVE = 'approve';

/**
* String that should be sent to the API to change the status of a credential to revoked.
*/
public const STATUS_REVOKE = 'revoke';

/** @var string Developer email or id. */
protected $companyName;

/** @var string App name. */
protected $appName;

public function __construct(string $organization, string $appName, \Apigee\Edge\ClientInterface $client, $entityNormalizers = [])
{
$this->appName = $appName;
$entityNormalizers[] = new AppCredentialNormalizer();
$entityNormalizers[] = new CredentialProductDenormalizer();
$entityNormalizers[] = new CredentialProductNormalizer();
$entityNormalizers[] = new AttributesPropertyDenormalizer();
parent::__construct($organization, $client, $entityNormalizers);
}

/**
* @inheritdoc
*/
public function create(string $consumerKey, string $consumerSecret): AppCredentialInterface
{
$response = $this->client->post(
// Just to spare some extra lines of code.
$this->getEntityEndpointUri('create'),
(string) json_encode((object) ['consumerKey' => $consumerKey, 'consumerSecret' => $consumerSecret])
);

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

/**
* @inheritdoc
*/
public function generate(
array $apiProducts,
array $scopes = [],
string $keyExpiresIn = '-1'
): AppCredentialInterface {
$response = $this->client->post(
$this->getBaseEndpointUri(),
(string) json_encode((object) [
'apiProducts' => $apiProducts,
'scopes' => $scopes,
'keyExpiresIn' => $keyExpiresIn,
])
);
// It returns a complete developer app entity, but we only returns the newly created credential for the
// sake of consistency.
$responseArray = $this->responseToArray($response);
$credentialArray = reset($responseArray['credentials']);

return $this->entityTransformer->denormalize(
$credentialArray,
$this->getEntityClass()
);
}

/**
* @inheritdoc
*/
public function addProducts(string $consumerKey, array $apiProducts): AppCredentialInterface
{
$response = $this->client->post(
$this->getEntityEndpointUri($consumerKey),
(string) json_encode((object) ['apiProducts' => $apiProducts])
);

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

/**
* @inheritdoc
*/
public function overrideAttributes(string $consumerKey, AttributesProperty $attributes): AppCredentialInterface
{
$response = $this->client->post(
$this->getEntityEndpointUri($consumerKey),
(string) json_encode((object) ['attributes' => $this->entityTransformer->normalize($attributes)])
);

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

/**
* @inheritdoc
*/
public function setApiProductStatus(string $consumerKey, string $apiProduct, string $status): void
{
$uri = $this->getBaseEndpointUri()
->withPath(sprintf('%s/keys/%s/apiproducts/%s', $this->getBaseEndpointUri(), $consumerKey, $apiProduct))
->withQuery(http_build_query(['action' => $status]));
$this->client->post($uri, null, ['Content-Type' => 'application/octet-stream']);
}

/**
* @inheritdoc
*/
public function deleteApiProduct(string $consumerKey, string $apiProduct): EntityInterface
{
$uri = $this->getBaseEndpointUri()
->withPath(sprintf('%s/keys/%s/apiproducts/%s', $this->getBaseEndpointUri(), $consumerKey, $apiProduct));
$response = $this->client->delete($uri);

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

/**
* @inheritdoc
*/
public function overrideScopes(string $consumerKey, array $scopes): AppCredentialInterface
{
$response = $this->client->put(
$this->getEntityEndpointUri($consumerKey),
(string) json_encode((object) ['scopes' => $scopes])
);

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

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