-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMattermostApi.php
115 lines (96 loc) · 3.68 KB
/
MattermostApi.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
declare(strict_types=1);
/*
* This file is part of forecast.it.fill project.
* (c) Patrick Jaja <[email protected]>
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace ForecastAutomation\MattermostClient\Business;
use ForecastAutomation\MattermostClient\Shared\Dto\MattermostConfigDto;
use ForecastAutomation\MattermostClient\Shared\Dto\MattermostPostsQueryDto;
use GuzzleHttp\Client;
use GuzzleHttp\Promise\Promise;
use GuzzleHttp\Promise\PromiseInterface;
class MattermostApi
{
private const AUTH_API = '/api/v4/users/login';
private const CHANNEL_API = '/api/v4/users/me/teams/%s/channels';
private const POSTS_API = '/api/v4/channels/%s/posts';
public static string $token = '';
public function __construct(private Client $guzzleClient, private MattermostConfigDto $mattermostConfigDto)
{
}
public function getChannel(array $channelFilterCollection): PromiseInterface
{
$this->auth();
$wrapPromise = new Promise(function () use ($channelFilterCollection, &$wrapPromise) {
$res = $this->guzzleClient->requestAsync(
'GET',
sprintf(self::CHANNEL_API, $this->mattermostConfigDto->teamId),
[
'headers' => [
'Authorization' => 'Bearer '.static::$token,
'Content-Type' => 'application/json',
],
]
)->wait();
$channelArray = json_decode((string) $res->getBody(), null, JSON_PARTIAL_OUTPUT_ON_ERROR, JSON_THROW_ON_ERROR);
$wrapPromise->resolve($this->applyChannelFilter($channelArray, $channelFilterCollection));
});
return $wrapPromise;
}
public function getPosts(MattermostPostsQueryDto $postsQueryDto): PromiseInterface
{
$this->auth();
$wrapPromise = new Promise(function () use ($postsQueryDto, &$wrapPromise) {
$res = $this->guzzleClient->requestAsync(
'GET',
sprintf(self::POSTS_API, $postsQueryDto->channelId),
[
'query' => ['since' => (int) $postsQueryDto->since->format('U') * 1000],
'headers' => [
'Authorization' => 'Bearer '.static::$token,
'Content-Type' => 'application/json',
],
],
)->wait();
$wrapPromise->resolve(json_decode((string) $res->getBody(), true, JSON_PARTIAL_OUTPUT_ON_ERROR, JSON_THROW_ON_ERROR)['posts']);
});
return $wrapPromise;
}
private function auth(): string
{
if ('' !== static::$token) {
return static::$token;
}
$res = $this->guzzleClient->request(
'POST',
self::AUTH_API,
[
'body' => json_encode(
[
'login_id' => $this->mattermostConfigDto->username,
'password' => $this->mattermostConfigDto->password,
],
JSON_THROW_ON_ERROR
),
],
);
$token = $res->getHeader('token');
if (0 === \count($token)) {
throw new \Exception('could not auth to mattermost');
}
static::$token = $token[0];
return static::$token;
}
private function applyChannelFilter(
array $channelArray,
array $channelFilterCollection
): array {
foreach ($channelFilterCollection as $channelFilter) {
$channelArray = $channelFilter->apply($channelArray);
}
return $channelArray;
}
}