1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ijeffro\Laralocker\LearningLocker\API; |
4
|
|
|
|
5
|
|
|
use Config; |
6
|
|
|
use Ijeffro\Laralocker\LearningLocker\Connection; |
7
|
|
|
|
8
|
|
|
class APIHandler extends Connection { |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Learning Locker: Requst URL |
12
|
|
|
* |
13
|
|
|
* @return $response |
|
|
|
|
14
|
|
|
*/ |
15
|
|
|
public function request($url) { |
16
|
|
|
try { |
17
|
|
|
$response = $this->client()->get($url, [ |
18
|
|
|
'auth' => $this->auth(), |
19
|
|
|
'headers' => $this->headers() |
20
|
|
|
]); |
21
|
|
|
if ( $response->getStatusCode() === 404 ) |
22
|
|
|
throw new ClientException(404); |
|
|
|
|
23
|
|
|
|
24
|
|
|
return $response->getBody()->getContents(); |
25
|
|
|
} catch (ClientException $e) { |
26
|
|
|
return $e; |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function save($url, $data) |
31
|
|
|
{ |
32
|
|
|
try { |
33
|
|
|
$data = json_encode($data); |
34
|
|
|
$response = $this->client()->patch($url, [ |
35
|
|
|
'auth' => $this->auth(), |
36
|
|
|
'headers' => $this->headers(), |
37
|
|
|
'body' => $data |
38
|
|
|
]); |
39
|
|
|
if ( $response->getStatusCode() === 404 ) |
40
|
|
|
throw new ClientException(404); |
41
|
|
|
|
42
|
|
|
return $response->getBody()->getContents(); |
43
|
|
|
} catch (ClientException $e) { |
44
|
|
|
return $e; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function destroy($url) |
49
|
|
|
{ |
50
|
|
|
try { |
51
|
|
|
$response = $this->client()->delete($url, [ |
52
|
|
|
'auth' => $this->auth(), |
53
|
|
|
'headers' => $this->headers() |
54
|
|
|
]); |
55
|
|
|
if ( $response->getStatusCode() === 404 ) |
56
|
|
|
throw new ClientException(404); |
57
|
|
|
|
58
|
|
|
return $response->getBody()->getContents(); |
59
|
|
|
} catch (ClientException $e) { |
60
|
|
|
return $e; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function make($url, $data = null) |
65
|
|
|
{ |
66
|
|
|
try { |
67
|
|
|
$data = json_encode($data); |
68
|
|
|
$response = $this->client()->post($url, [ |
69
|
|
|
'auth' => $this->auth(), |
70
|
|
|
'headers' => $this->headers(), |
71
|
|
|
'body' => $data |
72
|
|
|
]); |
73
|
|
|
return $response->getBody()->getContents(); |
74
|
|
|
} catch (ClientException $e) { |
75
|
|
|
return $e; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function select($selected = [], $response) |
80
|
|
|
{ |
81
|
|
|
if (!is_array($selected) || !$response) { |
82
|
|
|
$error = [ "error" => "Select must be an array."]; |
83
|
|
|
return json_encode($error); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($selected) { |
|
|
|
|
87
|
|
|
$response = (array) json_decode($response); |
88
|
|
|
$items = []; |
89
|
|
|
|
90
|
|
|
foreach($selected as $select) { |
91
|
|
|
$search = array_key_exists($select, $response); |
92
|
|
|
|
93
|
|
|
if ($search === true) { |
94
|
|
|
$items[$select] = $response[$select]; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$response = json_encode($items); |
99
|
|
|
return $response; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $response; |
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function checkConnection() |
107
|
|
|
{ |
108
|
|
|
try { |
109
|
|
|
$domain = Config::get("laralocker.learning_locker.api.url"); |
110
|
|
|
$url = $domain . '/api'; |
111
|
|
|
$response = $this->client()->get($url, [ |
112
|
|
|
'auth' => $this->auth(), |
113
|
|
|
'headers' => $this->headers() |
114
|
|
|
]); |
115
|
|
|
|
116
|
|
|
if ($response->getStatusCode() === 200) { |
117
|
|
|
return true; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if ( $response->getStatusCode() === 404 ) |
121
|
|
|
throw new ClientException(404); |
122
|
|
|
|
123
|
|
|
return $response->getBody()->getContents(); |
124
|
|
|
} catch (ClientException $e) { |
125
|
|
|
return $e; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
// dd($this->headers()); |
129
|
|
|
// $response = $client->get('http://httpbin.org/get'); |
130
|
|
|
// $response = $client->delete('http://httpbin.org/delete'); |
131
|
|
|
// $response = $client->head('http://httpbin.org/get'); |
132
|
|
|
// $response = $client->options('http://httpbin.org/get'); |
133
|
|
|
// $response = $client->patch('http://httpbin.org/patch'); |
134
|
|
|
// $response = $client->post('http://httpbin.org/post'); |
135
|
|
|
// $response = $client->put('http://httpbin.org/put'); |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|