9
9
use Flowpack \Prunner \ValueObject \JobId ;
10
10
use Flowpack \Prunner \ValueObject \PipelineName ;
11
11
use GuzzleHttp \Client ;
12
+ use GuzzleHttp \Exception \GuzzleException ;
12
13
use Neos \Flow \Annotations as Flow ;
13
14
use Psr \Http \Message \ResponseInterface ;
14
15
use Symfony \Component \Yaml \Exception \ParseException ;
@@ -53,14 +54,25 @@ class PrunnerApiService
53
54
public function loadPipelinesAndJobs (): PipelinesAndJobsResponse
54
55
{
55
56
$ resultString = $ this ->apiCall ('GET ' , 'pipelines/jobs ' , null )->getBody ()->getContents ();
56
- $ result = json_decode ($ resultString , true );
57
+ try {
58
+ $ result = json_decode ($ resultString , true , 512 , JSON_THROW_ON_ERROR );
59
+ } catch (\JsonException $ e ) {
60
+ throw new \RuntimeException ('Could not decode JSON response from prunner API: ' . $ e ->getMessage (), 1707485801 );
61
+ }
57
62
return PipelinesAndJobsResponse::fromJsonArray ($ result );
58
63
}
59
64
65
+ /**
66
+ * @throws \JsonException
67
+ */
60
68
public function loadJobDetail (JobId $ jobId ): ?Job
61
69
{
62
70
$ resultString = $ this ->apiCall ('GET ' , 'job/detail? ' . http_build_query (['id ' => $ jobId ->getId ()]), null )->getBody ()->getContents ();
63
- $ result = json_decode ($ resultString , true );
71
+ try {
72
+ $ result = json_decode ($ resultString , true , 512 , JSON_THROW_ON_ERROR );
73
+ } catch (\JsonException $ e ) {
74
+ throw new \RuntimeException ('Could not decode JSON response from prunner API: ' . $ e ->getMessage (), 1707485815 );
75
+ }
64
76
if (isset ($ result ['error ' ])) {
65
77
return null ;
66
78
}
@@ -70,22 +82,33 @@ public function loadJobDetail(JobId $jobId): ?Job
70
82
public function loadJobLogs (JobId $ jobId , string $ taskName ): JobLogs
71
83
{
72
84
$ resultString = $ this ->apiCall ('GET ' , 'job/logs? ' . http_build_query (['id ' => $ jobId ->getId (), 'task ' => $ taskName ]), null )->getBody ()->getContents ();
73
- $ result = json_decode ($ resultString , true );
85
+ try {
86
+ $ result = json_decode ($ resultString , true , 512 , JSON_THROW_ON_ERROR );
87
+ } catch (\JsonException $ e ) {
88
+ throw new \RuntimeException ('Could not decode JSON response from prunner API: ' . $ e ->getMessage (), 1707485821 );
89
+ }
74
90
return JobLogs::fromJsonArray ($ result );
75
91
}
76
92
93
+ /**
94
+ * @throws \JsonException
95
+ */
77
96
public function schedulePipeline (PipelineName $ pipeline , array $ variables ): JobId
78
97
{
79
98
$ response = $ this ->apiCall ('POST ' , 'pipelines/schedule ' , json_encode ([
80
99
'pipeline ' => $ pipeline ->getName (),
81
100
'variables ' => $ variables
82
- ], JSON_FORCE_OBJECT ));
101
+ ], JSON_THROW_ON_ERROR | JSON_FORCE_OBJECT ));
83
102
if ($ response ->getStatusCode () !== 202 ) {
84
103
throw new \RuntimeException ('Scheduling a new pipeline run should have returned status code 202, but got: ' . $ response ->getStatusCode ());
85
104
}
86
105
$ contents = $ response ->getBody ()->getContents ();
87
- $ tmp = json_decode ($ contents , true );
88
- return JobId::create ($ tmp ['jobId ' ]);
106
+ try {
107
+ $ result = json_decode ($ contents , true , 512 , JSON_THROW_ON_ERROR );
108
+ } catch (\JsonException $ e ) {
109
+ throw new \RuntimeException ('Could not decode JSON response from prunner API: ' . $ e ->getMessage (), 1707485793 );
110
+ }
111
+ return JobId::create ($ result ['jobId ' ]);
89
112
}
90
113
91
114
public function cancelJob (Job $ job ): void
@@ -99,9 +122,7 @@ public function cancelJob(Job $job): void
99
122
/**
100
123
* Low-Level method, handling only the authentication.
101
124
*
102
- * @param string $method
103
- * @param string $subpath
104
- * @param string|null $body
125
+ * @throws GuzzleException
105
126
*/
106
127
public function apiCall (string $ method , string $ subpath , ?string $ body ): ResponseInterface
107
128
{
@@ -122,9 +143,6 @@ public function apiCall(string $method, string $subpath, ?string $body): Respons
122
143
return $ client ->request ($ method , $ url , ['headers ' => ['Authorization ' => 'Bearer ' . $ authToken ], 'body ' => $ body , 'http_errors ' => false ]);
123
144
}
124
145
125
- /**
126
- * @return string
127
- */
128
146
private function loadJwtSecretFromConfigFile (): string
129
147
{
130
148
if ($ this ->configFile && file_exists ($ this ->configFile )) {
0 commit comments