1 | <?php |
||||
2 | |||||
3 | /* |
||||
4 | * @author Xavier Chopin <[email protected]> |
||||
5 | * |
||||
6 | * For the full copyright and license information, please view the LICENSE |
||||
7 | * file that was distributed with this source code. |
||||
8 | */ |
||||
9 | |||||
10 | namespace App\Model\API; |
||||
11 | |||||
12 | use App\Controller\AbstractController as Provider; |
||||
13 | use GuzzleHttp\Exception\GuzzleException; |
||||
14 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
||||
15 | |||||
16 | abstract class ApiModel |
||||
17 | { |
||||
18 | |||||
19 | /** |
||||
20 | * Generic function to GET /api/ OpenLRW routes |
||||
21 | * |
||||
22 | * @param String $route |
||||
23 | * @param String $prefix |
||||
24 | * @return mixed|object |
||||
25 | * @throws \Exception |
||||
26 | */ |
||||
27 | public static function get(String $route) |
||||
28 | { |
||||
29 | try { |
||||
30 | return json_decode(Provider::$http->request( |
||||
31 | 'GET', |
||||
32 | 'api/' . $route, |
||||
33 | [ |
||||
34 | 'headers' => [ |
||||
35 | 'X-Requested-With' => 'XMLHttpRequest', |
||||
36 | 'Authorization' => 'Bearer ' . Provider::makeJWT() |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
37 | ] |
||||
38 | ]) |
||||
39 | ->getBody() |
||||
40 | ->getContents() |
||||
41 | ); |
||||
42 | } catch (GuzzleException $e) { |
||||
43 | if ($e->getCode() == 401) { |
||||
44 | try { |
||||
45 | Provider::generateJwt(); |
||||
46 | } catch (GuzzleException $e) { |
||||
47 | die($e->getMessage()); |
||||
0 ignored issues
–
show
|
|||||
48 | } |
||||
49 | self::get($route); |
||||
50 | } else if ($e->getCode() == 404) { |
||||
51 | return null; |
||||
52 | } else { |
||||
53 | throw new \Exception($e); |
||||
0 ignored issues
–
show
$e of type GuzzleHttp\Exception\GuzzleException is incompatible with the type string expected by parameter $message of Exception::__construct() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
54 | } |
||||
55 | return null; |
||||
56 | } |
||||
57 | |||||
58 | } |
||||
59 | |||||
60 | |||||
61 | /** |
||||
62 | * Generic function to POST OpenLRW routes |
||||
63 | * |
||||
64 | * @param String $route |
||||
65 | * @param array $args |
||||
66 | * @return mixed|object |
||||
67 | * @throws \GuzzleHttp\Exception\GuzzleException |
||||
68 | */ |
||||
69 | public function post(String $route, array $args) // ToDo : add JWT |
||||
70 | { |
||||
71 | return json_decode( self::$http->request('POST', $route, [ |
||||
0 ignored issues
–
show
|
|||||
72 | 'headers' => [ 'X-Requested-With' => 'XMLHttpRequest' ], |
||||
73 | 'json' => $args |
||||
74 | ])->getBody()->getContents()); |
||||
75 | } |
||||
76 | |||||
77 | |||||
78 | |||||
79 | |||||
80 | } |