Example #1
0
 /**
  * Make Request Method Call To Server
  *
  * @param string|iSeqPathUri|iHttpUri|UriInterface $uri     Absolute Uri Or Relative To BaseUrl
  * @param array|iDataStruct|null              $options Browser Options Or Open Options Used By Plugins
  * @param iStreamable|string|null                  $body    Request Body
  * @param array|iHeaderCollection|null             $headers Specific Request Header/Replace Defaults
  *
  * @return ResponsePlatform
  */
 protected function __makeRequestCall($method, $uri, $options = null, $body = null, $headers = null)
 {
     $method = new ReqMethod(['uri' => $uri, 'method' => $method]);
     $options === null ?: $method->setBrowser($options);
     $body === null ?: $method->setBody($body);
     $headers === null ?: $method->setHeaders($headers);
     $response = $this->call($method);
     return $response;
 }
Example #2
0
 /**
  * Build Platform Specific Expression To Send
  * Trough Connection
  *
  * @param iApiMethod|ReqMethod $ReqMethod Method Interface
  *
  * @return HttpRequest
  */
 function makeExpression(iApiMethod $ReqMethod)
 {
     ## make a copy of browser when making changes on it by ReqMethod
     ### with bind browser options
     $CUR_BROWSER = $this->browser;
     $this->browser = clone $CUR_BROWSER;
     if (!$ReqMethod instanceof ReqMethod) {
         $ReqMethod = new ReqMethod($ReqMethod->toArray());
     }
     # Request Options:
     ## (1)
     /*
      * $browser->POST('/api/v1/auth/login', [
      *      'form_data' => [
      *      // ...
      */
     if ($ReqMethod->getBrowser()) {
         ## Browser specific options
         $prepConn = false;
         foreach ($ReqMethod->getBrowser()->props()->readable as $prop) {
             if ($ReqMethod->getBrowser()->__isset($prop)) {
                 $this->browser->optsData()->__set($prop, $ReqMethod->getBrowser()->__get($prop));
                 $prepConn = true;
             }
         }
         ## prepare connection again with new configs
         !$prepConn ?: $this->prepareTransporter($this->_connection, true);
     }
     // ...
     if ($ReqMethod->getUri() instanceof iHttpUri) {
         ## Reset Server Base Url When Absolute Http URI Requested
         /*
          * $browser->get(
          *   'http://www.pasargad-co.ir/forms/contact'
          *   , [ 'connection' => ['time_out' => 30],
          *     // ...
          */
         $t_uri = $ReqMethod->getUri();
         if ($t_uri->getHost()) {
             $this->browser->optsData()->setBaseUrl($t_uri);
             $this->prepareTransporter($this->_connection);
         }
         ### continue with sequence http uri
         $t_uri = $ReqMethod->getUri()->getPath() ? $ReqMethod->getUri()->getPath() : new SeqPathJoinUri('/');
         $ReqMethod->setUri($t_uri);
     }
     # Build Request Http Message:
     ## (2)
     $REQUEST = $this->__getRequestObject();
     $REQUEST->setMethod($ReqMethod->getMethod());
     $serverUrl = $this->_connection->inOptions()->getServerUrl();
     $serverUrl = new HttpUri($serverUrl);
     $REQUEST->setHost($serverUrl->getHost());
     ## req Headers ------------------------------------------------------------------\
     ### default headers
     $reqHeaders = $REQUEST->getHeaders();
     if ($this->browser->optsData()->__isset('user_agent')) {
         $reqHeaders->set(HeaderFactory::factory('User-Agent', $this->browser->optsData()->getUserAgent()));
     }
     $reqHeaders->set(HeaderFactory::factory('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'));
     $reqHeaders->set(HeaderFactory::factory('Cache-Control', 'no-cache'));
     /*if ($this->browser->inOptions()->getConnection())
       (!$this->browser->inOptions()->getConnection()->isAllowDecoding())
           ?: $reqHeaders->set(HeaderFactory::factory('Accept-Encoding'
           , 'gzip, deflate, sdch'
       ));*/
     ### headers as default browser defined header
     foreach ($this->browser->optsData()->getRequest()->getHeaders() as $h) {
         $reqHeaders->set($h);
     }
     ### headers as request method options
     if ($ReqMethod->getHeaders()) {
         /** @var iHeader $h */
         foreach ($ReqMethod->getHeaders() as $h) {
             if ($reqHeaders->has($h->getLabel())) {
                 $reqHeaders = $reqHeaders->del($h->getLabel());
             }
             $reqHeaders->set($h);
         }
     }
     ## req Uri ----------------------------------------------------------------------\
     $basePath = null;
     if ($this->browser->optsData()->__isset('base_url')) {
         $basePath = $this->browser->optsData()->getBaseUrl()->getPath();
     }
     if ($basePath === null) {
         $basePath = new SeqPathJoinUri('/');
     }
     $targetUri = $basePath->merge($ReqMethod->getUri());
     ### merge with request base url path
     $REQUEST->getUri()->setPath($targetUri);
     ### remove unnecessary iHttpUri parts such as port, host, ...
     ### its presented as host and etc. on Request Message Object
     $uri = new HttpUri(['path' => $REQUEST->getUri()->getPath(), 'query' => $REQUEST->getUri()->getQuery(), 'fragment' => $REQUEST->getUri()->getFragment()]);
     $REQUEST->setUri($uri);
     ## req body ---------------------------------------------------------------------\
     $REQUEST->setBody($ReqMethod->getBody());
     # Implement Browser Plugins:
     ## (3)
     foreach ($ReqMethod->getBrowser()->props()->readable as $prop) {
         if (!$this->getPluginManager()->has($prop)) {
             /*
              * $browser->POST('/api/v1/auth/login', [
              *      'form_data' => [ // <=== plugin form_data will trigger with this params
              *      // ...
              */
             continue;
         }
         ## no plugin bind on this option
         /** @var Browser\Plugin\AbstractBrowserPlugin $plugin */
         $plugin = $this->getPluginManager()->fresh($prop);
         $plugin->from($ReqMethod->getBrowser()->__get($prop));
         ### options for service
         if ($plugin instanceof iBrowserExpressionPlugin) {
             $plugin->withHttpRequest($REQUEST);
         }
     }
     $this->browser = $CUR_BROWSER;
     return $REQUEST;
 }