/**
  * Performs the actual REST request by rewriting
  * the method (people.get) to the proper REST endpoint
  * and converting the params into a properly formed
  * REST url
  *
  * @param osapiRequest $request
  * @return array decoded response body
  */
 private static function executeRestRequest(osapiRequest $request, osapiProvider $provider, osapiAuth $signer)
 {
     $service = $request->getService($request->method);
     $operation = $request->getOperation($request->method);
     if (!isset(self::$urlTemplates[$service])) {
         throw new osapiException("Invalid service: {$service}");
     }
     $urlTemplate = self::$urlTemplates[$service];
     if (!isset(self::$methodAliases[$operation])) {
         throw new osapiException("Invalid method: ({$service}) {$operation}");
     }
     $method = self::$methodAliases[$operation];
     $postBody = false;
     if ($method != 'GET') {
         if (isset(self::$postAliases[$service]) && isset($request->params[self::$postAliases[$service]])) {
             $postBody = json_encode($request->params[self::$postAliases[$service]]);
             unset($request->params[self::$postAliases[$service]]);
         }
     }
     $baseUrl = $provider->restEndpoint;
     if (substr($baseUrl, strlen($baseUrl) - 1, 1) == '/') {
         // Prevent double //'s in the url when concatinating
         $baseUrl = substr($baseUrl, 0, strlen($baseUrl) - 1);
     }
     $url = $baseUrl . self::constructUrl($urlTemplate, $request->params);
     if (!$provider->isOpenSocial) {
         // PortableContacts end points don't require the /people bit added
         $url = str_replace('/people', '', $url);
     }
     $signedUrl = $signer->sign($method, $url, $request->params, $postBody);
     $response = self::send($signedUrl, $method, $provider->httpProvider, $postBody);
     $ret = array();
     if ($response['http_code'] == '200' && !empty($response['data'])) {
         $ret['data'] = json_decode($response['data'], true);
         if ($ret['data'] == $response['data']) {
             // signals a failure in decoding the json
             throw new osapiException("Error decoding server response: '" . $response['data'] . "'");
         }
     } else {
         $ret = new osapiError($response['http_code'], isset($response['data']) ? $response['data'] : '');
     }
     return $ret;
 }
Esempio n. 2
0
 /**
  * Performs the actual REST request by rewriting
  * the method (people.get) to the proper REST endpoint
  * and converting the params into a properly formed
  * REST url
  *
  * @param osapiRequest $request
  * @return array decoded response body
  */
 private static function executeRestRequest(osapiRequest $request, osapiProvider $provider, osapiAuth $signer)
 {
     $service = $request->getService($request->method);
     $operation = $request->getOperation($request->method);
     if (!isset(self::$urlTemplates[$service])) {
         throw new osapiException("Invalid service: {$service}");
     }
     $urlTemplate = self::$urlTemplates[$service];
     if (!isset(self::$methodAliases[$operation])) {
         throw new osapiException("Invalid method: ({$service}) {$operation}");
     }
     $method = self::$methodAliases[$operation];
     $postBody = false;
     $headers = false;
     $hasPostBody = false;
     if ($method != 'GET') {
         if (isset(self::$postAliases[$service]) && isset($request->params[self::$postAliases[$service]])) {
             $hasPostBody = true;
             $headers = array("Content-Type: application/json");
             if ($request->method == 'mediaItems.upload') {
                 $postBody = $request->params[self::$postAliases[$service]];
                 $headers = array("Content-Type: " . $request->params['contentType'], 'Expect:');
                 unset($request->params['contentType']);
             }
         }
     }
     $baseUrl = $provider->restEndpoint;
     if (substr($baseUrl, strlen($baseUrl) - 1, 1) == '/') {
         // Prevent double //'s in the url when concatinating
         $baseUrl = substr($baseUrl, 0, strlen($baseUrl) - 1);
     }
     if (method_exists($provider, 'preRequestProcess')) {
         // Note that we're passing baseUrl, not the complete service URL.
         // It should be easier to change service parameters by changing
         // the params array than modifying a string url.
         $provider->preRequestProcess($request, $method, $baseUrl, $headers, $signer);
     }
     if ($hasPostBody) {
         if ($request->method == 'mediaItems.upload') {
             // If we are uploading a mediaItem don't try to json_encode it.
             $postBody = $request->params[self::$postAliases[$service]];
         } else {
             // Pull out the (possibly) modified post body parameter and
             // unset it from the request, so that it doesn't get signed.
             $postBody = json_encode($request->params[self::$postAliases[$service]]);
         }
         unset($request->params[self::$postAliases[$service]]);
     }
     $url = $baseUrl . self::constructUrl($urlTemplate, $request->params);
     if (!$provider->isOpenSocial) {
         // PortableContacts end points don't require the /people bit added
         $url = str_replace('/people', '', $url);
     }
     $signedUrl = $signer->sign($method, $url, $request->params, $postBody, $headers);
     $response = self::send($signedUrl, $method, $provider->httpProvider, $headers, $postBody);
     if (method_exists($provider, 'postRequestProcess')) {
         $provider->postRequestProcess($request, $response);
     }
     $ret = array();
     // Added 201 for create requests
     if (($response['http_code'] == '200' || $response['http_code'] == '201') && !empty($response['data'])) {
         $ret['data'] = json_decode($response['data'], true);
         if ($ret['data'] == $response['data']) {
             // signals a failure in decoding the json
             throw new osapiException("Error decoding server response: '" . $response['data'] . "'");
         }
     } else {
         $ret = new osapiError($response['http_code'], isset($response['data']) ? $response['data'] : '');
     }
     return $ret;
 }