@@ -89,22 +89,24 @@ public function getConnections(): array
89
89
90
90
/**
91
91
* {@inheritdoc}
92
- *
93
- * @throws \Radcliffe\Xero\Exception\InvalidOptionsException
94
92
*/
95
93
public static function createFromConfig (array $ config , array $ options = []): static
96
94
{
97
- if (isset ($ options ['tenant ' ])) {
98
- $ config ['headers ' ]['xero-tenant-id ' ] = $ options ['tenant ' ];
95
+ if (!isset ($ config ['Accept ' ])) {
96
+ $ config ['Accept ' ] = 'application/json ' ;
97
+ }
98
+
99
+ if (!isset ($ config ['base_uri ' ])) {
100
+ $ config ['base_uri ' ] = 'https://api.xero.com/api.xro/2.0/ ' ;
99
101
}
100
102
101
103
if (isset ($ config ['handler ' ]) && is_a ($ config ['handler ' ], '\GuzzleHttp\HandlerStack ' )) {
102
- $ stack = $ config ['handler ' ];
104
+ $ stack = HandlerStack:: create ( $ config ['handler ' ]) ;
103
105
} else {
104
106
$ stack = HandlerStack::create ();
105
107
}
106
108
107
- $ stack ->push ( Middleware::mapRequest (function (RequestInterface $ request ) use ($ options ) {
109
+ $ stack ->before ( ' prepare_body ' , Middleware::mapRequest (function (RequestInterface $ request ) use ($ options ) {
108
110
$ validUrls = array_filter (self ::getValidUrls (), fn ($ url ) => str_starts_with ($ request ->getUri (), $ url ));
109
111
if (empty ($ validUrls )) {
110
112
throw new XeroRequestException ('API URL is not valid ' , $ request );
@@ -113,8 +115,13 @@ public static function createFromConfig(array $config, array $options = []): sta
113
115
if (!isset ($ options ['auth_token ' ])) {
114
116
throw new XeroRequestException ('Missing required parameter auth_token ' , $ request );
115
117
}
116
- return $ request ->withHeader ('Authorization ' , 'Bearer ' . $ options ['auth_token ' ]);
117
- }));
118
+ $ new = $ request ->withHeader ('Authorization ' , 'Bearer ' . $ options ['auth_token ' ]);
119
+
120
+ if (isset ($ options ['tenant ' ])) {
121
+ $ new = $ new ->withHeader ('Xero-tenant-id ' , $ options ['tenant ' ]);
122
+ }
123
+ return $ new ;
124
+ }), 'xero ' );
118
125
119
126
$ client = new Client ($ config + [
120
127
'handler ' => $ stack ,
@@ -126,7 +133,6 @@ public static function createFromConfig(array $config, array $options = []): sta
126
133
* {@inheritdoc}
127
134
*
128
135
* @throws \League\OAuth2\Client\Provider\Exception\IdentityProviderException
129
- * @throws \Radcliffe\Xero\Exception\InvalidOptionsException
130
136
*/
131
137
public static function createFromToken (
132
138
string $ id ,
@@ -138,6 +144,8 @@ public static function createFromToken(
138
144
array $ collaborators = [],
139
145
string $ redirectUri = ''
140
146
): static {
147
+ $ guzzle_options = [];
148
+ $ xero_options = [];
141
149
if ($ grant !== null ) {
142
150
// Fetch a new access token from a refresh token.
143
151
$ provider = new XeroProvider ([
@@ -157,14 +165,20 @@ public static function createFromToken(
157
165
$ token = $ refreshedToken ->getToken ();
158
166
}
159
167
168
+ $ guzzle_options ['Accept ' ] = 'application/json ' ;
169
+ if (isset ($ options ['handler ' ])) {
170
+ $ guzzle_options ['handler ' ] = $ options ['handler ' ];
171
+ }
160
172
if (!isset ($ options ['base_uri ' ])) {
161
- $ options ['base_uri ' ] = 'https://api.xero.com/api.xro/2.0/ ' ;
173
+ $ guzzle_options ['base_uri ' ] = 'https://api.xero.com/api.xro/2.0/ ' ;
174
+ }
175
+ if (isset ($ options ['tenant ' ])) {
176
+ $ xero_options ['tenant ' ] = $ options ['tenant ' ];
162
177
}
178
+ $ xero_options ['auth_token ' ] = $ token ;
163
179
164
180
// Create a new static instance.
165
- $ instance = self ::createFromConfig ($ options , ['auth_token ' => $ token ]);
166
-
167
- $ instance ->tenantIds = $ instance ->getConnections ();
181
+ $ instance = self ::createFromConfig ($ guzzle_options , $ xero_options );
168
182
169
183
if (isset ($ refreshedToken )) {
170
184
$ instance ->refreshedToken = $ refreshedToken ;
@@ -226,10 +240,15 @@ public function getRefreshedToken(): ?AccessTokenInterface
226
240
/**
227
241
* The tenant guids accessible by this client.
228
242
*
243
+ * This will make a request if tenant ids is empty.
244
+ *
229
245
* @return string[]
230
246
*/
231
247
public function getTenantIds (): array
232
248
{
249
+ if (!$ this ->tenantIds ) {
250
+ $ this ->tenantIds = $ this ->getConnections ();
251
+ }
233
252
return $ this ->tenantIds ;
234
253
}
235
254
}
0 commit comments