Skip to content

Commit

Permalink
Added support for object-list of nonpaginated endpoint and added test…
Browse files Browse the repository at this point in the history
… case for pending charges
  • Loading branch information
bashar94 committed Nov 20, 2024
1 parent fee8294 commit dfeaead
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/Services/Billing/BillingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Vultr\VultrPhp\Services\Billing;

use Vultr\VultrPhp\Services\VultrService;
use Vultr\VultrPhp\Services\VultrServiceException;
use Vultr\VultrPhp\Util\ListOptions;

/**
Expand Down Expand Up @@ -67,15 +68,16 @@ public function getInvoiceItems(int $invoice_id, ?ListOptions &$options = null)
return $this->getListObjects("billing/invoices/{$invoice_id}/items", new InvoiceItem(), $options);
}

/**
* Retrieve the pending charges of all the service types of the account.
*
* @see https://www.vultr.com/api/#operation/pending-charges
* @throws BillingException
* @return PendingCharge[]
*/
public function getPendingCharges() : array
{
return $this->getListObjects('billing/pending-charges', new PendingCharge());
}
/**
* Retrieve the pending charges of all the service types of the account.
*
* @see https://www.vultr.com/api/#operation/pending-charges
* @throws BillingException
* @throws VultrServiceException
* @return PendingCharge[]
*/
public function getPendingCharges() : array
{
return $this->getNonPaginatedListObjects('billing/pending-charges', new PendingCharge());
}
}
37 changes: 37 additions & 0 deletions src/Services/VultrService.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,43 @@ protected function getListObjects(string $uri, ModelInterface $model, ?ListOptio
return $objects;
}

/**
* @param $uri - string - the url address to query after api.vultr.com/v2
* @param $model - ModelInterface - the object that will be mapped to the get response.
* @throws Child of VultrServiceObject
* @return ModelInterface[]
*/
protected function getNonPaginatedListObjects(string $uri, ModelInterface $model) : array
{
try
{
$response = $this->getClientHandler()->get($uri);
$stdclass = VultrUtil::decodeJSON((string)$response->getBody());

// Get the list name from the model
$list_name = $model->getResponseListName();

// Ensure the expected list exists
if (!isset($stdclass->$list_name)) {
throw new VultrServiceException("Response does not contain expected list: $list_name");
}

// Map each item in the list to a model instance
$objects = [];
foreach ($stdclass->$list_name as $object) {
$objects[] = VultrUtil::mapObject($object, clone $model);
}

return $objects;
}
catch (Throwable $e)
{
$exception_class = $model->getModelExceptionClass();
throw new $exception_class('Failed to list '.$model->getResponseListName().': '.$e->getMessage(), $e->getHTTPCode(), $e);
}
}


/**
* @param $uri - string - the url address to query after api.vultr.com/v2
* @param $model - ModelInterface - the object model that we are updating. This needs to be a fully initialized object.
Expand Down
29 changes: 29 additions & 0 deletions tests/Data/BillingData.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,33 @@ protected function dataGetInvoices() : array
}
', true);
}
protected function dataGetPendingCharges() : array
{
return json_decode('
{
"pending_charges": [
{
"description": "Load Balancer (my-loadbalancer)",
"start_date": "2020-10-10T01:56:20+00:00",
"end_date": "2020-10-10T01:56:20+00:00",
"units": 720,
"unit_type": "hours",
"unit_price": 0.0149,
"total": 10,
"product": "Load Balancer"
},
{
"description": "65.65.65.65 (1024 MB) [my-instance]",
"start_date": "2024-11-01T00:00:00+00:00",
"end_date": "2024-11-19T11:20:52+00:00",
"units": 444,
"unit_type": "hours",
"unit_price": 0.00744047619047619,
"total": 3.31,
"product": "Vultr Cloud Compute"
}
]
}
', true);
}
}
29 changes: 29 additions & 0 deletions tests/Suite/BillingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Vultr\VultrPhp\Services\Billing\BillingException;
use Vultr\VultrPhp\Services\Billing\Invoice;
use Vultr\VultrPhp\Services\Billing\InvoiceItem;
use Vultr\VultrPhp\Services\Billing\PendingCharge;
use Vultr\VultrPhp\Tests\VultrTest;

class BillingTest extends VultrTest
Expand Down Expand Up @@ -88,4 +89,32 @@ public function testGetInvoiceItems()
$this->expectException(BillingException::class);
$client->billing->getInvoiceItems(123456);
}

public function testGetPendingCharges()
{
$data = $this->getDataProvider()->getData();

$client = $this->getDataProvider()->createClientHandler([
new Response(200, ['Content-Type' => 'application/json'], json_encode($data)),
new Response(400, [], json_encode(['error' => 'Bad Request'])),
]);

foreach ($client->billing->getPendingCharges() as $pendingCharge)
{
$this->assertInstanceOf(PendingCharge::class, $pendingCharge);
foreach ($data[$pendingCharge->getResponseListName()] as $expectedCharge)
{
if ($expectedCharge['unit_price'] !== $pendingCharge->getUnitPrice()) continue;

foreach ($pendingCharge->toArray() as $prop => $prop_val)
{
$this->assertEquals($prop_val, $expectedCharge[$prop], "Prop {$prop} failed");
}
break;
}
}

$this->expectException(BillingException::class);
$client->billing->getPendingCharges();
}
}

0 comments on commit dfeaead

Please sign in to comment.