|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SevenShores\Hubspot\Http; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
|
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
7
|
|
|
use SevenShores\Hubspot\Exceptions\BadRequest; |
|
8
|
|
|
use SevenShores\Hubspot\Exceptions\InvalidArgument; |
|
9
|
|
|
|
|
10
|
|
|
class Client |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var string */ |
|
13
|
|
|
public $key; |
|
14
|
|
|
/** @var bool */ |
|
15
|
|
|
public $oauth; |
|
16
|
|
|
|
|
17
|
|
|
/** @var bool */ |
|
18
|
|
|
public $oauth2; |
|
19
|
|
|
|
|
20
|
|
|
/** @var int */ |
|
21
|
|
|
public $userId; |
|
22
|
|
|
|
|
23
|
|
|
/** @var \GuzzleHttp\Client */ |
|
24
|
|
|
public $client; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Guzzle allows options into its request method. Prepare for some defaults |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $clientOptions = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* if set to false, no Response object is created, but the one from Guzzle is directly returned |
|
34
|
|
|
* comes in handy own error handling |
|
35
|
|
|
* |
|
36
|
|
|
* @var bool |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $wrapResponse = true; |
|
39
|
|
|
|
|
40
|
|
|
/** @var string */ |
|
41
|
|
|
private $user_agent = "SevenShores_Hubspot_PHP/1.0.0-rc.1 (https://github.com/ryanwinchester/hubspot-php)"; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Make it, baby. |
|
45
|
|
|
* |
|
46
|
|
|
* @param array $config Configuration array |
|
47
|
|
|
* @param GuzzleClient $client The Http Client (Defaults to Guzzle) |
|
48
|
|
|
* @param array $clientOptions options to be passed to Guzzle upon each request |
|
49
|
|
|
* @param bool $wrapResponse wrap request response in own Response object |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct($config = [], $client = null, $clientOptions = [], $wrapResponse = true) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->clientOptions = $clientOptions; |
|
54
|
|
|
$this->wrapResponse = $wrapResponse; |
|
55
|
|
|
|
|
56
|
|
|
$this->key = isset($config["key"]) ? $config["key"] : getenv("HUBSPOT_SECRET"); |
|
57
|
|
|
if (empty($this->key)) { |
|
58
|
|
|
throw new InvalidArgument("You must provide a Hubspot api key or token."); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if (isset($config['userId'])) { |
|
62
|
|
|
$this->userId = $config['userId']; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$this->oauth = isset($config["oauth"]) ? $config["oauth"] : false; |
|
66
|
|
|
$this->oauth2 = isset($config["oauth2"]) ? $config["oauth2"] : false; |
|
67
|
|
|
if ($this->oauth && $this->oauth2) { |
|
68
|
|
|
throw new InvalidArgument("Cannot sign requests with both OAuth1 and OAuth2"); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$this->client = $client ?: new GuzzleClient(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Send the request... |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $method The HTTP request verb |
|
78
|
|
|
* @param string $endpoint The Hubspot API endpoint |
|
79
|
|
|
* @param array $options An array of options to send with the request |
|
80
|
|
|
* @param string $query_string A query string to send with the request |
|
81
|
|
|
* @param boolean $requires_auth Whether or not this HubSpot API endpoint requires authentication |
|
82
|
|
|
* @return \SevenShores\Hubspot\Http\Response|ResponseInterface |
|
83
|
|
|
* @throws \SevenShores\Hubspot\Exceptions\BadRequest |
|
84
|
|
|
*/ |
|
85
|
|
|
public function request($method, $endpoint, $options = [], $query_string = null, $requires_auth = true) |
|
86
|
|
|
{ |
|
87
|
|
|
$url = $this->generateUrl($endpoint, $query_string, $requires_auth); |
|
88
|
|
|
|
|
89
|
|
|
$options = array_merge($this->clientOptions, $options); |
|
90
|
|
|
$options["headers"]["User-Agent"] = $this->user_agent; |
|
91
|
|
|
|
|
92
|
|
|
if ($this->oauth2) { |
|
93
|
|
|
$options["headers"]["Authorization"] = "Bearer " . $this->key; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
try { |
|
97
|
|
|
if ($this->wrapResponse === false) { |
|
98
|
|
|
return $this->client->request($method, $url, $options); |
|
99
|
|
|
} |
|
100
|
|
|
return new Response($this->client->request($method, $url, $options)); |
|
101
|
|
|
} catch (\GuzzleHttp\Exception\BadResponseException $e) { |
|
102
|
|
|
throw new BadRequest(\GuzzleHttp\Psr7\str($e->getResponse()), $e->getCode(), $e); |
|
|
|
|
|
|
103
|
|
|
} catch (\Exception $e) { |
|
104
|
|
|
throw new BadRequest($e->getMessage(), $e->getCode(), $e); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Generate the full endpoint url, including query string. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $endpoint The HubSpot API endpoint. |
|
112
|
|
|
* @param string $query_string The query string to send to the endpoint. |
|
113
|
|
|
* @param boolean $requires_auth Whether or not this HubSpot API endpoint requires authentication |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function generateUrl($endpoint, $query_string = null, $requires_auth = true) |
|
117
|
|
|
{ |
|
118
|
|
|
$url = $endpoint."?"; |
|
119
|
|
|
|
|
120
|
|
|
if ($requires_auth) { |
|
121
|
|
|
$authType = $this->oauth ? "access_token" : "hapikey"; |
|
122
|
|
|
|
|
123
|
|
|
if (!$this->oauth2) { |
|
124
|
|
|
$url .= $authType."=".$this->key; |
|
125
|
|
|
|
|
126
|
|
|
if ($this->userId) { |
|
127
|
|
|
$url .= "&userId={$this->userId}"; |
|
128
|
|
|
} |
|
129
|
|
|
} else { |
|
130
|
|
|
if ($this->userId) { |
|
131
|
|
|
$url .= "userId={$this->userId}"; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $url.$query_string; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: