/** * Requests a document from the database. * * If the request is not successful, then getDocument will throw a DocumentNotFoundException. * * If the request is successful, then the body should contain a json object, which will be decoded, and then * returned. * * @param Connection $connection * @param Database $database * @param string $id * @throws DocumentNotFoundException * @return Response */ public function getDocument(Connection $connection, $database, $id) { $value = $connection->request('GET', $database . '/' . $id); $response = $this->response_factory->make($value); if (!$response->success()) { throw new DocumentNotFoundException(); } return json_decode($response->body()); }
/** * Attempts to create a document in the database with the given id and body. * * @param Connection $connection * @param string $database * @param string $id * @param string $body * @return mixed * @throws DocumentCreationException */ public function createDocument(Connection $connection, $database, $id, $body) { $value = $connection->request('PUT', $database . '/' . $id, $body); $response = $this->response_factory->make($value); if ($response->status() !== '201' && $response->status() !== '202') { throw new DocumentCreationException(); } return json_decode($response->body()); }
public function createAttachment(Uploader $uploader, $database, $id, $revision, $name, $file) { $value = $uploader->upload('PUT', $database . '/' . $id . '/' . $name . '?rev=' . $revision, $file); $response = $this->response_factory->make($value); if ($response->status() !== '201' && $response->status() !== '202') { throw new AttachmentCreationException(); } return json_decode($response->body()); }
public function testGetDocument() { $value = 'HTTP/1.1 200 OK' . "\r\n" . 'Server: CouchDB/1.3.1 (Erlang OTP/R15B03)' . "\r\n" . 'ETag: "7-9b82726d3c5ef5d5d95ae33ec59ddd06"' . "\r\n" . 'Date: Sun, 23 Mar 2014 21:09:51 GMT' . "\r\n" . 'Content-Type: text/plain; charset=utf-8' . "\r\n" . 'Content-Length: 456' . "\r\n" . 'Cache-Control: must-revalidate' . "\r\n" . '' . "\r\n" . '{"_id":"doc","_rev":"7-9b82726d3c5ef5d5d95ae33ec59ddd06","_attachments":{"key":{"content_type":"application/octet-stream","revpos":7,"digest":"md5-1B2M2Y8AsgTpgAmY7PhCfg==","length":0,"stub":true},"b":{"content_type":"application/octet-stream","revpos":6,"digest":"md5-fcbr7InXnOCoyr/rRJ3DtA==","length":405,"stub":true},"a":{"content_type":"application/x-www-form-urlencoded","revpos":3,"digest":"md5-fcbr7InXnOCoyr/rRJ3DtA==","length":405,"stub":true}}}'; $expect = 'doc'; $factory = new ResponseFactory(); $actual = json_decode($factory->make($value)->body())->_id; $this->assertEquals($expect, $actual); }
/** * Obtains an array of randomly generated uuids from the server. * * @param Connection $connection * @param int $count * @return mixed */ public function generateIds(Connection $connection, $count = 1) { $value = $connection->request('GET', '_uuids?count=' . $count); $response = $this->response_factory->make($value); return json_decode($response->body())->uuids; }