@@ -60,12 +60,16 @@ public function get($id = null, array $params = null)
6060 $ response ->hasHeader ('Content-Type ' )
6161 && substr ($ response ->getHeader ('Content-Type ' )[0 ], 0 , 16 ) === 'application/json '
6262 ) {
63- $ dataRespon = json_decode ($ response ->getBody ()->getContents (), true );
6463
65- return $ dataRespon ;
64+ return [
65+ 'status ' => 'success ' ,
66+ 'code ' => $ response ->getStatusCode (),
67+ 'data ' => json_decode ($ response ->getBody ()->getContents (), true ),
68+
69+ ];
6670 }
6771 } catch (Exception $ e ) {
68- return GuzzleAdapter::handleException ($ e );
72+ return GuzzleAdapter::RequestHandleException ($ e );
6973 }
7074 }
7175
@@ -97,16 +101,16 @@ public function getPaginations($id = null, array $params = null)
97101 $ response ->hasHeader ('Content-Type ' )
98102 && substr ($ response ->getHeader ('Content-Type ' )[0 ], 0 , 16 ) === 'application/json '
99103 ) {
100- $ dataRespon = [
101- 'body ' => json_decode ($ response ->getBody ()->getContents (), true ),
102- 'total ' => $ response ->getHeader ('X-WP-Total ' ),
103- 'totalpages ' => $ response ->getHeader ('X-WP-TotalPages ' ),
104+ return [
105+ 'status ' => 'success ' ,
106+ 'code ' => $ response ->getStatusCode (),
107+ 'data ' => json_decode ($ response ->getBody ()->getContents (), true ),
108+ 'X-WP-TOTAL ' => $ response ->getHeader ('X-WP-Total ' )[0 ],
109+ 'X-WP-TOTAL-PAGE ' => $ response ->getHeader ('X-WP-TotalPages ' )[0 ],
104110 ];
105-
106- return $ dataRespon ;
107111 }
108112 } catch (Exception $ e ) {
109- return GuzzleAdapter::handleException ($ e );
113+ return GuzzleAdapter::RequestHandleException ($ e );
110114 }
111115 }
112116
@@ -117,23 +121,31 @@ public function getPaginations($id = null, array $params = null)
117121 */
118122 public function save (array $ data )
119123 {
120- $ url = $ this ->getEndpoint ();
124+ try {
125+ $ url = $ this ->getEndpoint ();
121126
122- if (isset ($ data ['id ' ])) {
123- $ url .= '/ ' . $ data ['id ' ];
124- unset($ data ['id ' ]);
125- }
126127
127- $ request = new Request ('POST ' , $ url , ['Content-Type ' => 'application/json ' ], json_encode ($ data ));
128- $ response = $ this ->client ->send ($ request );
128+ if (isset ($ data ['id ' ])) {
129+ $ url .= '/ ' . $ data ['id ' ];
130+ unset($ data ['id ' ]);
131+ }
129132
130- if (
131- $ response ->hasHeader ('Content-Type ' )
132- && substr ($ response ->getHeader ('Content-Type ' )[0 ], 0 , 16 ) === 'application/json '
133- ) {
134- return json_decode ($ response ->getBody ()->getContents (), true );
135- }
133+ $ request = new Request ('POST ' , $ url , ['Content-Type ' => 'application/json ' ], json_encode ($ data ));
134+ $ response = $ this ->client ->send ($ request );
136135
137- throw new RuntimeException ('Unexpected response ' );
136+
137+ if (
138+ $ response ->hasHeader ('Content-Type ' )
139+ && substr ($ response ->getHeader ('Content-Type ' )[0 ], 0 , 16 ) === 'application/json '
140+ ) {
141+ return [
142+ 'status ' => 'success ' ,
143+ 'code ' => $ response ->getStatusCode (),
144+ 'data ' => json_decode ($ response ->getBody ()->getContents (), true )
145+ ];
146+ }
147+ } catch (Exception $ e ) {
148+ return GuzzleAdapter::handleException ($ e );
149+ }
138150 }
139151}
0 commit comments