1+ <?php namespace Atomx \Resources ;
2+
3+ use Atomx \ApiException ;
4+ use Atomx \AtomxClient ;
5+ use Atomx \ReportStreamer ;
6+ use GuzzleHttp \Message \Response ;
7+ use GuzzleHttp \Stream \Stream ;
8+
9+ class Report extends AtomxClient {
10+ private $ returnStream = false ;
11+
12+ // Run report
13+ public function run ($ json )
14+ {
15+ return $ this ->postUrl ('report ' , compact ('json ' ));
16+ }
17+
18+ public function status ($ reportId )
19+ {
20+ return $ this ->getUrl ('report/ ' . $ reportId , ['status ' => true ]);
21+ }
22+
23+ public function isReady ($ report )
24+ {
25+ if (isset ($ report ['report ' ]['is_ready ' ])) {
26+ return $ report ['report ' ]['is_ready ' ];
27+ }
28+
29+ return false ;
30+ }
31+
32+ public function download ($ report )
33+ {
34+ $ reportId = $ report ['report ' ]['id ' ];
35+ $ stream = Stream::factory ();
36+
37+ $ this ->returnStream = true ;
38+
39+ $ stream = $ this ->getUrl ('report/ ' . $ reportId , [], [
40+ // 'save_to' => $stream,
41+ // 'timeout' => 0,
42+ // 'connect_timeout' => 0
43+ ]);
44+
45+ $ this ->returnStream = false ;
46+ return new ReportStreamer ($ stream );
47+ }
48+
49+ protected function handleResponse (Response $ response )
50+ {
51+ if ($ response ->getStatusCode () == 200 ) {
52+ $ stream = $ response ->getBody ();
53+
54+ if ($ this ->returnStream == true )
55+ return $ stream ;
56+ else
57+ return $ stream ->getContents ();
58+ }
59+
60+ throw new ApiException ('Request failed, received the following status: ' .
61+ $ response ->getStatusCode () . ' Body: ' . $ response ->getBody ()->getContents ());
62+ }
63+ }
0 commit comments