1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CanalTP\AbstractGuzzle; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Request; |
6
|
|
|
use GuzzleHttp\Psr7\Response; |
7
|
|
|
use GuzzleHttp\Client; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Guzzle |
11
|
|
|
* @package CanalTP\AbstractGuzzle |
12
|
|
|
* |
13
|
|
|
* @method Client getClient() |
14
|
|
|
* @method Guzzle setClient(Client $client) |
15
|
|
|
*/ |
16
|
|
|
abstract class Guzzle |
17
|
|
|
{ |
18
|
|
|
protected $defaultOptions; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Init Guzzle client with base url. |
22
|
|
|
* |
23
|
|
|
* @param $baseUri |
24
|
|
|
* @param array $option |
25
|
|
|
*/ |
26
|
|
|
abstract public function __construct($baseUri, $option = []); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
abstract public function getBaseUri(); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $baseUri |
35
|
|
|
* |
36
|
|
|
* @return self |
37
|
|
|
*/ |
38
|
|
|
abstract public function setBaseUri($baseUri); |
39
|
|
|
|
40
|
|
|
abstract public function setDefaultOptions($options = []); |
41
|
|
|
|
42
|
|
|
abstract public function getDefaultOptions(); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* This is a shortcut to set auth headers and allow you to be client version proof |
46
|
|
|
* |
47
|
|
|
* @param string $username |
48
|
|
|
* @param string $password |
49
|
|
|
* @param string $type |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
|
|
abstract public function setDefaultAuth($username, $password, $type); |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param Request $request |
56
|
|
|
* |
57
|
|
|
* @return Response |
58
|
|
|
*/ |
59
|
|
|
abstract public function send(Request $request); |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $method |
63
|
|
|
* @param string $uri |
64
|
|
|
* @param string[] $headers |
65
|
|
|
* @param string $body |
66
|
|
|
* |
67
|
|
|
* @return Response |
68
|
|
|
*/ |
69
|
|
|
public function call($method, $uri, $headers = array(), $body = null) |
70
|
|
|
{ |
71
|
|
|
$request = new Request($method, $uri, $headers, $body); |
72
|
|
|
|
73
|
|
|
return $this->send($request); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $uri |
78
|
|
|
* @param string[] $headers |
79
|
|
|
* @param string $body |
80
|
|
|
* |
81
|
|
|
* @return Response |
82
|
|
|
*/ |
83
|
|
|
public function get($uri, $headers = array(), $body = null) |
84
|
|
|
{ |
85
|
|
|
return $this->call('get', $uri, $headers, $body); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $uri |
90
|
|
|
* @param string[] $headers |
91
|
|
|
* @param string $body |
92
|
|
|
* |
93
|
|
|
* @return Response |
94
|
|
|
*/ |
95
|
|
|
public function post($uri, $headers = array(), $body = null) |
96
|
|
|
{ |
97
|
|
|
return $this->call('post', $uri, $headers, $body); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $uri |
102
|
|
|
* @param string[] $headers |
103
|
|
|
* @param string $body |
104
|
|
|
* |
105
|
|
|
* @return Response |
106
|
|
|
*/ |
107
|
|
|
public function put($uri, $headers = array(), $body = null) |
108
|
|
|
{ |
109
|
|
|
return $this->call('put', $uri, $headers, $body); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $uri |
114
|
|
|
* @param string[] $headers |
115
|
|
|
* @param string $body |
116
|
|
|
* |
117
|
|
|
* @return Response |
118
|
|
|
*/ |
119
|
|
|
public function patch($uri, $headers = array(), $body = null) |
120
|
|
|
{ |
121
|
|
|
return $this->call('patch', $uri, $headers, $body); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $uri |
126
|
|
|
* @param string[] $headers |
127
|
|
|
* @param string $body |
128
|
|
|
* |
129
|
|
|
* @return Response |
130
|
|
|
*/ |
131
|
|
|
public function delete($uri, $headers = array(), $body = null) |
132
|
|
|
{ |
133
|
|
|
return $this->call('delete', $uri, $headers, $body); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $uri |
138
|
|
|
* @param string[] $headers |
139
|
|
|
* @param string $body |
140
|
|
|
* |
141
|
|
|
* @return Response |
142
|
|
|
*/ |
143
|
|
|
public function head($uri, $headers = array(), $body = null) |
144
|
|
|
{ |
145
|
|
|
return $this->call('head', $uri, $headers, $body); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|