1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* This Driver is based entirely on official documentation of the Mattermost Web
|
4
|
|
|
* Services API and you can extend it by following the directives of the documentation.
|
5
|
|
|
*
|
6
|
|
|
* For the full copyright and license information, please read the LICENSE.txt
|
7
|
|
|
* file that was distributed with this source code. For the full list of
|
8
|
|
|
* contributors, visit https://github.com/gnello/php-mattermost-driver/contributors
|
9
|
|
|
*
|
10
|
|
|
* God bless this mess.
|
11
|
|
|
*
|
12
|
|
|
* @author Luca Agnello <[email protected]>
|
13
|
|
|
* @link https://api.mattermost.com/
|
14
|
|
|
*/
|
15
|
|
|
|
16
|
|
|
namespace Gnello\Mattermost;
|
17
|
|
|
|
18
|
|
|
use GuzzleHttp\Client as GuzzleClient;
|
19
|
|
|
use GuzzleHttp\Exception\RequestException;
|
20
|
|
|
use GuzzleHttp\Psr7\Response;
|
21
|
|
|
use GuzzleHttp\RequestOptions;
|
22
|
|
|
use Psr\Http\Message\ResponseInterface;
|
23
|
|
|
use Pimple\Container;
|
24
|
|
|
|
25
|
|
|
/**
|
26
|
|
|
* Class Client
|
27
|
|
|
*
|
28
|
|
|
* @package Gnello\Mattermost
|
29
|
|
|
*/
|
30
|
|
|
class Client
|
31
|
|
|
{
|
32
|
|
|
/**
|
33
|
|
|
* @var string
|
34
|
|
|
*/
|
35
|
|
|
private $baseUri;
|
36
|
|
|
|
37
|
|
|
/**
|
38
|
|
|
* @var array
|
39
|
|
|
*/
|
40
|
|
|
private $headers = [];
|
41
|
|
|
|
42
|
|
|
/**
|
43
|
|
|
* @var GuzzleClient
|
44
|
|
|
*/
|
45
|
|
|
private $client;
|
46
|
|
|
|
47
|
|
|
/**
|
48
|
|
|
* Client constructor.
|
49
|
|
|
*
|
50
|
|
|
* @param Container $container
|
51
|
|
|
*/
|
52
|
|
|
public function __construct(Container $container)
|
53
|
|
|
{
|
54
|
|
|
$guzzleOptions = [];
|
55
|
|
|
if (isset($container['guzzle'])) {
|
56
|
|
|
$guzzleOptions = $container['guzzle'];
|
57
|
|
|
}
|
58
|
|
|
$this->client = new GuzzleClient($guzzleOptions);
|
59
|
|
|
|
60
|
|
|
$options = $container['driver'];
|
61
|
|
|
$this->baseUri = $options['scheme'] . '://' . $options['url'] . $options['basePath'];
|
62
|
|
|
}
|
63
|
|
|
|
64
|
|
|
/**
|
65
|
|
|
* @param $token
|
66
|
|
|
*/
|
67
|
|
|
public function setToken($token)
|
68
|
|
|
{
|
69
|
|
|
$this->headers = ['Authorization' => 'Bearer ' . $token];
|
70
|
|
|
}
|
71
|
|
|
|
72
|
|
|
/**
|
73
|
|
|
* @param $uri
|
74
|
|
|
* @return string
|
75
|
|
|
*/
|
76
|
|
|
private function makeUri($uri)
|
77
|
|
|
{
|
78
|
|
|
return $this->baseUri . $uri;
|
79
|
|
|
}
|
80
|
|
|
|
81
|
|
|
/**
|
82
|
|
|
* @param $options
|
83
|
|
|
* @param $type
|
84
|
|
|
* @return array
|
85
|
|
|
*/
|
86
|
|
|
private function buildOptions($options, $type)
|
87
|
|
|
{
|
88
|
|
|
return [
|
89
|
|
|
RequestOptions::HEADERS => $this->headers,
|
90
|
|
|
$type => $options,
|
91
|
|
|
];
|
92
|
|
|
}
|
93
|
|
|
|
94
|
|
|
/**
|
95
|
|
|
* @param $method
|
96
|
|
|
* @param $uri
|
97
|
|
|
* @param $type
|
98
|
|
|
* @param array $options
|
99
|
|
|
* @return ResponseInterface
|
100
|
|
|
*/
|
101
|
|
|
private function dispatch($method, $uri, $type, array $options = [])
|
102
|
|
|
{
|
103
|
|
|
try {
|
104
|
|
|
$response = $this->client->{$method}($this->makeUri($uri), $this->buildOptions($options, $type));
|
105
|
|
|
} catch (RequestException $e) {
|
106
|
|
|
if ($e->hasResponse()) {
|
107
|
|
|
$response = $e->getResponse();
|
108
|
|
|
} else {
|
109
|
|
|
$response = new Response(500, [], $e->getMessage());
|
110
|
|
|
}
|
111
|
|
|
}
|
112
|
|
|
|
113
|
|
|
return $response;
|
114
|
|
|
}
|
115
|
|
|
|
116
|
|
|
/**
|
117
|
|
|
* @param $uri
|
118
|
|
|
* @param array $options
|
119
|
|
|
* @param string $type
|
120
|
|
|
* @return ResponseInterface
|
121
|
|
|
*/
|
122
|
|
|
public function get($uri, array $options = [], $type = RequestOptions::QUERY)
|
123
|
|
|
{
|
124
|
|
|
return $this->dispatch('get', $uri, $type, $options);
|
125
|
|
|
}
|
126
|
|
|
|
127
|
|
|
/**
|
128
|
|
|
* @param $uri
|
129
|
|
|
* @param array $options
|
130
|
|
|
* @param string $type
|
131
|
|
|
* @return ResponseInterface
|
132
|
|
|
*/
|
133
|
|
|
public function post($uri, $options = [], $type = RequestOptions::JSON)
|
134
|
|
|
{
|
135
|
|
|
return $this->dispatch('post', $uri, $type, $options);
|
136
|
|
|
}
|
137
|
|
|
|
138
|
|
|
/**
|
139
|
|
|
* @param $uri
|
140
|
|
|
* @param array $options
|
141
|
|
|
* @param string $type
|
142
|
|
|
* @return ResponseInterface
|
143
|
|
|
*/
|
144
|
|
|
public function put($uri, $options = [], $type = RequestOptions::JSON)
|
145
|
|
|
{
|
146
|
|
|
return $this->dispatch('put', $uri, $type, $options);
|
147
|
|
|
}
|
148
|
|
|
|
149
|
|
|
/**
|
150
|
|
|
* @param $uri
|
151
|
|
|
* @param array $options
|
152
|
|
|
* @param string $type
|
153
|
|
|
* @return ResponseInterface
|
154
|
|
|
*/
|
155
|
|
|
public function delete($uri, $options = [], $type = RequestOptions::JSON)
|
156
|
|
|
{
|
157
|
|
|
return $this->dispatch('delete', $uri, $type, $options);
|
158
|
|
|
}
|
159
|
|
|
}
|
160
|
|
|
|