Skip to content

Commit 785b1f9

Browse files
committed
update collections API
1 parent a4e0ca2 commit 785b1f9

1 file changed

Lines changed: 69 additions & 18 deletions

File tree

lib/GetStream/Stream/Collections.php

Lines changed: 69 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ private function doRequest($method, $endpoint, $params=null)
5050
if( $method === 'POST' || $method === 'PUT' ){
5151
$params = ['json' => $params];
5252
}
53+
if( $method === 'GET' && $params !== null ){
54+
55+
$endpoint .= '?' . http_build_query($params);
56+
}
5357
try {
5458
$response = $this->client->request($method, $endpoint, $params);
5559
} catch (ClientException $e) {
@@ -62,6 +66,22 @@ private function doRequest($method, $endpoint, $params=null)
6266
return $response;
6367
}
6468

69+
/**
70+
* @param string $collectionName
71+
* @param array $data
72+
* @param string $id (optional)
73+
* @param string $user_id (optional)
74+
*
75+
* @return array
76+
*/
77+
public function add($collectionName, array $data, $id=null, $user_id=null)
78+
{
79+
$payload = ["id" => $id, "data" => $data, "user_id" => $user_id];
80+
$response = $this->doRequest('POST', 'collections/' . $collectionName . '/', $payload);
81+
$body = $response->getBody()->getContents();
82+
return json_decode($body, true);
83+
}
84+
6585
/**
6686
* @param string $collectionName
6787
* @param string $id
@@ -73,6 +93,48 @@ public function createReference($collectionName, $id)
7393
return "SO:".$collectionName.":".$id;
7494
}
7595

96+
/**
97+
* @param string $collectionName
98+
* @param string $id
99+
*
100+
* @return array
101+
*/
102+
public function delete($collectionName, $id)
103+
{
104+
105+
$response = $this->doRequest('DELETE', 'collections/' . $collectionName . '/' . $id . '/');
106+
$body = $response->getBody()->getContents();
107+
return json_decode($body, true);
108+
}
109+
110+
/**
111+
* @param string $collectionName
112+
* @param array $ids
113+
*
114+
* @return array
115+
*/
116+
public function deleteMany($collectionName, array $ids)
117+
{
118+
$ids = join(',', $ids);
119+
$queryParams = ['collection_name' => $collectionName, 'ids' => $ids];
120+
$response = $this->client->request('DELETE', 'collections/?'.http_build_query($queryParams));
121+
$body = $response->getBody()->getContents();
122+
return json_decode($body, true);
123+
}
124+
125+
/**
126+
* @param string $collectionName
127+
* @param string $id
128+
*
129+
* @return array
130+
*/
131+
public function get($collectionName, $id)
132+
{
133+
$response = $this->doRequest('GET', 'collections/' . $collectionName . '/' . $id . '/');
134+
$body = $response->getBody()->getContents();
135+
return json_decode($body, true);
136+
}
137+
76138
/**
77139
* @param string $collectionName
78140
* @param array $ids
@@ -92,40 +154,29 @@ public function select($collectionName, array $ids)
92154

93155
/**
94156
* @param string $collectionName
157+
* @param string $id
95158
* @param array $data
96159
*
97160
* @return array
98161
*/
99-
public function upsert($collectionName, array $data)
162+
public function update($collectionName, $id, array $data)
100163
{
101-
$response = $this->doRequest('POST', 'meta/', ['data' => [$collectionName => array($data)]]);
164+
$payload = ["data" => $data];
165+
$response = $this->doRequest('PUT', 'collections/' . $collectionName . '/' . $id . '/', $payload);
102166
$body = $response->getBody()->getContents();
103167
return json_decode($body, true);
104168
}
105169

106170
/**
107171
* @param string $collectionName
108-
* @param array $ids
172+
* @param array $data
109173
*
110174
* @return array
111175
*/
112-
public function delete($collectionName, array $ids)
176+
public function upsert($collectionName, array $data)
113177
{
114-
$ids = join(',', $ids);
115-
$queryParams = ['collection_name' => $collectionName, 'ids' => $ids];
116-
117-
try {
118-
$response = $this->client->request('DELETE', 'meta/?'.http_build_query($queryParams));
119-
} catch (ClientException $e) {
120-
$response = $e->getResponse();
121-
$msg = $response->getBody();
122-
$code = $response->getStatusCode();
123-
$previous = $e;
124-
throw new StreamFeedException($msg, $code, $previous);
125-
}
126-
178+
$response = $this->doRequest('POST', 'meta/', ['data' => [$collectionName => array($data)]]);
127179
$body = $response->getBody()->getContents();
128-
129180
return json_decode($body, true);
130181
}
131182

0 commit comments

Comments
 (0)