-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathComponentHttpClient.php
More file actions
76 lines (58 loc) · 2.17 KB
/
ComponentHttpClient.php
File metadata and controls
76 lines (58 loc) · 2.17 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
<?php
namespace Sheaf\Cli\Services;
use Exception;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
class ComponentHttpClient
{
protected string $baseUrl;
protected string|null $token;
public function __construct()
{
$this->baseUrl = config('sheaf.cli.server_url');
$this->token = SheafConfig::getUserToken();
}
public function fetchComponentFilesPath(string $name)
{
$url = "{$this->baseUrl}/api/cli/components/$name/files";
$response = Http::asJson()->get($url);
if ($response->failed()) {
$component = Str::of($name)->headline();
$message = array_key_exists('message', $response->json() ?? []) ? $response->json()['message'] : "";
throw new Exception("Failed to install the component '$component'. \n $message");
}
return [
'success' => true,
'data' => $response->collect()
];
}
public function fetchResources(string $componentName)
{
$projectHash = SheafConfig::getProjectHash();
$url = "{$this->baseUrl}/api/cli/components/$componentName?project_hash=$projectHash";
$response = Http::asJson()
->when($this->token, fn($http) => $http->withToken($this->token))
->get($url);
if ($response->failed()) {
$component = Str::of($componentName)->headline();
$message = array_key_exists('message', $response->json() ?? []) ? $response->json()['message'] : "";
throw new Exception("Failed to install the component '$component'. \n $message");
}
return [
'success' => true,
'data' => $response->collect()
];
}
public function subscribeUserToNewsletter(string $email)
{
try {
$url = "{$this->baseUrl}/api/cli/newsletter/subscribe?email={$email}";
$result = Http::asJson()->get($url);
if (!$result['success']) {
throw new Exception("Failed to subscribe to the news letter");
}
} catch (\Throwable $th) {
throw new Exception("Failed to subscribe to the news letter");
}
}
}