5
5
use Illuminate \Http \Client \PendingRequest ;
6
6
use Illuminate \Support \Collection ;
7
7
use Illuminate \Support \Facades \Http ;
8
- use Log1x \EnvoyerDeploy \Exceptions \ApiKeyMissingException ;
8
+ use Log1x \EnvoyerDeploy \Exceptions \EnvoyerApiErrorException ;
9
+ use Log1x \EnvoyerDeploy \Exceptions \EnvoyerApiKeyMissingException ;
9
10
10
11
class EnvoyerApi
11
12
{
@@ -34,6 +35,8 @@ public static function make(): EnvoyerApi
34
35
35
36
/**
36
37
* Get the Envoyer HTTP client.
38
+ *
39
+ * @throws \Log1x\EnvoyerDeploy\Exceptions\EnvoyerApiKeyMissingException
37
40
*/
38
41
public function api (): PendingRequest
39
42
{
@@ -44,6 +47,42 @@ public function api(): PendingRequest
44
47
])->baseUrl ($ this ->endpoint );
45
48
}
46
49
50
+ /**
51
+ * Get from the Envoyer API.
52
+ *
53
+ * @throws \Log1x\EnvoyerDeploy\Exceptions\EnvoyerApiErrorException
54
+ */
55
+ public function get (string $ uri ): object
56
+ {
57
+ $ response = $ this ->api ()->get ($ uri );
58
+
59
+ if ($ response ->failed ()) {
60
+ throw new EnvoyerApiErrorException ($ response );
61
+ }
62
+
63
+ return json_decode (
64
+ $ response ->body ()
65
+ );
66
+ }
67
+
68
+ /**
69
+ * Post to the Envoyer API.
70
+ *
71
+ * @throws \Log1x\EnvoyerDeploy\Exceptions\EnvoyerApiErrorException
72
+ */
73
+ public function post (string $ uri ): object
74
+ {
75
+ $ response = $ this ->api ()->post ($ uri );
76
+
77
+ if ($ response ->failed ()) {
78
+ throw new EnvoyerApiErrorException ($ response );
79
+ }
80
+
81
+ return json_decode (
82
+ $ response ->body ()
83
+ );
84
+ }
85
+
47
86
/**
48
87
* Get the Envoyer API endpoint.
49
88
*/
@@ -75,7 +114,7 @@ public function apiKey(string $apiKey): EnvoyerApi
75
114
/**
76
115
* Get the Envoyer API key.
77
116
*
78
- * @throws \Log1x\EnvoyerDeploy\Exceptions\ApiKeyMissingException
117
+ * @throws \Log1x\EnvoyerDeploy\Exceptions\EnvoyerApiKeyMissingException
79
118
*/
80
119
public function getApiKey (): string
81
120
{
@@ -86,7 +125,7 @@ public function getApiKey(): string
86
125
$ this ->apiKey = config ('envoyer.api_key ' , null );
87
126
88
127
if (empty ($ this ->apiKey )) {
89
- throw new ApiKeyMissingException ;
128
+ throw new EnvoyerApiKeyMissingException ;
90
129
}
91
130
92
131
return $ this ->apiKey ;
@@ -107,9 +146,7 @@ public function project(int $project): EnvoyerApi
107
146
*/
108
147
public function getProjects (): Collection
109
148
{
110
- $ projects = json_decode (
111
- $ this ->api ()->get ('/projects ' )->body ()
112
- );
149
+ $ projects = $ this ->get ('/projects ' );
113
150
114
151
return collect ($ projects ->projects ?? []);
115
152
}
@@ -119,9 +156,7 @@ public function getProjects(): Collection
119
156
*/
120
157
public function getProject (): ?object
121
158
{
122
- $ project = json_decode (
123
- $ this ->api ()->get ("/projects/ {$ this ->project }" )->body ()
124
- );
159
+ $ project = $ this ->get ("/projects/ {$ this ->project }" );
125
160
126
161
return $ project ->project ?? null ;
127
162
}
@@ -131,9 +166,7 @@ public function getProject(): ?object
131
166
*/
132
167
public function getDeployment (int $ deployment ): ?object
133
168
{
134
- $ deployment = json_decode (
135
- $ this ->api ()->get ("/projects/ {$ this ->project }/deployments/ {$ deployment }" )->body ()
136
- );
169
+ $ deployment = $ this ->get ("/projects/ {$ this ->project }/deployments/ {$ deployment }" );
137
170
138
171
return $ deployment ->deployment ?? null ;
139
172
}
@@ -143,9 +176,7 @@ public function getDeployment(int $deployment): ?object
143
176
*/
144
177
public function getDeployments (): Collection
145
178
{
146
- $ deployments = json_decode (
147
- $ this ->api ()->get ("/projects/ {$ this ->project }/deployments " )->body ()
148
- );
179
+ $ deployments = $ this ->get ("/projects/ {$ this ->project }/deployments " );
149
180
150
181
return collect ($ deployments ->deployments ?? []);
151
182
}
@@ -155,9 +186,7 @@ public function getDeployments(): Collection
155
186
*/
156
187
public function getActions (): Collection
157
188
{
158
- $ actions = json_decode (
159
- $ this ->api ()->get ('/actions ' )->body ()
160
- );
189
+ $ actions = $ this ->get ('/actions ' );
161
190
162
191
return collect ($ actions ->actions ?? []);
163
192
}
@@ -167,9 +196,7 @@ public function getActions(): Collection
167
196
*/
168
197
public function getHooks (): Collection
169
198
{
170
- $ hooks = json_decode (
171
- $ this ->api ()->get ("/projects/ {$ this ->project }/hooks " )->body ()
172
- );
199
+ $ hooks = $ this ->get ("/projects/ {$ this ->project }/hooks " );
173
200
174
201
return collect ($ hooks ->hooks ?? []);
175
202
}
@@ -179,9 +206,7 @@ public function getHooks(): Collection
179
206
*/
180
207
public function getFolders (): Collection
181
208
{
182
- $ folders = json_decode (
183
- $ this ->api ()->get ("/projects/ {$ this ->project }/folders " )->body ()
184
- );
209
+ $ folders = $ this ->get ("/projects/ {$ this ->project }/folders " );
185
210
186
211
return collect ($ folders ->folders ?? []);
187
212
}
@@ -191,6 +216,6 @@ public function getFolders(): Collection
191
216
*/
192
217
public function deploy (): void
193
218
{
194
- $ this ->api ()-> post ("/projects/ {$ this ->project }/deployments " );
219
+ $ this ->post ("/projects/ {$ this ->project }/deployments " );
195
220
}
196
221
}
0 commit comments