コード例 #1
0
 public function testCreateInvalidTransporter()
 {
     $this->swapConfig(['trucker::error_handler.driver' => 'invalid']);
     Config::setApp($this->app);
     $this->setExpectedException('ReflectionException');
     $this->setExpectedException('InvalidArgumentException');
     $foo = ErrorHandlerFactory::build();
 }
コード例 #2
0
ファイル: Model.php プロジェクト: cdyweb/trucker
 /**
  * Function to delete an existing entity
  *
  * @return Boolean  Success of the delete operation
  */
 public function destroy()
 {
     //get a request object
     $request = RequestFactory::build();
     //init the request
     $request->createRequest(Config::get('request.base_uri'), UrlGenerator::getDeleteUri($this, [':' . $this->getIdentityProperty() => $this->getId()]), 'DELETE', [], Config::get('request.http_method_param'));
     //add auth if it is needed
     if ($auth = AuthFactory::build()) {
         $request->authenticate($auth);
     }
     //actually send the request
     $response = $request->sendRequest();
     //clean up anything no longer needed
     $this->doPostRequestCleanUp();
     $interpreter = ResponseInterpreterFactory::build();
     //handle clean response with errors
     if ($interpreter->success($response)) {
         return true;
     } else {
         if ($interpreter->invalid($response)) {
             //get the errors and set them to our local collection
             $this->errors = ErrorHandlerFactory::build()->parseErrors($response);
         }
     }
     //end if-else
     return false;
 }
コード例 #3
0
 /**
  * Function to fetch a collection of Trucker\Resource\Model object
  * from the remote API.
  *
  * @param  Model                      $model       Instance of entity type being fetched
  * @param  QueryConditionInterface    $condition   Query conditions for the request
  * @param  QueryResultOrderInterface  $resultOrder Result ordering requirements for the request
  * @param  array                      $getParams   Additional GET parameters to send w/ request
  * @return Trucker\Responses\Collection
  */
 public function fetch(Model $model, QueryConditionInterface $condition = null, QueryResultOrderInterface $resultOrder = null, array $getParams = [])
 {
     //get a request object
     $request = RequestFactory::build();
     //init the request
     $request->createRequest(Config::get('request.base_uri'), UrlGenerator::getCollectionUri($model), 'GET');
     //add auth if it is needed
     if ($auth = AuthFactory::build()) {
         $request->authenticate($auth);
     }
     //add query conditions if needed
     if ($condition) {
         $request->addQueryCondition($condition);
     }
     //add result ordering if needed
     if ($resultOrder) {
         $request->addQueryResultOrder($resultOrder);
     }
     //set any get parameters on the request
     $request->setGetParameters($getParams);
     //actually send the request
     $response = $request->sendRequest();
     //handle clean response with errors
     if (ResponseInterpreterFactory::build()->invalid($response)) {
         //get the errors and set them to our local collection
         $errors = ErrorHandlerFactory::build()->parseErrors($response);
         throw new RequestException('Error received when requesting collection', $errors);
     }
     //end if
     //get api response
     $data = $response->parseResponseToData();
     //make an array to hold results
     $records = array();
     //figure out wether a collection key is used
     $collection_key = Config::get('resource.collection_key');
     //set records array appropriatley
     if (isset($collection_key)) {
         $recordCollection = $data[$collection_key];
     } else {
         $recordCollection = $data;
     }
     //create an array of popuplated results
     foreach ($recordCollection as $values) {
         $instance = new $model($values);
         //inflate the ID property that should be guarded
         $id = $instance->getIdentityProperty();
         if (array_key_exists($id, $values)) {
             $instance->{$id} = $values[$id];
         }
         //add the instance to the records array
         $records[] = $instance;
     }
     //end foreach
     //create a collection object to return
     $collection = new Collection($records);
     // if there was a collection_key, put any extra data that was returned
     // outside the collection key in the metaData attribute
     if (isset($collection_key)) {
         $collection->metaData = array_diff_key($data, array_flip((array) array($collection_key)));
     }
     return $collection;
 }
コード例 #4
0
ファイル: RestRequest.php プロジェクト: summer11123/trucker
 /**
  * Function to execute a raw request on the base URI with the given uri path
  * and params
  *
  * @param  string $uri       uri to hit (i.e. /users)
  * @param  string $method    Request method (GET, PUT, POST, PATCH, DELETE, etc.)
  * @param  array  $params    PUT or POST parameters to send
  * @param  array  $getParams Querystring parameters to send
  * @param  array  $files     PUT or POST files to send (key = name, value = path)
  * @param  array  $headers   Optional headers to use
  * @return \Trucker\Responses\RawResponse
  */
 public function rawRequest($uri, $method, $params = array(), $getParams = array(), $files = array(), $headers = array())
 {
     $this->request = self::createRequest(Config::get('request.base_uri'), $uri, $method);
     $this->setPostParameters($params);
     $this->setGetParameters($getParams);
     $this->setFileParameters($files);
     $this->setHeaders($headers);
     //encode the request body
     /** @var \Trucker\Transporters\TransporterInterface $transporter */
     $transporter = TransporterFactory::build();
     $transporter->setRequestBody($this, $params);
     // Trucker\Response
     $response = $this->sendRequest();
     //handle clean response with errors
     if (ResponseInterpreterFactory::build()->invalid($response)) {
         //get the errors and set them to our local collection
         $errors = (array) ErrorHandlerFactory::build()->parseErrors($response);
         return new RawResponse(false, $response, $errors);
     }
     //end if
     return new RawResponse(true, $response);
 }
コード例 #5
0
ファイル: Model.php プロジェクト: victormacko/trucker
 /**
  * Actually send the request specified. This has been separated out to allow subclasses to add in their own
  * methods as needed.
  *
  * @param \Trucker\Requests\RestRequest $request
  * @param string $requestType (GET/DELETE/PUT/PATCH/POST/etc)
  * @return bool|\Trucker\Responses\Response
  */
 protected function sendRequest($request)
 {
     //add auth if it is needed
     if ($auth = AuthFactory::build()) {
         $request->authenticate($auth);
     }
     //actually send the request
     $response = $request->sendRequest();
     //handle clean response with errors
     if (ResponseInterpreterFactory::build()->invalid($response)) {
         //get the errors and set them to our local collection
         $this->errors = ErrorHandlerFactory::build()->parseErrors($response);
         //do any needed cleanup
         $this->doPostRequestCleanUp();
         return false;
     }
     //end if
     return $response;
 }