-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuildBaseRequest.php
59 lines (49 loc) · 1.8 KB
/
BuildBaseRequest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
declare(strict_types=1);
namespace Lemaur\Pinterest\Services\Concerns;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Http;
use Lemaur\Pinterest\Exceptions\OAuthException;
trait BuildBaseRequest
{
/**
* @throws RequestException
* @throws OAuthException
*/
public function buildRequestWithToken(): PendingRequest
{
if ($this->oauth->missingAccessToken()) {
$this->oauth()->requestAccessToken();
}
if ($this->oauth->isAccessTokenExpired()) {
$this->oauth()->refreshAccessToken();
}
if ($this->oauth->isRefreshTokenExpired()) {
throw new OAuthException('Pinterest refresh token expired. You should request a new access code by running: `php artisan pinterest:get-access-code-link`');
}
return $this->withBaseUrl()
->asJson()
->withToken($this->oauth->access_token);
}
public function buildRequestWithBasicBase64(): PendingRequest
{
$authHeader = 'Basic '.base64_encode($this->config->client_id.':'.$this->config->client_secret);
return $this->withBaseUrl()
->asForm()
->withHeaders(['Authorization' => $authHeader]);
}
public function withBaseUrl(): PendingRequest
{
return Http::baseUrl($this->config->base_url)
->connectTimeout(seconds: $this->config->connect_timeout)
->timeout(seconds: $this->config->timeout)
->when(
value: $this->config->retry_enabled,
callback: fn (PendingRequest $client) => $client->retry(
times: $this->config->retry_times,
sleepMilliseconds: $this->config->retry_sleep
)
);
}
}