Example #1
0
 protected static function convertArray(osapiRequest $request, $val, $strictMode)
 {
     $converted = null;
     $service = $request->getService($request->method);
     $method = substr($request->method, stripos($request->method, '.') + 1);
     // don't converArray on responses that do not need to be placed into their respective models. (supportedFields, delete, create, update)
     if ($method == 'get') {
         switch ($service) {
             case 'people':
                 $converted = osapiPeople::convertArray($val, $strictMode);
                 break;
             case 'activities':
                 $converted = osapiActivities::convertArray($val, $strictMode);
                 break;
             case 'appdata':
                 $converted = osapiAppData::convertArray($val, $strictMode);
                 break;
             case 'messages':
                 $converted = osapiMessages::convertArray($val, $strictMode);
                 break;
             case 'mediaItems':
                 $converted = osapiMediaItems::convertArray($val, $strictMode);
                 break;
             case 'albums':
                 $converted = osapiAlbums::convertArray($val, $strictMode);
                 break;
         }
     }
     return $converted ? $converted : $val;
 }
Example #2
0
 public function create($params)
 {
     if (!isset($params['message'])) {
         throw new osapiException("Missing message in osapiMessages->create()");
     }
     if (!$params['message'] instanceof osapiMessage) {
         throw new osapiException("The params['message'] should be a osapiMessage in osapiMessages->create()");
     }
     return osapiRequest::createRequest('messages.create', $params);
 }
 /**
  * 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;
 }
 /**
  * Gets status and mood. Uses specific endpoint for this
  * Myspace specific
  * @return osapiRequest the request
  */
 public function get($params)
 {
     if (!array_key_exists('userId', $params)) {
         $params['userId'] = '@me';
     }
     if (!array_key_exists('groupId', $params)) {
         $params['groupId'] = '@self';
     }
     return osapiRequest::createRequest('profilecomments.get', $params);
 }
 /**
  * Creates an album
  *
  * @param array $params the parameters defining the album to create
  * @return osapiRequest the request
  */
 public function create($params)
 {
     if (!isset($params['album'])) {
         throw new osapiException("Missing album in osapiAlbums->create()");
     }
     if (!$params['album'] instanceof osapiAlbum) {
         throw new osapiException("The params['album'] should be a osapiAlbum in osapiAlbums->create()");
     }
     //TODO: check album.field restrictions
     return osapiRequest::createRequest('albums.create', $params);
 }
 /**
  * Correct issue where data objects are nested inside a 'modelname' node.
  * @param osapiRequest $request
  * @param array $response
  */
 private function fixModelContainer(osapiRequest &$request, &$response)
 {
     $plural_rules = array('groups' => 'group', 'people' => 'person', 'albums' => 'album', 'mediaItems' => 'mediaItem', 'activities' => 'activity', 'appdata' => 'appData', 'statusmood' => 'statusmood', 'notifications' => 'notification');
     $data = json_decode($response['data']);
     $service = $request->getService($request->method);
     $model = $plural_rules[$service];
     if (isset($data->entry)) {
         foreach ($data->entry as $key => $value) {
             if ($model == 'appData') {
                 $data->entry[$key] = $value->{'userAppData'};
             } else {
                 $data->entry[$key] = $value->{$model};
             }
         }
     } else {
         if (isset($data->{$model})) {
             $data = $data->{$model};
         }
     }
     $response['data'] = json_encode($data);
 }
 /**
  * Creates an activity.
  *
  * @param array $params the parameters defining the activity to create
  * @return osapiRequest the request
  */
 public function create($params) {
   // basic sanity checking of the request object
   if (!isset($params['userId'])) throw new osapiException("Missing 'userId' param for osapiActivities->create");
   if (!isset($params['groupId'])) throw new osapiException("Missing 'groupId' param for osapiActivities->create");
   if (!isset($params['activity'])) throw new osapiException("Missing 'activity' param for osapiActivities->create");
   if (!$params['activity'] instanceof osapiActivity) throw new osapiException("Activity param should be a osapiActivity in osapiActivities->create");
   // strip out the null values before we post the activity
   $params['activity'] = self::trimResponse($params['activity']);
   // add appId => @app if it is missing.
   if (!isset($params['appId'])) {
     $params['appId'] = '@app';
   }
   
   return osapiRequest::createRequest('activities.create', $params);
 }
 public function delete($params)
 {
     if (!isset($params['userId'])) {
         throw new osapiException("Invalid or no userId specified for osapiAppData->delete");
     }
     if (!isset($params['groupId'])) {
         throw new osapiException("Invalid or no groupId specified for osapiAppData->delete");
     }
     if (!isset($params['appId'])) {
         throw new osapiException("Invalid or no appId specified for osapiAppData->delete");
     }
     if (isset($params['fields'])) {
         if (!is_array($params['fields'])) {
             throw new osapiException("Optional param 'fields' should be an array in osapiAppData->delete");
         }
         foreach ($params['fields'] as $key) {
             if (!self::isValidKey($key)) {
                 throw new osapiException("Invalid key specified in osapiAppData->delete: {$key}");
             }
         }
     }
     return osapiRequest::createRequest('appdata.delete', $params);
 }
 /**
  * Sets status. Uses specific endpoint for this
  * Myspace specific
  * @return osapiRequest the request
  */
 public function update($params)
 {
     $params = array_merge($params, array('userId' => '@me', 'groupId' => '@self'));
     return osapiRequest::createRequest('statusmood.update', $params);
 }
 /**
  * Tests osapiRequest->createRequest()
  */
 public function testCreateRequestWithBadGroup()
 {
     $this->setExpectedException('osapiException');
     $request = osapiRequest::createRequest('people.get', array('userId' => true, 'groupId' => '@family'));
 }
 /**
  * Gets a users group.
  *
  * @param array $params the parameters defining which groups to fetch
  * @return osapiRequest the request
  */
 public function get($params)
 {
     return osapiRequest::createRequest('groups.get', $params);
 }
Example #12
0
 /**
  * Gets the application's viewer.
  *
  * @param array $params the parameters defining which people to retrieve
  * @return osapiRequest the request
  */
 public function getViewer($params)
 {
     return osapiRequest::createRequest('people.get', array_merge(array('userId' => '@viewer'), $params));
 }
Example #13
0
 /**
  * Upload mediaItem to an album
  *
  * @param array $params the parameters defining the album and mediaItem data to upload
  * @return osapiRequest the request
  */
 public function uploadContent($params)
 {
     return osapiRequest::createRequest('mediaItems.upload', $params);
 }
Example #14
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;
 }
 /**
  * Calls the system.listMethods method that returns an array
  * of supported RPC methods, ie something like:
  * array('people.get', 'activities.get', 'activities.create', etc)
  *
  * @return osapiRequest
  */
 public function listMethods()
 {
     return osapiRequest::createRequest('system.listMethods', array());
 }
 public function create($params)
 {
     $params = array_merge($params, array('userId' => '@me', 'groupId' => '@self'));
     return osapiRequest::createRequest('notifications.create', $params);
 }