Giter Club home page Giter Club logo

vultr-php's Introduction

Vultr API PHP Client.

GitHub license Vultr-Php Changelog PHP Version Require Latest Stable Version Latest Unstable Version Total Downloads PHP Tests Test Coverage Library Documentation

Getting Started

Must have a PSR7, PSR17, and PSR18 Compatible HTTP Client. View all PSR's This client will act on those interfaces which allow for dependancy injection. See Usage for more info. https://packagist.org/providers/psr/http-client-implementation

Installation

composer require vultr/vultr-php

Usage

Initializing the client

Once decided on what HTTP client implementation that will be used to initiate the client. If the client implementation you chose is a wider used client, it may be possible to be auto detected. This is because this client uses PHP-Http/Discovery.

<?php

declare(strict_types=1);

require (__DIR__.'/../vendor/autoload.php');

$client = Vultr\VultrPhp\VultrClient::create('Your Lovely Vultr API Key');

The above code example would try and initialize the client using the HTTP Discovery method. If you wanna customize your http client, or the Discovery Method does not find it, the VultrClient will allow to pass in the PSR18 client along with a PSR17 HTTP Factory.

<?php

declare(strict_types=1);

require (__DIR__.'/../vendor/autoload.php');

$http_factory = new GuzzleHttp\Psr7\HttpFactory();
$client = Vultr\VultrPhp\VultrClient::create('Heres my api key', new GuzzleHttp\Client(), $http_factory, $http_factory);

Using the client

This client implements all the service endpoints in the current iteration of the version 2 api of vultr. Which can be found here.

For more detailed examples view the examples folder.

Pagination

The client uses a linked list to paginate between your cursors. Each list call returns a ListOptions passed by reference which you can manipulate with each subsequent call and thus the function manipulates it as well. This allows you to choose previous and or next cursor links to navigate.

<?php

declare(strict_types=1);

require(__DIR__.'/../vendor/autoload.php');

$client = Vultr\VultrPhp\VultrClient::create('Your Lovely Vultr API Key');

$options = new Vultr\VultrPhp\Util\ListOptions();
// Or
// $options = null;
/**
 * Whether you pass in a null $options or a ListOptions. You can always expect to have ListOptions be passed back out too you when calling the function.
 */
while (true)
{
	$instances = [];
	foreach ($client->instances->getInstances(null, $options) as $instance)
	{
		$instances[] = $instance;
	}

	// Exit our loop, we have reached the end. Hooray!
	if ($options->getNextCursor() == '')
	{
		break;
	}
	// Setting the "CurrentCursor" will tell the client which page it should transcode the url to make the request too.
	$options->setCurrentCursor($options->getNextCursor());
}

ModelOptions Usage

ModelOptions are objects that allow the user to pass in many arguments that don't neccessarily belong to a Model object. These are attributes that are specific to creation and update functions throughout the client library. Usage of these objects are quite simple. The idea was to reduce code complexity but also give the flexibility to deprecate certain methods when/if attributes are removed from responses.

Lets take InstanceCreate for example. This object has many properties in it, that are all underscore_cased. These property names are than used to generate a request to the api.

To keep the uniformity between the camelCased functions in this client library. ModelOptions makes use of php's __call magic method. In order to set these protected properties you can use variation of with functions example: withYourLovelyPropName('hello_world') or set functions example: setYourLovelyPropName('hello_world').

These functions will set your attributes that will be used to generate the request of our underscored_props that will be sent to the api.

With the addition of with and set type functions. There are also get functions that can be used as well. They follow the same camcelCased layout as the with and set functions.

Example usage of these object functions.

declare(strict_types=1);

require(__DIR__.'/../vendor/autoload.php');

use Vultr\VultrPhp\Services\Instances\InstanceCreate;

$create = new InstanceCreate('ewr', 'vc2-6c-16gb');

$create->setOsId(6969);

$create = $create->withHostname('my-amazing-hostname');

var_dump($create->getOsId(), $create->getHostname());

Exception Usage

