|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://github.com/flipbox/relay-hubspot/blob/master/LICENSE |
|
6
|
|
|
* @link https://github.com/flipbox/relay-hubspot |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Flipbox\Relay\HubSpot\Middleware; |
|
10
|
|
|
|
|
11
|
|
|
use Flipbox\Http\Stream\Factory; |
|
12
|
|
|
use Flipbox\Relay\Middleware\AbstractMiddleware; |
|
13
|
|
|
use Flipbox\Skeleton\Helpers\JsonHelper; |
|
14
|
|
|
use GuzzleHttp\Client as GuzzleHttpClient; |
|
15
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
16
|
|
|
use Psr\Http\Message\RequestInterface; |
|
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @author Flipbox Factory <[email protected]> |
|
21
|
|
|
* @since 1.0.0 |
|
22
|
|
|
*/ |
|
23
|
|
|
class Client extends AbstractMiddleware |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @inheritdoc |
|
27
|
|
|
* @throws \Flipbox\Http\Stream\Exceptions\InvalidStreamException |
|
28
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next = null) |
|
31
|
|
|
{ |
|
32
|
|
|
parent::__invoke($request, $response, $next); |
|
33
|
|
|
|
|
34
|
|
|
$request = $this->prepRequest($request); |
|
35
|
|
|
$response = $this->call($request, $response); |
|
36
|
|
|
return $next($request, $response); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Call the API |
|
41
|
|
|
* |
|
42
|
|
|
* @param RequestInterface $request |
|
43
|
|
|
* @param ResponseInterface $response |
|
44
|
|
|
* @return ResponseInterface |
|
45
|
|
|
* @throws \Flipbox\Http\Stream\Exceptions\InvalidStreamException |
|
46
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
47
|
|
|
*/ |
|
48
|
|
|
private function call(RequestInterface $request, ResponseInterface $response) |
|
49
|
|
|
{ |
|
50
|
|
|
try { |
|
51
|
|
|
$this->info( |
|
52
|
|
|
"HUBSPOT API REQUEST - URI: {uri}, METHOD: {method}, PAYLOAD: {payload}", |
|
53
|
|
|
[ |
|
54
|
|
|
'uri' => $request->getUri(), |
|
55
|
|
|
'method' => $request->getMethod(), |
|
56
|
|
|
'payload' => $request->getBody() |
|
57
|
|
|
] |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
$httpResponse = (new GuzzleHttpClient()) |
|
61
|
|
|
->send($request); |
|
62
|
|
|
} catch (ClientException $e) { |
|
63
|
|
|
$this->error( |
|
64
|
|
|
"HUBSPOT API EXCEPTION: {exception}", |
|
65
|
|
|
[ |
|
66
|
|
|
'exception' => $e |
|
67
|
|
|
] |
|
68
|
|
|
); |
|
69
|
|
|
$httpResponse = $e->getResponse(); |
|
70
|
|
|
|
|
71
|
|
|
// If an exception was thrown, pass the message on via the response |
|
72
|
|
|
$contents = $httpResponse->getBody()->getContents(); |
|
73
|
|
|
if (empty($contents)) { |
|
74
|
|
|
$stream = Factory::create(JsonHelper::encode([ |
|
75
|
|
|
'status' => 'exception', |
|
76
|
|
|
'message' => $e->getMessage() |
|
77
|
|
|
])); |
|
78
|
|
|
|
|
79
|
|
|
$httpResponse = $httpResponse->withBody($stream); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$httpResponse->getBody()->rewind(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
// Sync responses |
|
86
|
|
|
if ($httpResponse !== null) { |
|
87
|
|
|
$this->info( |
|
88
|
|
|
"HUBSPOT API RESPONSE - CODE: {code}, BODY: {response}", |
|
89
|
|
|
[ |
|
90
|
|
|
'code' => $httpResponse->getStatusCode(), |
|
91
|
|
|
'response' => $httpResponse->getBody() |
|
92
|
|
|
] |
|
93
|
|
|
); |
|
94
|
|
|
|
|
95
|
|
|
$httpResponse->getBody()->rewind(); |
|
96
|
|
|
|
|
97
|
|
|
$response = $this->syncResponse($httpResponse, $response); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $response; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param RequestInterface $request |
|
105
|
|
|
* @return RequestInterface |
|
106
|
|
|
*/ |
|
107
|
|
|
private function prepRequest(RequestInterface $request) |
|
108
|
|
|
{ |
|
109
|
|
|
return $request->withHeader('Content-Type', 'application/json') |
|
110
|
|
|
->withHeader('Accept', 'application/json'); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param ResponseInterface $httpResponse |
|
115
|
|
|
* @param ResponseInterface $response |
|
116
|
|
|
* @return ResponseInterface |
|
117
|
|
|
*/ |
|
118
|
|
|
private function syncResponse(ResponseInterface $httpResponse, ResponseInterface $response) |
|
119
|
|
|
{ |
|
120
|
|
|
// Add headers |
|
121
|
|
|
foreach ($httpResponse->getHeaders() as $name => $value) { |
|
122
|
|
|
$response = $response->withHeader($name, $value); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $response->withStatus($httpResponse->getStatusCode(), $httpResponse->getReasonPhrase()) |
|
126
|
|
|
->withBody($httpResponse->getBody()) |
|
127
|
|
|
->withProtocolVersion($httpResponse->getProtocolVersion()); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|