/** * {@inheritDoc} */ protected function _executeCreateQuery(Query $query, array $options = []) { $postArguments = ['status' => $query->set()['text']]; /* @var Response $response */ $response = $this->driver()->client()->post($this->_baseUrl() . '/update.json', $postArguments); $this->_checkResponse($response); return $this->_transformResource($query->endpoint(), $response->json); }
/** * {@inheritDoc} */ protected function _executeReadQuery(Query $query, array $options = []) { $vars = []; foreach ($query->where() as $k => $v) { if ($k == 'actual' || $k == 'unplanned') { $vars[$k] = is_string($v) ? $v : ($v ? 'true' : 'false'); } else { $vars[$k] = $v; } } $response = $this->driver()->client()->get('/ns-api-storingen', $vars); if (!$response->isOk()) { return false; } $xmlData = $response->xml; $disruptions = []; $keys = ['Ongepland', 'Gepland']; foreach ($keys as $key) { foreach ($xmlData->{$key}->Storing as $disruption) { $disruptions[] = ['id' => !empty($disruption->id) ? (string) $disruption->id : null, 'planned' => $key == 'Gepland', 'route' => !empty($disruption->Traject) ? (string) $disruption->Traject : null, 'reason' => !empty($disruption->Oorzaak) ? (string) $disruption->Oorzaak : null, 'advice' => !empty($disruption->Advies) ? (string) $disruption->Advies : null, 'message' => !empty($disruption->Bericht) ? (string) $disruption->Bericht : null, 'delay' => !empty($disruption->Vertraging) ? (string) $disruption->Vertraging : null, 'period' => !empty($disruption->Periode) ? (string) $disruption->Periode : null]; } } return new ResultSet($this->_transformResults($disruptions, $options['resourceClass'])); }
/** * @expectedException \CvoTechnologies\Twitter\Webservice\Exception\UnknownErrorException */ public function testStatusUpdateDuplicate() { $client = $this->getMockBuilder('Cake\\Network\\Http\\Client')->setMethods(['post'])->getMock(); $client->expects($this->once())->method('post')->with('/1.1/statuses/update.json', ['status' => 'Test123'])->willReturn(new Response(['HTTP/1.1 403 Forbidden'], json_encode(['errors' => [['message' => 'Status is a duplicate.']]]))); $this->webservice->driver()->client($client); $query = new Query($this->webservice, new Endpoint()); $query->create(); $query->set(['text' => 'Test123']); $resource = $this->webservice->execute($query); $this->assertInstanceOf('Muffin\\Webservice\\Model\\Resource', $resource); $this->assertEquals($resource->text, 'Test123'); }
public function testDelete() { StreamWrapper::emulate(HttpEmulation::fromCallable(function (RequestInterface $request) { $this->assertEquals('POST', $request->getMethod()); $this->assertEquals('/1.1/statuses/destroy/2.json', $request->getUri()->getPath()); return new \GuzzleHttp\Psr7\Response(200, [], json_encode(['id' => 2, 'text' => 'Status 2?'])); })); $query = new Query($this->webservice, new Endpoint(['endpoint' => 'statuses', 'connection' => $this->webservice->driver()])); $query->action(Query::ACTION_DELETE); $query->where(['id' => 2]); $this->assertTrue($this->webservice->execute($query)); }
/** * {@inheritDoc} */ protected function _executeDeleteQuery(Query $query, array $options = []) { if (!isset($query->where()['id']) || is_array($query->where()['id'])) { return false; } $url = $this->_baseUrl() . '/destroy/' . $query->where()['id'] . '.json'; /* @var Response $response */ $response = $this->driver()->client()->post($url); if (!$response->isOk()) { throw new Exception($response->json['errors'][0]['message']); } return true; }
/** * Calls a finder method directly and applies it to the passed query, * if no query is passed a new one will be created and returned * * @param string $type name of the finder to be called * @param \Muffin\Webservice\Query $query The query object to apply the finder options to * @param array $options List of options to pass to the finder * * @return \Muffin\Webservice\Query */ public function callFinder($type, Query $query, array $options = []) { $query->applyOptions($options); $options = $query->getOptions(); $finder = 'find' . $type; if (method_exists($this, $finder)) { return $this->{$finder}($query, $options); } throw new \BadMethodCallException(sprintf('Unknown finder method "%s"', $type)); }
/** * Logs a query to the specified logger * * @param \Muffin\Webservice\Query $query The query to log * @param \Psr\Log\LoggerInterface $logger The logger instance to use * * @return void */ protected function _logQuery(Query $query, LoggerInterface $logger) { if (!$this->driver()->logQueries()) { return; } $logger->debug($query->endpoint(), ['params' => $query->where()]); }
/** * Use the Twitter filter stream. * * @param \Muffin\Webservice\Query $query The query to modify. * @param array $options The conditions to apply to the query. * @return Query The modified query. */ public function findFilterStream(Query $query, array $options) { return $query->applyOptions(['streamEndpoint' => 'filter'])->where($options); }
/** * @expectedException \CvoTechnologies\Redmine\Webservice\Exception\MissingResultsException */ public function testReadMissingResults() { $this->webservice->driver()->client($this->_clientMock('get', '/issues.json', [], [], [], 200)); $query = new Query($this->webservice, new Endpoint(['connection' => new Connection(['name' => 'redmine', 'driver' => 'CvoTechnologies\\Redmine\\Webservice\\Driver\\Redmine']), 'endpoint' => 'issues', 'schema' => new Schema('issues')])); $query->read(); $this->webservice->execute($query); }
/** * {@inheritDoc} */ protected function _executeReadQuery(Query $query, array $options = []) { $url = $this->getBaseUrl(); $requestParameters = []; $requestOptions = []; // Set API key if necessary if ($this->driver()->config('api_key')) { $requestParameters['key'] = $this->driver()->config('api_key'); } // Single resource if (isset($query->clause('where')['id']) && !is_array($query->clause('where')['id'])) { $url .= '/' . $query->clause('where')['id']; } else { // Loop over conditions and apply them to the query foreach ($query->clause('where') as $field => $value) { $column = $query->endpoint()->schema()->column($field); // Custom fields should use cf_x as parameter if ($column['custom_field_id']) { $field = 'cf_' . $column['custom_field_id']; } $requestParameters[$field] = $value; } } $url .= '.json'; $orderClauses = []; // Turn ORM order clauses in a query parameter foreach ($query->clause('order') as $field => $value) { if (is_int($field)) { $field = $value; } if (!in_array($value, ['ASC', 'DESC'])) { $value = null; } if (!$value) { $orderClauses[] = $field; continue; } // Replace ASC with asc and DESC with desc $orderClauses[] = $field . ':' . str_replace(['ASC', 'DESC'], ['asc', 'desc'], $value); } if (!empty($orderClauses)) { $requestParameters['sort'] = implode($orderClauses, ','); } if ($query->clause('limit')) { $requestParameters['limit'] = $query->clause('limit'); } if ($query->clause('offset')) { $requestParameters['offset'] = $query->clause('offset'); } // Include details using the API if (isset($query->getOptions()['include'])) { $include = $query->getOptions()['include']; // If the value isn't a array, for example a string put it in a string if (!is_array($include)) { $include = [$include]; } $requestParameters['include'] = implode(',', $include); } // Switch user if the 'user' options has been given if (isset($query->getOptions()['user'])) { $requestOptions['headers']['X-Redmine-Switch-User'] = $query->getOptions()['user']; } /* @var \Cake\Network\Http\Response $response */ $response = $this->driver()->client()->get($url, $requestParameters, $requestOptions); if (!$response->isOk()) { throw new UnexpectedStatusCodeException([$response->statusCode()]); } // Single resource if (isset($query->clause('where')['id'])) { // Turn result into resources $resource = $this->_transformResource($response->json[Inflector::singularize($this->endpoint())], $query->endpoint()); return new ResultSet([$resource], 1); } if (!isset($response->json[$this->endpoint()])) { throw new MissingResultsException([$url]); } $results = $response->json[$this->endpoint()]; $total = count($results); // Set the amount if results to total_count if it has been provided by the API if (isset($response->json['total_count'])) { $total = $response->json['total_count']; } // Turn results into resources $resources = $this->_transformResults($results, $query->endpoint()); return new ResultSet($resources, $total); }