|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace MedianetDev\PConnector; |
|
4
|
|
|
|
|
5
|
|
|
use MedianetDev\PConnector\Concerns\Configurations; |
|
6
|
|
|
use MedianetDev\PConnector\Concerns\Requests; |
|
7
|
|
|
use MedianetDev\PConnector\Concerns\Utils; |
|
8
|
|
|
|
|
9
|
|
|
class PConnector |
|
10
|
|
|
{ |
|
11
|
|
|
use Configurations; |
|
12
|
|
|
use Requests; |
|
13
|
|
|
use Utils; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var \MedianetDev\PConnector\Contracts\Http |
|
17
|
|
|
* |
|
18
|
|
|
* The http client |
|
19
|
|
|
*/ |
|
20
|
|
|
private $httpClient; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(\MedianetDev\PConnector\Contracts\Http $httpClient) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->httpClient = $httpClient; |
|
25
|
|
|
|
|
26
|
|
|
$this->profile(config('p-connector.default_profile')); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Send a request. |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $path [EX: 'posts'] |
|
33
|
|
|
* @param array|string $data The query data |
|
34
|
|
|
* @param string $method |
|
35
|
|
|
* @return \MedianetDev\PConnector\PConnector |
|
36
|
|
|
*/ |
|
37
|
|
|
public function send(string $path = '', $data = [], string $method = 'GET') |
|
38
|
|
|
{ |
|
39
|
|
|
if (config('p-connector.profiles.'.$this->profile.'.request.enable_localization', config('p-connector.request.enable_localization', true)) && ! array_key_exists('Accept-Language', $this->headers)) { |
|
40
|
|
|
$this->lang(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$this->loadResponse($this->httpClient->send(build_url($path, $this->profile, $this->url), $data, $method, $this->profile, $this->withAuthentication, $this->headers, $this->withJson)); |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
if ($this->status && |
|
46
|
|
|
$this->withAuthentication && |
|
47
|
|
|
in_array($this->response['status_code'], config('p-connector.profiles.'.$this->profile.'.auth.re_auth_on_codes', config('p-connector.auth.re_auth_on_codes', [401]))) |
|
48
|
|
|
) { |
|
49
|
|
|
AuthManager::deleteTokenFor($this->profile); |
|
50
|
|
|
$this->loadResponse($this->httpClient->send(build_url($path, $this->profile, $this->url), $data, $method, $this->profile, $this->withAuthentication, $this->headers, $this->withJson)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if ($this->allowDebugging) { |
|
54
|
|
|
$this->log(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Logout the profile by deleting it's token from database. |
|
62
|
|
|
* |
|
63
|
|
|
* @return void |
|
64
|
|
|
*/ |
|
65
|
|
|
public function logout() |
|
66
|
|
|
{ |
|
67
|
|
|
AuthManager::deleteTokenFor($this->profile); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|