All exceptions are children of VultrException.

Exception tree

  • VultrException
    • VultrClientException
    • VultrServiceException
      • AccountException
      • ApplicationException
      • BackupException
      • BareMetalException
      • BillingException
      • BlockStorageException
      • DNSException
      • FirewallException
      • InstanceException
      • ISOException
      • KubernetesException
      • LoadBalancerException
      • ObjectStorageException
      • OperatingSystemException
      • PlanException
      • RegionException
      • ReservedIPException
      • SnapshotException
      • SSHKeyException
      • StartupScriptException
      • UserException
      • VPCException
<?php

declare(strict_types=1);

require (__DIR__.'/../vendor/autoload.php');

$client = Vultr\VultrPhp\VultrClient::create('Your Lovely Vultr API Key');

try
{
	$account = $client->account->getAccount();
}
catch (Vultr\VultrPhp\Services\Account\AccountException $e)
{
	exit('Just a little http error no biggy :wink: : '. $e->getMessage().PHP_EOL);
}
catch (Vultr\VultrPhp\VultrException $e)
{
	exit('O crap something really bad happen: '.$e->getMessage().PHP_EOL);
}

Documentation

See our documentation for detailed information about API v2.

View our code-coverage for a detailed look https://vultr.github.io/vultr-php/code-coverage/index.html

To view the specific library documentation please view https://vultr.github.io/vultr-php/docs/index.html

Versioning

This project follows SemVer for versioning. For the versions available, see the tags on this repository or for stable releases

Contribute

Feel free to send pull requests our way! Please see the contributing guidelines.

License

This project is licensed under the MIT License - see the LICENSE file for details.

vultr-php's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

vultr-php's Issues

FirewallService crashes when creating an anywhere rule

The api crashes with an invalid request error when ever I try to create a firewall rule set to accept from anywhere ( 0.0.0.0/0 ). I brought this up as a side note before that getInitializedProps wont return key pairs when the value evaluates to false. I tested this on the latest version 0.2.3.

[BUG] - missing ListOptions namespace declaration causes library to crash

The namespace declaration for ListOptions is missing from /src/Services/VPC/VPCService.php and /src/Services/Firewall/FirewallService.php files which causes the library to crash when calling list methods.

This can be reproduced by calling $client->firewall->getFirewallGroups and $client->vpc->getVPCs

Is it possible to get php7 support?

I have many reasons for this request.

  1. Stable distributions ( production environments ) haven't fully adopted php8.
  2. The latest Ubuntu release defaults to php7 ( in my case php8 was a headache to get working )
  3. Not all modules have or are php8 friendly, I had to use custom/questionable repos and still failed due to a buggy module.

The only way I can run this library is through a docker image. Perhaps other people are experiencing similar issues.

[BUG] - No way to create a firewall rule with FirewallService

The createFirewallRule function is missing in src/Services/Firewall/FirewallService.php file. After implementing it, I had trouble setting a subnet to 0.0.0.0/0 because getInitializedProps() routine wouldn't return properties with a value of 0.

I ended up writing this to get what I needed working.

  • public function createFirewallRule(string $group_id, FirewallRule $rule) : FirewallRule
  • {
  •  $properties = $rule->getInitializedProps();
    
  •  if ( !isset( $properties['subnet_size'] ) )
    
  •      $properties['subnet_size'] = 0;
    
  •  return $this->createObject('firewalls/'.$group_id.'/rules', new FirewallRule(), $properties);
    
  • }

Refactor Exception Usage

Is your feature request related to a problem? Please describe.
The need to alleviate long exception messages when, I can just view previous exceptions for more additional information if needed.

Describe the solution you'd like
To stop the chaining of try/catches in many functions throughout the service handlers

Example:
image

[Feature] - Add version numbering to library

I'm using this library to automate my vultr projects but there's no version number association so I'm unable to isolate a specific revision with composer. Also, because I did some bug-fixes/touch-ups, I wanted to create a patch file for the current revision. When do you think we can see a version 0.0.1 or 1.0.0?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.