1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Laravel Paga package. |
5
|
|
|
* |
6
|
|
|
* (c) Henry Ugochukwu <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Phalconvee\Paga\Services; |
13
|
|
|
|
14
|
|
|
use GuzzleHttp\Client; |
15
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
16
|
|
|
use Phalconvee\Paga\Exceptions\IsNullException; |
17
|
|
|
|
18
|
|
|
class GuzzleRequestService |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var Client |
22
|
|
|
*/ |
23
|
|
|
protected $client; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* GuzzleRequestService constructor. |
27
|
|
|
* |
28
|
|
|
* @param $base_url |
29
|
|
|
* @param $hash |
30
|
|
|
* @param $publicKey |
31
|
|
|
* @param $secretKey |
32
|
|
|
*/ |
33
|
|
|
public function __construct($base_url, $hash, $publicKey, $secretKey) |
34
|
|
|
{ |
35
|
|
|
$this->client = new Client([ |
36
|
|
|
'base_uri' => $base_url, |
37
|
|
|
'headers' => [ |
38
|
|
|
'Content-Type' => 'application/json', |
39
|
|
|
'Accept' => 'application/json', |
40
|
|
|
'hash' => $hash, |
41
|
|
|
'principal' => $publicKey, |
42
|
|
|
'credentials' => $secretKey, |
43
|
|
|
], |
44
|
|
|
]); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Set options for making the Client request. |
49
|
|
|
* |
50
|
|
|
* @param $method |
51
|
|
|
* @param $url |
52
|
|
|
* @param null $body |
|
|
|
|
53
|
|
|
* |
54
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
55
|
|
|
* @throws IsNullException |
56
|
|
|
* |
57
|
|
|
* @return mixed|\Psr\Http\Message\ResponseInterface|null |
58
|
|
|
*/ |
59
|
|
|
public function makeHttpRequest($method, $url, $body = null) |
60
|
|
|
{ |
61
|
|
|
$response = null; |
62
|
|
|
|
63
|
|
|
if (is_null($method)) { |
64
|
|
|
throw new isNullException('Empty Method Not Allowed'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($method == 'GET') { |
68
|
|
|
$response = $this->doGet($url, $body); |
69
|
|
|
} elseif ($method == 'POST') { |
70
|
|
|
$response = $this->doPostRaw($url, $body); |
71
|
|
|
} elseif ($method == 'MULTIPART') { |
72
|
|
|
$response = $this->doMultiPart($url, $body); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$response = json_decode($response->getBody(), true); |
76
|
|
|
|
77
|
|
|
return $response; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Make GET Client Request. |
82
|
|
|
* |
83
|
|
|
* @param $url |
84
|
|
|
* @param array $body |
85
|
|
|
* |
86
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
87
|
|
|
* |
88
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
89
|
|
|
*/ |
90
|
|
|
private function doGet($url, $body = [ ]) |
91
|
|
|
{ |
92
|
|
|
return $this->client->request('GET', $url, [ |
93
|
|
|
'query' => $body, |
94
|
|
|
]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Make Raw POST Client Request. |
99
|
|
|
* |
100
|
|
|
* @param $url |
101
|
|
|
* @param $body |
102
|
|
|
* |
103
|
|
|
* @throws GuzzleException |
104
|
|
|
* |
105
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
106
|
|
|
*/ |
107
|
|
|
private function doPostRaw($url, $body) |
108
|
|
|
{ |
109
|
|
|
return $this->client->request('POST', $url, [ |
110
|
|
|
'body' => json_encode($body), |
111
|
|
|
]); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Make Multipart Client Request. |
116
|
|
|
* |
117
|
|
|
* @param $url |
118
|
|
|
* @param $multipart |
119
|
|
|
* |
120
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
121
|
|
|
* |
122
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
123
|
|
|
*/ |
124
|
|
|
private function doMultiPart($url, $multipart) |
125
|
|
|
{ |
126
|
|
|
return $this->client->request('POST', $url, $multipart); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|