Source of file Client.php
Size: 18,215 Bytes - Last Modified: 2016-08-28T19:38:31+00:00
/Users/dshafik/src/akamai-open/edgegrid-auth-php/src/Client.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
Covered by 66 test(s):
114
Covered by 66 test(s):
115
Covered by 66 test(s):
116
Covered by 66 test(s):
117118
Covered by 66 test(s):
119
Covered by 66 test(s):
120121122123124125126127128129130131132
Covered by 45 test(s):
133134
Covered by 45 test(s):
135
Covered by 45 test(s):
136137138139140
Covered by 45 test(s):
141142143144145
Covered by 14 test(s):
146147
Covered by 14 test(s):
148149150151152153154155156157158159160161
Covered by 27 test(s):
162163
Covered by 27 test(s):
164165166167168169170171172173174175176177
Covered by 14 test(s):
178179
Covered by 14 test(s):
180181182183184185186187188189190
Covered by 14 test(s):
191192
Covered by 14 test(s):
193194195196197198199200201202203
Covered by 1 test(s):
204
Covered by 1 test(s):
205206207
Covered by 1 test(s):
208
Covered by 1 test(s):
209
Covered by 1 test(s):
210211
Covered by 1 test(s):
212
Covered by 1 test(s):
213214
Covered by 1 test(s):
215216
Covered by 1 test(s):
217218219220221222223224225226227
Covered by 1 test(s):
228229
Covered by 1 test(s):
230231232233234235236237238239240
Covered by 4 test(s):
241
Covered by 4 test(s):
242
Covered by 4 test(s):
243244245246247248249250251252253
Covered by 6 test(s):
254
Covered by 6 test(s):
255
Covered by 6 test(s):
256257258259260261262263264265266267268269
Covered by 11 test(s):
270
Covered by 1 test(s):
271
Covered by 1 test(s):
272
Covered by 1 test(s):
273274275
Covered by 11 test(s):
276277
Covered by 11 test(s):
278
Covered by 11 test(s):
279280
Covered by 11 test(s):
281
Covered by 11 test(s):
282283
Covered by 11 test(s):
284285286287288289290291292293294
Covered by 1 test(s):
295296297298
Covered by 1 test(s):
299
Covered by 1 test(s):
300
Covered by 1 test(s):
301302
Covered by 1 test(s):
303304305306307308309310311312313314315316317318
Covered by 4 test(s):
319320
Covered by 4 test(s):
321
Covered by 4 test(s):
322323324
Covered by 4 test(s):
325
Covered by 4 test(s):
326327328329330331332333334335
Covered by 74 test(s):
336
Covered by 74 test(s):
337338339340341342343344345
Covered by 82 test(s):
346
Covered by 82 test(s):
347348349350351352353354355
Covered by 59 test(s):
356
Covered by 1 test(s):
357358359
Covered by 58 test(s):
360
Covered by 4 test(s):
361
Covered by 54 test(s):
362
Covered by 8 test(s):
363364365
Covered by 46 test(s):
366367368369370371372373374375
Covered by 59 test(s):
376
Covered by 47 test(s):
377378379
Covered by 13 test(s):
380
Covered by 4 test(s):
381382383
Covered by 9 test(s):
384385386387388389390391392393
Covered by 59 test(s):
394
Covered by 45 test(s):
395396397
Covered by 14 test(s):
398
Covered by 2 test(s):
399400401
Covered by 12 test(s):
402403404405406407408409410411
Covered by 66 test(s):
412
Covered by 66 test(s):
413
Covered by 62 test(s):
414415416
Covered by 66 test(s):
417
Covered by 14 test(s):
418419420
Covered by 66 test(s):
421
Covered by 14 test(s):
422423
Covered by 66 test(s):
424425426427428429430431432433434
Covered by 66 test(s):
435436
Covered by 66 test(s):
437
Covered by 66 test(s):
438
Covered by 66 test(s):
439
Covered by 9 test(s):
440441442
Covered by 66 test(s):
443444445
Covered by 66 test(s):
446
Covered by 9 test(s):
447448
Covered by 9 test(s):
449450
Covered by 66 test(s):
451452453454455456457458459
Covered by 66 test(s):
460
Covered by 65 test(s):
461462463
Covered by 66 test(s):
464
Covered by 1 test(s):
465
Covered by 1 test(s):
466467
Covered by 65 test(s):
468469470471472473474475476477478479480481482483484485
Covered by 2 test(s):
486
Covered by 2 test(s):
487488
Covered by 2 test(s):
489
Covered by 2 test(s):
490
Covered by 2 test(s):
491492493494495496497498499500501502
Covered by 13 test(s):
503
Covered by 3 test(s):
504505506
Covered by 13 test(s):
507508509
Covered by 13 test(s):
510
Covered by 1 test(s):
511512513
Covered by 13 test(s):
514515516517
Covered by 13 test(s):
518519520521
Covered by 13 test(s):
522
Covered by 13 test(s):
523524525
Covered by 13 test(s):
526527528529530
Covered by 13 test(s):
531532
Covered by 13 test(s):
533534535536537538539540541542543544
Covered by 11 test(s):
545
Covered by 2 test(s):
546547
Covered by 2 test(s):
548549550551552553
Covered by 11 test(s):
554555556557558559560561562563564565566
Covered by 14 test(s):
567
Covered by 11 test(s):
568
Covered by 3 test(s):
569
Covered by 1 test(s):
570571572
Covered by 14 test(s):
573574575
Covered by 14 test(s):
576
Covered by 3 test(s):
577578579
Covered by 14 test(s):
580
Covered by 1 test(s):
581582583
Covered by 14 test(s):
584585586587
Covered by 14 test(s):
588
Covered by 14 test(s):
589590591
Covered by 14 test(s):
592593594595596
Covered by 14 test(s):
597598
Covered by 14 test(s):
599600601602603604605606607
Covered by 59 test(s):
608609
Covered by 59 test(s):
610
Covered by 45 test(s):
611612613
Covered by 59 test(s):
614615616617
Covered by 59 test(s):
618
Covered by 2 test(s):
619620621
Covered by 59 test(s):
622
Covered by 14 test(s):
623624625
Covered by 59 test(s):
626
Covered by 59 test(s):
627
Covered by 13 test(s):
628629630
Covered by 59 test(s):
631
Covered by 1 test(s):
632
Covered by 1 test(s):
633634635
Covered by 58 test(s):
636637638
| <?php /** * Akamai {OPEN} EdgeGrid Auth for PHP * * Akamai\Open\EdgeGrid\Client wraps GuzzleHttp\Client * providing request authentication/signing for Akamai * {OPEN} APIs. * * This client works _identically_ to GuzzleHttp\Client * * However, if you try to call an Akamai {OPEN} API you *must* * first call {@see Akamai\Open\EdgeGrid\Client->setAuth()}. * * @author Davey Shafik <dshafik@akamai.com> * @copyright Copyright 2015 Akamai Technologies, Inc. All rights reserved. * @license Apache 2.0 * @link https://github.com/akamai-open/edgegrid-auth-php * @link https://developer.akamai.com * @link https://developer.akamai.com/introduction/Client_Auth.html */ namespace Akamai\Open\EdgeGrid; use Akamai\Open\EdgeGrid\Authentication; use Akamai\Open\EdgeGrid\Handler\Authentication as AuthenticationHandler; use Akamai\Open\EdgeGrid\Handler\Debug as DebugHandler; use Akamai\Open\EdgeGrid\Handler\Verbose as VerboseHandler; /** * Akamai {OPEN} EdgeGrid Client for PHP * * Akamai {OPEN} EdgeGrid Client for PHP. Based on * [Guzzle](http://guzzlephp.org). * * @package Akamai {OPEN} EdgeGrid Client */ class Client extends \GuzzleHttp\Client implements \Psr\Log\LoggerAwareInterface { const VERSION = "0.4.5"; /** * @const int Default Timeout in seconds */ const DEFAULT_REQUEST_TIMEOUT = 300; /** * @var bool|array|resource Whether verbose mode is enabled * * - true - Use STDERR * - array - output/error streams (different) * - resource - output/error stream (same) */ protected static $staticVerbose = false; /** * @var bool|resource Whether debug mode is enabled */ protected static $staticDebug = false; /** * @var \Akamai\Open\EdgeGrid\Authentication */ protected $authentication; /** * @var \Akamai\Open\EdgeGrid\Handler\Verbose */ protected $verboseHandler; /** * @var \Akamai\Open\EdgeGrid\Handler\Debug */ protected $debugHandler; /** * @var bool|array|resource Whether verbose mode is enabled * * - true - Use STDOUT * - array - output/error streams (different) * - resource - output/error stream (same) */ protected $verbose = false; /** * @var bool|resource Whether debugging is enabled */ protected $debug = false; /** * @var bool Whether to override the static verbose setting */ protected $verboseOverride = false; /** * @var bool Whether to override the static debug setting */ protected $debugOverride = false; /** * @var \Closure Logging Handler */ protected $logger; /** * \GuzzleHttp\Client-compatible constructor * * @param array $config Config options array * @param Authentication|null $authentication */ public function __construct( $config = [], Authentication $authentication = null ) { $config = $this->setAuthenticationHandler($config, $authentication); $config = $this->setBasicOptions($config); $config['headers']['User-Agent'] = 'Akamai-Open-Edgegrid-PHP/' . self::VERSION . ' ' . \GuzzleHttp\default_user_agent(); parent::__construct($config); } /** * Make an Asynchronous request * * @param string $method * @param string $uri * @param array $options * @return \GuzzleHttp\Promise\PromiseInterface * @throws \GuzzleHttp\Exception\GuzzleException */ public function requestAsync($method, $uri = null, array $options = []) { $options = $this->setRequestOptions($options); $query = parse_url($uri, PHP_URL_QUERY); if (!empty($query)) { $uri = substr($uri, 0, ((strlen($query)+1)) * -1); parse_str($query, $options['query']); } return parent::requestAsync($method, $uri, $options); } public function sendAsync(\Psr\Http\Message\RequestInterface $request, array $options = []) { $options = $this->setRequestOptions($options); return parent::sendAsync($request, $options); } /** * Set Akamai {OPEN} Authentication Credentials * * @param string $client_token * @param string $client_secret * @param string $access_token * @return $this */ public function setAuth($client_token, $client_secret, $access_token) { $this->authentication->setAuth($client_token, $client_secret, $access_token); return $this; } /** * Specify the headers to include when signing the request * * This is specified by the API, currently no APIs use this * feature. * * @param array $headers * @return $this */ public function setHeadersToSign(array $headers) { $this->authentication->setHeadersToSign($headers); return $this; } /** * Set the max body size * * @param int $max_body_size * @return $this */ public function setMaxBodySize($max_body_size) { $this->authentication->setMaxBodySize($max_body_size); return $this; } /** * Set Request Host * * @param string $host * @return $this */ public function setHost($host) { if (substr($host, -1) == '/') { $host = substr($host, 0, -1); } $headers = $this->getConfig('headers'); $headers['Host'] = $host; $this->setConfigOption('headers', $headers); if (strpos('/', $host) === false) { $host = 'https://' . $host; } $this->setConfigOption('base_uri', $host); return $this; } /** * Set the HTTP request timeout * * @param int $timeout_in_seconds * @return $this */ public function setTimeout($timeout_in_seconds) { $this->setConfigOption('timeout', $timeout_in_seconds); return $this; } /** * Print formatted JSON responses to output * * @param bool|resource $enable * @return $this */ public function setInstanceVerbose($enable) { $this->verboseOverride = true; $this->verbose = $enable; return $this; } /** * Print HTTP requests/responses to output * * @param bool|resource $enable * @return $this */ public function setInstanceDebug($enable) { $this->debugOverride = true; $this->debug = $enable; return $this; } /** * Set a PSR-3 compatible logger (or use monolog by default) * * @param \Psr\Log\LoggerInterface $logger * @param string $messageFormat Message format * @return $this */ public function setLogger( \Psr\Log\LoggerInterface $logger = null, $messageFormat = \GuzzleHttp\MessageFormatter::CLF ) { if ($logger === null) { $handler = new \Monolog\Handler\ErrorLogHandler(\Monolog\Handler\ErrorLogHandler::SAPI); $handler->setFormatter(new \Monolog\Formatter\LineFormatter("%message%")); $logger = new \Monolog\Logger('HTTP Log', [$handler]); } $formatter = new \GuzzleHttp\MessageFormatter($messageFormat); $handler = \GuzzleHttp\Middleware::log($logger, $formatter); $this->logger = $handler; $handlerStack = $this->getConfig('handler'); $this->setLogHandler($handlerStack, $handler); return $this; } /** * Add logger using a given filename/format * * @param string $filename * @param string $format */ public function setSimpleLog($filename, $format = "{code}") { if ($this->logger && !($this->logger instanceof \Monolog\Logger)) { return false; } $handler = new \Monolog\Handler\StreamHandler($filename); $handler->setFormatter(new \Monolog\Formatter\LineFormatter("%message%")); $log = new \Monolog\Logger('HTTP Log', [$handler]); return $this->setLogger($log, $format); } /** * Factory method to create a client using credentials from `.edgerc` * * Automatically checks your HOME directory, and the current working * directory for credentials, if no path is supplied. * * @param string $section Credential section to use * @param string $path Path to .edgerc credentials file * @param array $config Options to pass to the constructor/guzzle * @return \Akamai\Open\EdgeGrid\Client */ public static function createFromEdgeRcFile($section = 'default', $path = null, $config = []) { $auth = \Akamai\Open\EdgeGrid\Authentication::createFromEdgeRcFile($section, $path); if ($host = $auth->getHost()) { $config['base_uri'] = 'https://' . $host; } $client = new static($config, $auth); return $client; } /** * Print HTTP requests/responses to STDOUT * * @param bool|resource $enable */ public static function setDebug($enable) { self::$staticDebug = $enable; } /** * Print formatted JSON responses to STDOUT * * @param bool|resource $enable */ public static function setVerbose($enable) { self::$staticVerbose = $enable; } /** * Handle debug option * * @return bool|resource */ protected function getDebugOption($config) { if (isset($config['debug'])) { return ($config['debug'] === true) ? fopen('php://stderr', 'a') : $config['debug']; } if (($this->debugOverride && $this->debug)) { return ($this->debug === true) ? fopen('php://stderr', 'a') : $this->debug; } elseif ((!$this->debugOverride && static::$staticDebug)) { return (static::$staticDebug === true) ? fopen('php://stderr', 'a') : static::$staticDebug; } return false; } /** * Debugging status for the current request * * @return bool|resource */ protected function isDebug() { if (($this->debugOverride && !$this->debug) || (!($this->debugOverride) && !static::$staticDebug)) { return false; } if ($this->debugOverride && $this->debug) { return $this->debug; } return static::$staticDebug; } /** * Verbose status for the current request * * @return array|bool|resource */ protected function isVerbose() { if (($this->verboseOverride && !$this->verbose) || (!($this->verboseOverride) && !static::$staticVerbose)) { return false; } if ($this->verboseOverride && $this->verbose) { return $this->verbose; } return static::$staticVerbose; } /** * Set the Authentication instance * * @param Authentication|null $authentication */ protected function setAuthentication(array $config, Authentication $authentication = null) { $this->authentication = $authentication; if ($authentication === null) { $this->authentication = new Authentication(); } if (isset($config['timestamp'])) { $this->authentication->setTimestamp($config['timestamp']); } if (isset($config['nonce'])) { $this->authentication->setNonce($config['nonce']); } } /** * Set the Authentication Handler * * @param array $config * @param Authentication|null $authentication * @return array */ protected function setAuthenticationHandler($config, Authentication $authentication = null) { $this->setAuthentication($config, $authentication); $authenticationHandler = new AuthenticationHandler(); $authenticationHandler->setSigner($this->authentication); if (!isset($config['handler'])) { $config['handler'] = \GuzzleHttp\HandlerStack::create(); } try { if (!($config['handler'] instanceof \GuzzleHttp\HandlerStack)) { $config['handler'] = \GuzzleHttp\HandlerStack::create($config['handler']); } $config['handler']->before("history", $authenticationHandler, 'authentication'); } catch (\InvalidArgumentException $e) { // history middleware not added yet $config['handler']->push($authenticationHandler, 'authentication'); } return $config; } /** * @param $config * @return mixed */ protected function setBasicOptions($config) { if (!isset($config['timeout'])) { $config['timeout'] = static::DEFAULT_REQUEST_TIMEOUT; } if (isset($config['base_uri']) && strpos($config['base_uri'], 'http') === false) { $config['base_uri'] = 'https://' . $config['base_uri']; return $config; } return $config; } /** * Set values on the private \GuzzleHttp\Client->config * * This is a terrible hack, and illustrates why making * anything private makes it difficult to extend, and impossible * when there is no setter. * * @param string $what Config option to set * @param mixed $value Value to set the option to * @return void */ protected function setConfigOption($what, $value) { $closure = function () use ($what, $value) { /* @var $this \GuzzleHttp\Client */ $this->config[$what] = $value; }; $closure = $closure->bindTo($this, \GuzzleHttp\Client::CLASS); $closure(); } /** * Add the Debug handler to the HandlerStack * * @param array $options Guzzle Options * @param bool|resource|null $fp Stream to write to * @return array */ protected function setDebugHandler($options, $fp = null) { try { if (is_bool($fp)) { $fp = null; } $handler = $this->getConfig('handler'); // if we have a default handler, and we've already created a DebugHandler // we can bail out now (or we will add another one to the stack) if ($handler && $this->debugHandler) { return $options; } if (isset($options['handler'])) { $handler = $options['handler']; } if ($handler === null) { $handler = \GuzzleHttp\HandlerStack::create(); } if (!$this->debugHandler) { $this->debugHandler = new DebugHandler($fp); } $handler->after("allow_redirects", $this->debugHandler, "debug"); } catch (\InvalidArgumentException $e) { $handler->push($this->debugHandler, "debug"); } $options['handler'] = $handler; return $options; } /** * Add the Log handler to the HandlerStack * * @param \GuzzleHttp\HandlerStack $handlerStack * @param callable $logHandler */ protected function setLogHandler(\GuzzleHttp\HandlerStack $handlerStack, callable $logHandler) { try { $handlerStack->after("history", $logHandler, "logger"); } catch (\InvalidArgumentException $e) { try { $handlerStack->before("allow_redirects", $logHandler, "logger"); } catch (\InvalidArgumentException $e) { $handlerStack->push($logHandler, "logger"); } } return $this; } /** * Add the Verbose handler to the HandlerStack * * @param array $options Guzzle Options * @param bool|resource|array|null $fp Stream to write to * @return array */ protected function setVerboseHandler($options, $fp = null) { try { if (is_bool($fp) || $fp === null) { $fp = ['outputStream' => null, 'errorStream' => null]; } elseif (!is_array($fp)) { $fp = ['outputStream' => $fp, 'errorStream' => $fp]; } $handler = $this->getConfig('handler'); // if we have a default handler, and we've already created a VerboseHandler // we can bail out now (or we will add another one to the stack) if ($handler && $this->verboseHandler) { return $options; } if (isset($options['handler'])) { $handler = $options['handler']; } if ($handler === null) { $handler = \GuzzleHttp\HandlerStack::create(); } if (!$this->verboseHandler) { $this->verboseHandler = new VerboseHandler(array_shift($fp), array_shift($fp)); } $handler->after("allow_redirects", $this->verboseHandler, "verbose"); } catch (\InvalidArgumentException $e) { $handler->push($this->verboseHandler, "verbose"); } $options['handler'] = $handler; return $options; } /** * @param array $options * @return array */ protected function setRequestOptions(array $options) { if (isset($options['timestamp'])) { $this->authentication->setTimestamp($options['timestamp']); } elseif (!$this->getConfig('timestamp')) { $this->authentication->setTimestamp(); } if (isset($options['nonce'])) { $this->authentication->setNonce($options['nonce']); } if (isset($options['handler'])) { $options = $this->setAuthenticationHandler($options, $this->authentication); } if ($fp = $this->isVerbose()) { $options = $this->setVerboseHandler($options, $fp); } $options['debug'] = $this->getDebugOption($options); if ($fp = $this->isDebug()) { $options = $this->setDebugHandler($options, $fp); } if ($this->logger && isset($options['handler'])) { $this->setLogHandler($options['handler'], $this->logger); return $options; } return $options; } } |