Skip to content

Commit 16096b7

Browse files
committed
Update End Point And Status Message
1 parent 1431e74 commit 16096b7

File tree

6 files changed

+95
-27
lines changed

6 files changed

+95
-27
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3394,3 +3394,4 @@ old/vendor/vnn/wordpress-rest-api-client/src/Endpoint/Tags.php
33943394
old/vendor/vnn/wordpress-rest-api-client/src/Endpoint/Users.php
33953395
old/vendor/vnn/wordpress-rest-api-client/src/Http/ClientInterface.php
33963396
old/vendor/vnn/wordpress-rest-api-client/src/Http/GuzzleAdapter.php
3397+
s.json

app/Endpoint/AbstractWpEndpoint.php

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

app/Endpoint/Categories.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Reactmore\WordpressClient\Endpoint;
4+
5+
/**
6+
* Class Posts
7+
* @package Reactmore\WordpressClient\Endpoint
8+
*/
9+
class Categories extends AbstractWpEndpoint
10+
{
11+
/**
12+
* {@inheritdoc}
13+
*/
14+
protected function getEndpoint()
15+
{
16+
return '/wp-json/wp/v2/categories';
17+
}
18+
}

app/Endpoint/Tags.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Reactmore\WordpressClient\Endpoint;
4+
5+
/**
6+
* Class Posts
7+
* @package Reactmore\WordpressClient\Endpoint
8+
*/
9+
class Tags extends AbstractWpEndpoint
10+
{
11+
/**
12+
* {@inheritdoc}
13+
*/
14+
protected function getEndpoint()
15+
{
16+
return '/wp-json/wp/v2/tags';
17+
}
18+
}

app/Request/GuzzleAdapter.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,24 @@ public static function handleException($exception)
5252
'error' => 'Connection Timeout Error. Please check your internet connection and try again'
5353
], 408, 'failed');
5454
} else {
55+
$error = json_decode($exception->getResponse()->getBody()->getContents(), true);
5556
return ResponseFormatter::formatResponse([
56-
'error' => $exception->getMessage()
57+
'error' => $error['message'],
58+
'term_id' => $error['data']['term_id']
59+
], 400, 'failed');
60+
}
61+
}
62+
63+
public static function RequestHandleException($exception)
64+
{
65+
if ($exception instanceof ConnectException) {
66+
return ResponseFormatter::formatResponse([
67+
'error' => 'Connection Timeout Error. Please check your internet connection and try again'
68+
], 408, 'failed');
69+
} else {
70+
$error = json_decode($exception->getResponse()->getBody()->getContents(), true);
71+
return ResponseFormatter::formatResponse([
72+
'error' => $error['message']
5773
], 400, 'failed');
5874
}
5975
}

app/Wordpress.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
use Psr\Http\Message\RequestInterface;
66
use Psr\Http\Message\ResponseInterface;
7-
use RuntimeException;
8-
use Reactmore\WordpressClient\Auth\AuthInterface;
97
use Reactmore\WordpressClient\Endpoint;
8+
use Reactmore\WordpressClient\Auth\AuthInterface;
109
use Reactmore\WordpressClient\Request\ClientInterface;
10+
use RuntimeException;
1111

1212
/**
1313
* Class Wordpress
@@ -86,10 +86,13 @@ public function send(RequestInterface $request)
8686
$request = $this->credentials->addCredentials($request);
8787
}
8888

89+
90+
8991
$request = $request->withUri(
9092
$this->httpClient->makeUri($this->wordpressUrl . $request->getUri())
9193
);
9294

95+
9396
return $this->httpClient->send($request);
9497
}
9598
}

0 commit comments

Comments
 (0)