Skip to content
Closed
27 changes: 27 additions & 0 deletions src/Model/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,33 @@ public static function get($id, $collectionUrl = null, ClientInterface $client)
}
}

/**
* Post request for returned data.
*
* @param string $body array of the data to send in the post.
* @param string $collectionUrl The URL of the collection.
* @param ClientInterface $client A suitably configured Guzzle
* client.
*
* @return array The resource object, or false if the resource is
* not found.
*/
public static function post(array $body, $collectionUrl = null, ClientInterface $client)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is mimicking get() but I think you want to mimic getCollection(), as it's a list of results you're after

you could add a parameter to getCollection to override the request method, getCollection( ... , $method = 'get')

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would getCollection work? Given that is also processes the returned data with wrapCollection.

I thought we might need to implement more posts in future if we use the APIs more so it seems reasonable to create a new post method. If you don't think that's the case we can alter an existing method.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It takes a $collectionUrl so it seems like you're trying to fetch a collection; list of things. But it's ignoring 404s, which isn't appropriate for a POST nor for a collection.

{
try {
$request = $client->createRequest('post', $collectionUrl, ['json' => $body]);
$data = self::send($request, $client);
return $data;
} catch (BadResponseException $e) {
$response = $e->getResponse();
if ($response && $response->getStatusCode() === 404) {
return false;
}
throw $e;
}
}


/**
* Create a resource.
*
Expand Down
12 changes: 12 additions & 0 deletions src/Model/SetupOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Platformsh\Client\Model;

/**
* Represents Platform.sh setup options return data.
*
*/
class SetupOptions extends Resource
Copy link
Collaborator

@pjcdawkins pjcdawkins Sep 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SetupOptions is an odd "resource" name, it just generally doesn't fit the RESTful "resource"/"collection" model because it isn't a thing that I can fetch with a GET, create with a POST (containing its properties), and update with a PUT/PATCH. I wonder if it just shouldn't use Resource at all. As in we might want a SetupOptions class that wraps the API data with appropriate methods and properties.

The other related problem here is that the class doesn't have/document any properties. Other Resource classes use these @property-read annotations because you can fetch from properties via __get(), so you can get a list of the properties within your object (if you have an IDE or have read the docblock). But the data here is a list so it doesn't have properties...

... so tl;dr I think extending Resource is getting in the way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still learning about the API but I'm wondering if there are other API endpoints we're going to need to use in future that behave in the same way? If that's the case we probably shouldn't call it SetupOptions.

{

}
27 changes: 27 additions & 0 deletions src/PlatformClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Platformsh\Client\Model\Project;
use Platformsh\Client\Model\Region;
use Platformsh\Client\Model\Result;
use Platformsh\Client\Model\SetupOptions;
use Platformsh\Client\Model\SshKey;
use Platformsh\Client\Model\Subscription;

Expand Down Expand Up @@ -333,4 +334,30 @@ public function getRegions()
{
return Region::getCollection($this->accountsEndpoint . 'regions', 0, [], $this->getConnector()->getClient());
}

/**
* Get the setup options file for a user.
*
* @param string $vendor The query string containing the vendor machine name.
* @param string $plan The machine name of the plan which has been selected during the project setup process.
* @param string $options_url The URL of a project options file which has been selected as a setup template.
* @param string $username The name of the account for which the project is to be created.
* @param string $organization The name of the organization for which the project is to be created.
*
*
* @return SetupOptions[]
*/
public function getSetupOptions($vendor = NULL, $plan = NULL, $options_url = NULL, $username = NULL, $organization = NULL)
{
$url = $this->accountsEndpoint . 'setup/options';
$options = $this->cleanRequest([
'vendor' => $vendor,
'plan' => $plan,
'options_url' => $options_url,
'username' => $username,
'organization' => $organization
]);

return SetupOptions::post($options, $url, $this->connector->getClient());
}
}