/** * Adjusts a request prior to being sent in order to fix container-specific * bugs. * @param mixed $request The osapiRequest object being processed, or an array * of osapiRequest objects. * @param string $method The HTTP method used for this request. * @param string $url The url being fetched for this request. * @param array $headers The headers being sent in this request. * @param osapiAuth $signer The signing mechanism used for this request. */ public function preRequestProcess(&$request, &$method, &$url, &$headers, osapiAuth &$signer) { // Using the full myspace ID in the xoauth_requestor_id doesn't work if ($signer instanceof osapiOAuth2Legged) { $signer->setUserId(str_replace('myspace.com.person.', '', $signer->getUserId())); } if($request->method == 'appdata.update' || $request->method == 'appdata.create') { $this->formatAppDataOut($request); } }
/** * 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; }
public static function sendBatch(array $requests, osapiProvider $provider, osapiAuth $signer, $strictMode = false) { $request = json_encode($requests); $signedUrl = $signer->sign('POST', $provider->rpcEndpoint, array(), $request); $ret = self::send($signedUrl, 'POST', $provider->httpProvider, $request); if ($ret['http_code'] == '200') { $result = json_decode($ret['data'], true); // the decode result and input string being the same indicates a decoding failure if ($result == $ret['data']) { throw new osapiException("Error decoding response body:\n" . $ret['data']); } if (isset($result['code']) && $result['code'] == '401') { throw new osapiAuthError("Authentication error: {$result['message']}"); } if (is_array($result)) { // rewrite the result set into a $key => $response set $ret = array(); foreach ($result as $entry) { $requestType = ''; foreach ($requests as $request) { if (isset($entry['id']) && $request->id == $entry['id']) { $requestType = $request->getService($request->method); break; } } if (isset($entry['error'])) { $entry['data'] = new osapiError(isset($entry['error']['code']) ? $entry['error']['code'] : 500, isset($entry['error']['message']) ? $entry['error']['message'] : 'Unknown error occured'); unset($entry['error']); } else { if (isset($entry['data']['entry'])) { if ($strictMode) { throw new osapiException("Invalid RPC response body, collection key should be a 'list' and not 'entry'"); } else { $entry['data']['list'] = $entry['data']['entry']; } } if (isset($entry['data']['list'])) { // first see if we can convert each individual response entry into a typed object if (isset($entry['id'])) { // map back to the original request so we can determine the expected type foreach ($entry['data']['list'] as $key => $val) { $entry['data']['list'][$key] = self::convertArray($requestType, $val, $strictMode); } } $entry['data'] = self::listToCollection($entry['data'], $strictMode); } else { // convert the response into a type implementation if no error occured if (isset($entry['data'])) { $entry['data'] = self::convertArray($requestType, $entry['data'], $strictMode); } } } if (isset($entry['id'])) { // remove the request id from the data array, and use that id to store it in the results array $id = $entry['id']; unset($entry['id']); $ret[$id] = isset($entry['data']) ? $entry['data'] : $entry; } else { // no request id specified, store it as-is $ret[] = $entry; } } return $ret; } else { return $result; } } else { throw new osapiException("Error sending RPC request: " . $ret['data']); } }