Skip to content

Commit

Permalink
Updating the generator to use a more advanced configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Mar 1, 2018
1 parent 8953133 commit 7be9041
Show file tree
Hide file tree
Showing 28 changed files with 1,130 additions and 518 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
language: php

dist: trusty

sudo: false

php:
- 7.0
- 7.1
- 7.2

matrix:
include:
- php: 7.0
- php: 7.1
env:
- BUILD_PHAR=true

Expand All @@ -23,4 +23,4 @@ deploy:
skip_cleanup: true
on:
tags: true
php: '7.0'
php: '7.1'
6 changes: 2 additions & 4 deletions bin/php-cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env php
<?php

if (PHP_SAPI !== 'cli') {
Expand All @@ -11,11 +12,8 @@ if (!(bool)getenv('PHPCS_PHAR')) {
}

use MyENA\CloudStackClientGenerator\Application;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Output\ConsoleOutput;

error_reporting(-1);

$application = new Application('PHP CloudStack', '3.0.0');
$application = new Application('PHP CloudStack', '5.0.0');
$application->run();
6 changes: 1 addition & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"email": "[email protected]"
}
],

"autoload": {
"psr-4": {
"MyENA\\CloudStackClientGenerator\\": "src/"
Expand All @@ -34,7 +33,6 @@
"files/constants.php"
]
},

"require": {
"php": "^7.0",
"symfony/console": "@stable",
Expand All @@ -46,10 +44,8 @@
"guzzlehttp/guzzle": "@stable"
},
"require-dev": {
"myena/default-logger": "@stable",
"phpunit/phpunit": "@stable"
"myena/default-logger": "@stable"
},

"scripts": {
"build": [
"rm -rf vendor",
Expand Down
47 changes: 32 additions & 15 deletions files/config_prototype.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,35 @@

# php_cs_generator must be the root key
php_cs_generator:
dev: # Name of this config. Accessible via the "config-env" option
scheme: "http"
host: ~ # dev.ourcloudstack.com
port: 8080
apipath: "client/api"
consolepath: "client/console"
out: ~ # choose output path
namespace: "\\MyGreatNamespace"
key: ~ # your api key
secret: ~ # your api secret
prod:
host: prod.ourcloudstack.com
port: 8765
key: ~ # your api key
secret: ~ # your api secret
# environment configuration
environments:
dev: # Name of this config. Accessible via the "env" option
scheme: "http"
host: ~ # dev.ourcloudstack.com
port: 8080
apipath: "client/api"
consolepath: "client/console"
out: ~ # choose output path
namespace: "\\MyGreatNamespace"
key: ~ # your api key
secret: ~ # your api secret
logger:
class: ~ # psr-3 compliant logger instance
level: ~ #level the environment's logger should be
http_client:
class: ~ # any class that implements \GuzzleHttp\ClientInterface
config:
allow_redirects: true
http_errors: true
verify: false
prod:
host: prod.ourcloudstack.com
port: 8765
key: ~ # your api key
secret: ~ # your api secret
# allows you to extend the classes constructed by this lib with your own class
overloaded_classes:
- name: Tags # name of class you are overloading
overload: MyGreatTagsClass # fully qualified name of class extending the base class
- name: ListVirtualMachinesRequest
overload: DoSomethingCrazyRequest
23 changes: 13 additions & 10 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,53 @@
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Uri;
use GuzzleHttp\RequestOptions;
use MyENA\CloudStackClientGenerator\Configuration\Environment;

/**
* Class Client
* @package MyENA\CloudStackClientGenerator
*/
class Client {
/** @var \MyENA\CloudStackClientGenerator\Configuration */
protected $config;
/** @var \MyENA\CloudStackClientGenerator\Configuration\Environment */
protected $env;

/**
* Client constructor.
* @param \MyENA\CloudStackClientGenerator\Configuration $c
* @param \MyENA\CloudStackClientGenerator\Configuration\Environment $e
*/
public function __construct(Configuration $c) {
$this->config = $c;
public function __construct(Environment $e) {
$this->env = $e;
}

/**
* @param string $command
* @param array $parameters
* @param array $headers
* @return \stdClass
* @throws \Exception
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function do(string $command, array $parameters = [], array $headers = []): \stdClass {
static $defaultHeaders =
['Accept' => ['application/json'], 'Content-Type' => ['application/x-www-form-urlencoded']];

$params = ['apikey' => $this->config->getKey(), 'command' => $command, 'response' => 'json'] + $parameters;
$params = ['apikey' => $this->env->getKey(), 'command' => $command, 'response' => 'json'] + $parameters;

ksort($params);

$query = http_build_query($params, '', '&', PHP_QUERY_RFC3986);

$uri = new Uri(sprintf(
'%s/%s?%s&signature=%s',
$this->config->getCompiledAddress(),
$this->config->getApiPath(),
$this->env->getCompiledAddress(),
$this->env->getApiPath(),
$query,
$this->config->buildSignature($query)
$this->env->buildSignature($query)
));

$r = new Request('GET', $uri, $headers + $defaultHeaders);

$resp = $this->config->HttpClient->send($r, [
$resp = $this->env->getHttpClient()->send($r, [
RequestOptions::HTTP_ERRORS => false,
RequestOptions::DECODE_CONTENT => false,
]);
Expand Down
Loading

0 comments on commit 7be9041

Please sign in to comment.