-
Notifications
You must be signed in to change notification settings - Fork 36
/
Batcher.php
123 lines (112 loc) · 2.91 KB
/
Batcher.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
116
117
118
119
120
121
122
123
<?php
namespace GetStream\Stream;
use GuzzleHttp\HandlerStack;
use Psr\Http\Message\RequestInterface;
class Batcher extends Feed
{
/**
* @var Signer
*/
private $signer;
/**
* @param ClientInterface $client
* @param Signer $signer
* @param string $api_key
*/
public function __construct(ClientInterface $client, Signer $signer, $api_key)
{
$this->client = $client;
$this->signer = $signer;
$this->api_key = $api_key;
}
/**
* @return \GuzzleHttp\HandlerStack
*/
public function getHandlerStack()
{
$stack = HandlerStack::create();
$stack->push(function (callable $handler) {
return function (RequestInterface $request, array $options) use ($handler) {
return $handler($this->signer->signRequest($request), $options);
};
});
return $stack;
}
/**
* @param array $activityData
* @param array $feeds
*
* @throws StreamFeedException
*
* @return array
*/
public function addToMany($activityData, $feeds)
{
$data = [
'feeds' => $feeds,
'activity' => $activityData,
];
return $this->makeHttpRequest('feed/add_to_many/', 'POST', $data);
}
/**
* @param array $follows
* @param int $activity_copy_limit
*
* @throws StreamFeedException
*
* @return array
* $follows = [
* ['source' => 'flat:1', 'target' => 'user:1'],
* ['source' => 'flat:1', 'target' => 'user:3']
* ]
*/
public function followMany($follows, $activity_copy_limit = null)
{
$query_params = [];
if ($activity_copy_limit !== null) {
$query_params["activity_copy_limit"] = $activity_copy_limit;
}
return $this->makeHttpRequest('follow_many/', 'POST', $follows, $query_params);
}
/**
* @param array $unfollows
*
* @throws StreamFeedException
*
* @return array
* $unfollows = [
* ['source' => 'user:1', 'target' => 'timeline:1'],
* ['source' => 'user:2', 'target' => 'timeline:2', 'keep_history' => true]
* ]
*/
public function unfollowMany($unfollows)
{
$query_params = [];
return $this->makeHttpRequest('unfollow_many/', 'POST', $unfollows, $query_params);
}
/**
* @param string $method
*
* @throws StreamFeedException
*
* @return mixed
*/
public function test($method)
{
return $this->makeHttpRequest('test/auth/digest/', $method);
}
/**
* @param string $resource
* @param string $action
*
* @return array
*/
protected function getHttpRequestHeaders($resource, $action)
{
return [
'Content-Type' => 'application/json',
'Date' => gmdate('D, d M Y H:i:s T'),
'X-Api-Key' => $this->api_key,
];
}
}