-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiResponseTrait.php
More file actions
210 lines (189 loc) · 6.34 KB
/
Copy pathApiResponseTrait.php
File metadata and controls
210 lines (189 loc) · 6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
namespace App\Http\Traits;
use Illuminate\Support\Arr;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\ResourceCollection;
trait ApiResponseTrait
{
/**
* Return generic json response with the given data.
*
* @param $data
* @param int $statusCode
* @param array $headers
*
* @return \Illuminate\Http\JsonResponse
*/
protected function apiResponse($data = [], $statusCode = 200, $headers = [])
{
// This bit is a little to hacky but acheives what it needs to
if (!isset($data['result'])) {
$data['result'] = (object)[];
}
if (!isset($data['result']->meta)) {
$data['result']->meta = (object)[];
}
// Add the meta data to every request
$data['result']->meta->company = [
'website' => env('APP_URL', 'Api'),
'email' => env('MAIL_FROM_ADDRESS', 'api@adminui.co.uk'),
];
$data['result']->meta->api = [
'version' => config('api-version', '1.0.1'),
'author' => 'https://www.adminui.co.uk',
'email' => 'support@adminui.co.uk',
];
// Build response structure
$responseStructure = [
'status' => $data['success'] ?? 'failed',
'message' => $data['message'] ?? null,
'data' => $data['result']->data ?? null,
'links' => $data['result']->links ?? null,
'meta' => $data['result']->meta ?? null,
];
// Are there any errors
if (isset($data['errors'])) {
$responseStructure['errors'] = $data['errors'];
}
// Get status code
if (isset($data['status'])) {
$statusCode = $data['status'];
}
// Is there any exceptions set
if (isset($data['exception']) && ($data['exception'] instanceof \Error || $data['exception'] instanceof \Exception)) {
// restrict what is sent back to avoid displaying whole stack
$responseStructure['exception'] = [
'message' => $data['exception']->getMessage(),
'file' => $data['exception']->getFile(),
'line' => $data['exception']->getLine(),
'code' => $data['exception']->getCode()
];
// Set status code of 500
if ($statusCode === 200) {
$statusCode = 500;
}
}
// if success is not set throw error code (from handler)
if ($data['success'] === false) {
if (isset($data['error_code'])) {
$responseStructure['error_code'] = $data['error_code'];
} else {
$responseStructure['error_code'] = 1;
}
}
// return the json
return response()->json(
$responseStructure, $statusCode, $headers
);
}
/*
* For a single resource result
*/
protected function respondWithResource(JsonResource $resource, $message = null, $statusCode = 200, $headers = [])
{
return $this->apiResponse(
[
'success' => 'success',
'result' => $resource->response()->getData(),
'message' => $message
], $statusCode, $headers
);
}
/*
* For a collection result
*/
protected function respondWithResourceCollection(ResourceCollection $resourceCollection, $message = null, $statusCode = 200, $headers = [])
{
return $this->apiResponse(
[
'success' => 'success',
'result' => $resourceCollection->response()->getData(),
'message' => $message
], $statusCode, $headers
);
}
/**
* Respond with error.
*
* @param $message
* @param int $statusCode
*
* @param \Exception|null $exception
* @param bool|null $error_code
* @return \Illuminate\Http\JsonResponse
*/
protected function respondError($message, int $statusCode = 400, \Exception $exception = null, int $error_code = 1)
{
return $this->apiResponse(
[
'success' => 'failed',
'message' => $message ?? null,
'exception' => $exception,
'error_code' => $error_code
], $statusCode
);
}
/**
* Respond with success.
* @param string $message
* @return \Illuminate\Http\JsonResponse
*/
protected function respondSuccess($message = '')
{
return $this->apiResponse(['success' => 'success', 'message' => $message]);
}
/**
* Respond with created.
* @param $data
* @return \Illuminate\Http\JsonResponse
*/
protected function respondCreated($data)
{
return $this->apiResponse($data, 201);
}
/**
* Respond with no content
* @param string $message
* @return \Illuminate\Http\JsonResponse
*/
protected function respondNoContent($message = 'No Content Found')
{
return $this->apiResponse(['success' => 'success', 'message' => $message], 200);
}
/**
* Respond with unauthorized.
* @param string $message
* @return \Illuminate\Http\JsonResponse
*/
protected function respondUnAuthorized($message = 'Unauthorized')
{
return $this->respondError($message, 401);
}
/**
* Respond with forbidden.
* @param string $message
* @return \Illuminate\Http\JsonResponse
*/
protected function respondForbidden($message = 'Forbidden')
{
return $this->respondError($message, 403);
}
/**
* Respond with not found.
* @param string $message
* @return \Illuminate\Http\JsonResponse
*/
protected function respondNotFound($message = 'Not Found')
{
return $this->respondError($message, 404);
}
/**
* Respond with internal error.
* @param string $message
* @return \Illuminate\Http\JsonResponse
*/
protected function respondInternalError($message = 'Internal Error')
{
return $this->respondError($message, 500);
}
}