-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
124 lines (101 loc) · 3.13 KB
/
index.php
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
<?php
declare(strict_types=1);
class Client
{
private $base_endpoint_url = '';
public function __construct()
{
spl_autoload_register([$this, 'autoload']);
// Avoids hardcoding the api URL, though that'd probably be better done in some config file
$this->base_endpoint_url = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . Helpers::getBaseUri() . '/api';
}
private function autoload($class_name)
{
if (file_exists(__DIR__ . '/' . $class_name . ".php"))
{
require_once __DIR__ . '/' . $class_name . ".php";
}
}
private function get_jwt_token(string $username, string $password): ?string
{
$request_data = [
'username' => $username,
'password' => $password,
];
$request_url = $this->base_endpoint_url . "/login";
try
{
$response = Requests::post_json($request_url, $request_data);
}
catch (RequestsInvalidJSONException $e)
{
return null;
}
if ($response['status_code'] != 200 || $response['content']['success'] === false)
{
return null;
}
return $response['content']['token'];
}
private function make_data_query(string $request_url, array $request_data, string $jwt_token): string
{
$request_headers = [
'Authorization: Bearer ' . $jwt_token,
];
try
{
$response = Requests::get_json($request_url, $request_data, $request_headers);
}
catch (RequestsInvalidJSONException $e)
{
return "Response content not valid JSON";
}
if ($response['content']['success'] === false)
{
return "Error: ". $response['content']['error'];
}
return json_encode($response['content'], JSON_PRETTY_PRINT);
}
public function run(): Response
{
$data = [
'base_endpoint_url' => $this->base_endpoint_url,
// For this sample task these credentials are hardcoded in AuthProvider.
'jwt_username' => 'testuser',
'jwt_password' => 'password',
'books_isbn' => Input::get('isbn', '9780330508117'),
'books_response' => '',
'movies_title' => Input::get('title', 'Iron Sky'),
'movies_year' => Input::get('year', '2012'),
'movies_plot' => Input::get('plot', 'full'),
'movies_response' => '',
];
$data['jwt_token'] = $this->get_jwt_token($data['jwt_username'], $data['jwt_password']);
// If auth token could not be obtained then making the requests won't be possible.
if (!is_null($data['jwt_token']))
{
// Retrieve book info by ISBN.
$request_url = $this->base_endpoint_url . "/getBook";
$request_data = [
'isbn' => $data['books_isbn'],
];
$data['books_response'] = $this->make_data_query($request_url, $request_data, $data['jwt_token']);
// Retrieve movie info by title and year.
$request_url = $this->base_endpoint_url . "/getMovie";
$request_data = [
'title' => $data['movies_title'],
'year' => $data['movies_year'],
'plot' => $data['movies_plot'],
];
$data['movies_response'] = $this->make_data_query($request_url, $request_data, $data['jwt_token']);
}
return new Response(
Response::STATUS_OK,
Response::CONTENT_TYPE_HTML,
View::load("Main.php", $data)
);
}
}
$client = new Client();
$response = $client->run();
$response->output();