|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Akamai {OPEN} EdgeGrid Auth Client |
|
4
|
|
|
* |
|
5
|
|
|
* @author Davey Shafik <[email protected]> |
|
6
|
|
|
* @copyright Copyright 2016 Akamai Technologies, Inc. All rights reserved. |
|
7
|
|
|
* @license Apache 2.0 |
|
8
|
|
|
* @link https://github.com/akamai-open/AkamaiOPEN-edgegrid-php-client |
|
9
|
|
|
* @link https://developer.akamai.com |
|
10
|
|
|
* @link https://developer.akamai.com/introduction/Client_Auth.html |
|
11
|
|
|
*/ |
|
12
|
|
|
namespace Akamai\Open\EdgeGrid\Handler; |
|
13
|
|
|
|
|
14
|
|
|
use Akamai\Open\EdgeGrid\Exception\HandlerException\IOException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Debug Response Guzzle Middleware Handler |
|
18
|
|
|
* |
|
19
|
|
|
* @package Akamai\Open\EdgeGrid\Client |
|
20
|
|
|
*/ |
|
21
|
|
|
class Debug |
|
22
|
|
|
{ |
|
23
|
|
|
protected static $messages = [ |
|
24
|
|
|
403 => [ |
|
25
|
|
|
"This indicates a problem with authorization.\n", |
|
26
|
|
|
"Please ensure that the credentials you created for this script\n", |
|
27
|
|
|
"have the necessary permissions in the Luna portal.\n", |
|
28
|
|
|
], |
|
29
|
|
|
400 => [ |
|
30
|
|
|
'This indicates a problem with authentication or headers.', |
|
31
|
|
|
'Please ensure that the .edgerc file is formatted correctly.', |
|
32
|
|
|
'If you still have issues, please use gen_edgerc.php to generate the credentials', |
|
33
|
|
|
], |
|
34
|
|
|
401 => 400, |
|
35
|
|
|
404 => [ |
|
36
|
|
|
"This means that the page does not exist as requested.\n", |
|
37
|
|
|
"Please ensure that the URL you're calling is correctly formatted\n", |
|
38
|
|
|
"or look at other examples to make sure yours matches.\n", |
|
39
|
|
|
] |
|
40
|
|
|
]; |
|
41
|
|
|
|
|
42
|
|
|
protected $fp; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Debug constructor. |
|
46
|
|
|
* |
|
47
|
|
|
* This method accepts a stream resource or a valid stream URL |
|
48
|
|
|
* (including file paths). If none is passed in stderr is used. |
|
49
|
|
|
* |
|
50
|
|
|
* @param resource|null $resource |
|
51
|
|
|
* @throws \Akamai\Open\EdgeGrid\Exception\HandlerException\IOException |
|
52
|
|
|
*/ |
|
53
|
18 |
|
public function __construct($resource = null) |
|
54
|
|
|
{ |
|
55
|
18 |
|
$fp = $resource; |
|
56
|
|
|
|
|
57
|
18 |
|
if (!is_resource($fp) && $fp !== null) { |
|
58
|
2 |
|
$fp = @fopen($fp, 'ab+'); |
|
59
|
2 |
|
if (!$fp) { |
|
60
|
1 |
|
throw new IOException('Unable to use resource: ' . (string) $resource); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
17 |
|
if ($fp === null) { |
|
65
|
6 |
|
$fp = fopen('php://stderr', 'ab'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
17 |
|
$this->fp = $fp; |
|
69
|
17 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Handle the request/response |
|
73
|
|
|
* |
|
74
|
|
|
* @param callable $handler |
|
75
|
|
|
* @return \Closure |
|
76
|
|
|
*/ |
|
77
|
16 |
|
public function __invoke(callable $handler) |
|
78
|
|
|
{ |
|
79
|
|
|
$colors = [ |
|
80
|
16 |
|
'red' => '', |
|
81
|
|
|
'yellow' => '', |
|
82
|
|
|
'cyan' => '', |
|
83
|
|
|
'reset' => '', |
|
84
|
|
|
]; |
|
85
|
|
|
|
|
86
|
16 |
View Code Duplication |
if (PHP_SAPI === 'cli') { |
|
87
|
|
|
$colors = [ |
|
88
|
16 |
|
'red' => "\x1b[31;01m", |
|
89
|
|
|
'yellow' => "\x1b[33;01m", |
|
90
|
|
|
'cyan' => "\x1b[36;01m", |
|
91
|
|
|
'reset' => "\x1b[39;49;00m", |
|
92
|
|
|
]; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return function (\Psr\Http\Message\RequestInterface $request, array $config) use ($handler, $colors) { |
|
96
|
16 |
|
return $handler($request, $config)->then( |
|
97
|
|
|
function (\Psr\Http\Message\ResponseInterface $response) use ($colors, $request) { |
|
98
|
13 |
|
$statusCode = $response->getStatusCode(); |
|
99
|
13 |
|
if ($statusCode > 399 && $statusCode < 600) { |
|
100
|
7 |
|
$body = trim($response->getBody()); |
|
101
|
7 |
|
$result = json_decode($body); |
|
102
|
7 |
|
if ($result !== null) { |
|
103
|
5 |
|
if (isset($result->detail)) { |
|
104
|
4 |
|
$detail = $result->detail; |
|
105
|
|
|
} else { |
|
106
|
5 |
|
$detail = json_encode($result, JSON_PRETTY_PRINT); |
|
107
|
|
|
} |
|
108
|
|
|
} else { |
|
109
|
2 |
|
$detail = (!empty(trim($body))) ? $body : 'No response body returned'; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
7 |
|
$out = []; |
|
113
|
7 |
|
$out[] = "{$colors['red']}===> [ERROR] Call to %s failed with a %s result"; |
|
114
|
|
|
|
|
115
|
7 |
|
if (isset(self::$messages[$statusCode])) { |
|
116
|
4 |
|
$message = self::$messages[$statusCode]; |
|
117
|
4 |
|
if (is_int($message) && isset(self::$messages[$message])) { |
|
118
|
1 |
|
$message = self::$messages[$message]; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
4 |
|
foreach ($message as $line) { |
|
122
|
4 |
|
$out[] = '===> [ERROR] ' . $line; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
7 |
|
$out[] = '===> [ERROR] Problem details:'; |
|
127
|
7 |
|
$out[] = $detail; |
|
128
|
|
|
|
|
129
|
7 |
|
$out = sprintf( |
|
130
|
7 |
|
implode("\n", $out), |
|
131
|
7 |
|
$request->getUri()->getPath(), |
|
132
|
7 |
|
$response->getStatusCode() . ' ' . $response->getReasonPhrase(), |
|
133
|
|
|
$detail |
|
134
|
|
|
); |
|
135
|
|
|
|
|
136
|
7 |
|
fwrite($this->fp, $out); |
|
137
|
7 |
|
fwrite($this->fp, "{$colors['reset']}\n"); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
13 |
|
return $response; |
|
141
|
16 |
|
}, |
|
142
|
16 |
|
function (\Exception $reason) { |
|
143
|
3 |
|
return new \GuzzleHttp\Promise\RejectedPromise($reason); |
|
144
|
16 |
|
} |
|
145
|
|
|
); |
|
146
|
16 |
|
}; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|