|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Napp\Core\Api\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Auth\AuthManager; |
|
6
|
|
|
use Illuminate\Http\JsonResponse; |
|
7
|
|
|
use Illuminate\Http\Response; |
|
8
|
|
|
use Napp\Core\Api\Exceptions\Exceptions\ApiInternalCallException; |
|
9
|
|
|
use Napp\Core\Api\Router\Router; |
|
10
|
|
|
use Napp\Core\Api\Auth\NappHttpHeaders; |
|
11
|
|
|
|
|
12
|
|
|
class ApiInternalController extends BaseController |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var AuthManager |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $auth; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var Router |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $internalApiRouter; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param AuthManager $auth |
|
26
|
|
|
* @param Router $router |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(AuthManager $auth, Router $router) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->auth = $auth; |
|
31
|
|
|
$this->internalApiRouter = $router; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return Router |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getInternalRouter(): Router |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->internalApiRouter; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $uri |
|
44
|
|
|
* @param array $headers |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
|
|
public function get(string $uri, array $headers = []): array |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->formatResponse( |
|
50
|
|
|
$this->getInternalRouter()->get($uri, [], $this->getInternalCallHeaders($headers)) |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $uri |
|
56
|
|
|
* @param array $data |
|
57
|
|
|
* @param array $headers |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
|
|
public function post(string $uri, array $data, array $headers = []): array |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->formatResponse( |
|
63
|
|
|
$this->getInternalRouter()->post($uri, $data, $this->getInternalCallHeaders($headers)) |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string $uri |
|
69
|
|
|
* @param array $data |
|
70
|
|
|
* @param array $headers |
|
71
|
|
|
* @return array |
|
72
|
|
|
*/ |
|
73
|
|
|
public function put(string $uri, array $data, array $headers = []): array |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->formatResponse( |
|
76
|
|
|
$this->getInternalRouter()->put($uri, $data, $this->getInternalCallHeaders($headers)) |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $uri |
|
82
|
|
|
* @param array $headers |
|
83
|
|
|
* @return array |
|
84
|
|
|
*/ |
|
85
|
|
|
public function delete(string $uri, array $headers = []): array |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->formatResponse( |
|
88
|
|
|
$this->getInternalRouter()->delete($uri, [], $this->getInternalCallHeaders($headers)) |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param array $headers |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function getInternalCallHeaders(array $headers): array |
|
97
|
|
|
{ |
|
98
|
|
|
return array_merge($headers, [ |
|
99
|
|
|
NappHttpHeaders::NAPP_API_CALL_TYPE => 'internal', |
|
100
|
|
|
NappHttpHeaders::NAPP_AUTH_GLOBAL_USER_ID => $this->auth->guard()->id(), |
|
101
|
|
|
]); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param Response|JsonResponse $response |
|
106
|
|
|
* @return array |
|
107
|
|
|
* @throws ApiInternalCallException |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function formatResponse($response): array |
|
110
|
|
|
{ |
|
111
|
|
|
if (true === $response instanceof JsonResponse) { |
|
112
|
|
|
$data = $response->getData(true); |
|
113
|
|
|
} else { |
|
114
|
|
|
$data = json_decode($response->getContent(), true); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
if (true === array_key_exists('error', $data)) { |
|
118
|
|
|
if (true === config('app.debug')) { |
|
119
|
|
|
$message = json_encode($data['error']); |
|
120
|
|
|
} else { |
|
121
|
|
|
$message = $data['error']['message']; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
throw new ApiInternalCallException($response, $message); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $data; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|