Ejemplo n.º 1
0
 /**
  * Get a specific entity.
  * @param Entity $instance
  * @return Entity
  */
 public function fetch(Entity $instance)
 {
     if (!$instance->isAllowedMethod(__FUNCTION__)) {
         throw new \DomainException(__FUNCTION__ . ' not allowed on this entity.');
     }
     $url = sprintf('%s/%s/%s/%s', $this->getApiUrl(), $this->getNetworkId(), $this->getEndpointName($instance), $instance->getId());
     $response = \Httpful\Request::get($url)->authenticateWith($this->getLogin(), $this->getPassword())->parseWith(function ($body) use($instance) {
         return EntityFactory::createFromArray($instance->getNonQualifiedClassName(), (array) json_decode($body, true));
     })->send();
     if ($response->hasErrors()) {
         $exception = json_decode($response->raw_body);
         throw new \UnexpectedValueException(sprintf("API response raised a '%s' exception with a message: '%s'. Status code %s", $exception->name, $exception->message, $response->code));
     }
     return $response->body;
 }