forked from tradecoverexchange/google-cloud-tasks-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRequestGenerator.php
64 lines (55 loc) · 1.88 KB
/
RequestGenerator.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
60
61
62
63
64
<?php
namespace TradeCoverExchange\GoogleCloudTaskLaravel;
use Google\Cloud\Tasks\V2beta3\AppEngineHttpRequest;
use Google\Cloud\Tasks\V2beta3\HttpMethod;
use Google\Cloud\Tasks\V2beta3\HttpRequest;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Routing\UrlGenerator;
class RequestGenerator
{
use ConnectionRetrieval;
public function __construct(protected UrlGenerator $generator, protected Container $container)
{
}
public function forAppEngine(string $payload, string $connection, string $queue): AppEngineHttpRequest
{
return (new AppEngineHttpRequest())
->setHttpMethod(HttpMethod::POST)
->setBody($payload)
->setRelativeUri(
$this->generator->route(
'google.tasks',
['connection' => $connection, 'queue' => $queue],
false
)
);
}
public function forHttpHandler(string $payload, string $connection, string $queue): HttpRequest
{
return (new HttpRequest())
->setHttpMethod(HttpMethod::POST)
->setBody($payload)
->setUrl(
$this->cloudTasksUrl($connection, $queue)
);
}
protected function cloudTasksUrl(string $connection, string $queue): string
{
$config = $this->getConfig($connection);
if ($config['domain'] ?? false) {
return 'https://' . $config['domain'] .
$this->generator->route(
'google.tasks',
['connection' => $connection, 'queue' => $queue],
false
);
}
return $this->generator->secure(
$this->generator->route(
'google.tasks',
['connection' => $connection, 'queue' => $queue],
false
)
);
}
}