/** * Sends a request to the API for getting device capabilities. * * @return DCResponse API response. * @throws ServiceException if API request was not successful */ public function getDeviceInformation() { $endpoint = $this->getFqdn() . '/rest/2/Devices/Info'; $req = new RESTFulRequest($endpoint); $result = $req->setAuthorizationHeader($this->getToken())->setHeader('Accept', 'application/json')->sendHttpGet(); $arr = Service::parseJson($result); return DCResponse::fromArray($arr); }
public function testParseJson() { $body = '{}'; $r = new RestfulResponse($body, 204, array()); try { Service::parseJson($r); // should throw exception $this->assertTrue(false); } catch (Exception $e) { } try { $codes = array(200, 204); Service::parseJson($r, $codes); // should NOT throw exception } catch (Exception $e) { $this->assertTrue(false); } }
/** * Parses the result received from sending an API request for an OAuth * token. * * @param array $result the result returned from a restful request * * @return OAuthToken oauth token if request was successful * @throws OAuthException if request was not successful * @see Service::parseResult() */ protected function parseResult($result) { $tokenResponse = Service::parseJson($result); if (!isset($tokenResponse['access_token'])) { throw new OAuthException('Parse', 'No access token in response.'); } if (!isset($tokenResponse['expires_in'])) { throw new OAuthException('Parse', 'No expires_in in response.'); } if (!isset($tokenResponse['refresh_token'])) { throw new OAuthException('Parse', 'No refresh_token in response.'); } return new OAuthToken($tokenResponse['access_token'], $tokenResponse['expires_in'], $tokenResponse['refresh_token']); }
/** * Sends a request to the API for getting delivery status of an MMS. * * @param string $mmsId MMS Id for which to get status. * * @return MMSStatusResponse API response. * @throws ServiceException if API request was not successful. */ public function getMMSStatus($mmsId) { $endpoint = $this->getFqdn() . '/mms/v3/messaging/outbox/' . $mmsId; $req = new RESTFULRequest($endpoint); $result = $req->setHeader('Accept', 'application/json')->setAuthorizationHeader($this->getToken())->sendHttpGet(); $arr = Service::parseJson($result); return Status::fromArray($arr); }
/** * Sends a request to the api for generating a signed document and * signature, both of which will be contained within the returned notary * object. * * @param string $payload payload used to generate signature and signed * document * * @return Notary notary generated from payload * @throws ServiceException if api response isn't successful */ public function getNotary($payload) { $endpoint = $this->_fqdn . '/Security/Notary/Rest/1/SignedPayload'; $req = new RestfulRequest($endpoint); $httpPost = new HttpPost(); $this->_setHeaders($req); $httpPost->setBody($payload); $result = $req->sendHttpPost($httpPost); $responseArr = Service::parseJson($result); return new Notary($responseArr['SignedDocument'], $responseArr['Signature'], $payload); }
public function getNotificationConnectionDetails($queues) { $endpoint = $this->getFqdn() . '/myMessages/v2/notificationConnectionDetails'; $req = new RestfulRequest($endpoint); $req->setHeader('Accept', 'application/json')->setAuthorizationHeader($this->getToken()); $httpGet = new HttpGet(); $httpGet->setParam('queues', $queues); $result = $req->sendHttpGet($httpGet); $arr = Service::parseJson($result); return IMMNNotificactionCD::fromArray($arr); }
/** * Sends a request to the API for getting any SMS messages that were sent * to the specified short code. * * @param string $shortCode gets messages sent to this short code * * @return GetSMSResponse API response * @throws ServiceException if API request was not successful */ public function getMessages($shortCode, $raw_response = false) { $endpoint = $this->getFqdn() . '/sms/v3/messaging/inbox/' . urlencode($shortCode); $req = new RESTFulRequest($endpoint); $result = $req->setAuthorizationHeader($this->getToken())->setHeader('Accept', 'application/json')->setHeader('Content-Type', 'application/x-www-form-urlencoded')->sendHttpGet(); // Handle the flag to return json. if ($raw_response) { $body = Service::parseApiResposeBody($result); // Note: This could throw ServiceExeption return $body; } $arr = Service::parseJson($result); return GetSMSResponse::fromArray($arr); }
/** * Sends a custom API request for converting speech to text. * * @param string $cntxt speech context. * @param string $fname path to file that contains speech to convert. * @param string $gfname path to file that contains grammar. * @param string $dfname path to file that contains dictionary. * @param string $xArg optional arguments. * @param string $lang language used to set the Content-Language header. * * @return array API response as an array of key-value pairs. * @throws ServiceException if API request was not successful. */ public function speechToTextCustom($cntxt, $fname, $gfname = null, $dfname = null, $xArg = null, $lang = 'en-US') { $endpoint = $this->getFqdn() . '/speech/v3/speechToTextCustom'; $mpart = new SpeechMultipartBody(); $req = new RESTFulRequest($endpoint); $req->setHeader('X-SpeechContext', $cntxt)->setHeader('Accept', 'application/json')->setHeader('Content-Language', $lang)->setAuthorizationHeader($this->getToken()); if ($xArg != null) { $req->setHeader('X-Arg', $xArg); } if ($dfname != null) { $mpart->addXDictionaryPart($dfname); } if ($gfname != null) { $mpart->addXGrammarPart($gfname); } $mpart->addFilePart($fname); $result = $req->sendHttpMultipart($mpart); return Service::parseJson($result); }
/** * Sends a request to the API for getting an advertisement. * * @param string $category category of this app. * @param string $userAgent user agent string to send to API. * @param string $udid specifies a universially unique * identifier, which must be at least 30 * characters in length. * @param OptArgs|null $optArgs any optional values. * * @return null|ADSResponse null if no ads were returned, * otherwise an ADSResponse object * @throws ServiceException if API request was not successful */ public function getAdvertisement($category, $userAgent, $udid, OptArgs $optArgs = null, $raw_response = false) { $endpoint = $this->getFqdn() . '/rest/1/ads'; $req = new RestfulRequest($endpoint); $req->setAuthorizationHeader($this->getToken())->setHeader('User-agent', $userAgent)->setHeader('Udid', $udid); $httpGet = new HttpGet(); $httpGet->setParam('Category', $category); if ($optArgs != null) { $this->_appendOptArgs($httpGet, $optArgs); } $result = $req->sendHttpGet($httpGet); // no ads returned if ($result->getResponseCode() == 204) { if ($raw_response) { return $result->getResponseBody(); } return null; } if ($raw_response) { return Service::parseApiResposeBody($result); } // response as json array $jarr = Service::parseJson($result); return ADSResponse::fromArray($jarr); }
/** * Sends a request to the API for getting any SMS messages that were sent * to the specified short code. * * @param string $shortCode gets messages sent to this short code * * @return GetSMSResponse API response * @throws ServiceException if API request was not successful */ public function getMessages($shortCode) { $endpoint = $this->getFqdn() . '/sms/v3/messaging/inbox/' . urlencode($shortCode); $req = new RESTFulRequest($endpoint); $result = $req->setAuthorizationHeader($this->getToken())->setHeader('Accept', 'application/json')->setHeader('Content-Type', 'application/x-www-form-urlencoded')->sendHttpGet(); $arr = Service::parseJson($result); return GetSMSResponse::fromArray($arr); }
/** * Sends a request to the API gateway for getting subscriber's personal * contact card. * * @return Contact contact information for subscriber * @throws ServiceException if request was not successful */ public function getMyInfo() { $endpoint = $this->getFqdn() . '/addressBook/v1/myInfo'; $req = new RESTFulRequest($endpoint); $req->setAuthorizationHeader($this->getToken())->setHeader('Accept', 'application/json'); $result = $req->sendHttpGet(); $successCodes = array(200); $arr = Service::parseJson($result, $successCodes); return Contact::fromArray($arr['myInfo']); }
public function getNotificationSubscription($channelId, $subscriptionId) { $channelId = urlencode($channelId); $subscriptionId = urlencode($subscriptionId); $suburl = '/notification/v1/channels/' . $channelId . '/subscriptions/' . $subscriptionId; $endpoint = $this->getFqdn() . $suburl; $req = new RestfulRequest($endpoint); $req->setAuthorizationHeader($this->getToken())->setHeader('Accept', 'application/json'); $result = $req->sendHttpGet(); $contentType = $result->getHeader('content-type'); $systemTransId = $result->getHeader('x-systemTransactionId'); $successCodes = array(200); $arr = Service::parseJson($result, $successCodes); $arrSubscription = $arr['subscription']; $arrSubscriptionId = $arrSubscription['subscriptionId']; $arrExpiresIn = $arrSubscription['expiresIn']; /* TODO: remove work-around for events/eventFilters check */ $arrEvents = null; if (isset($arrSubscription['events'])) { $arrEvents = $arrSubscription['events']; } else { $arrEvents = $arrSubscription['eventFilters']; } $arrCallbackData = $arrSubscription['callbackData']; return new GetSubscriptionResponse($contentType, $arrSubscriptionId, $arrExpiresIn, $arrEvents, $arrCallbackData, $systemTransId); }
/** * Internal function used for sending common transaction operation * statuses, such as refunding a transaction or cancelling a subscription. * * @param string $rReasonTxt reason for refunding * @param string $rReasonCode reason code for refunding * @param string $transOptStatus transaction operation status * (e.g. Refunded). * @param string $url URL used for sending request * * @return string api response * @throws ServiceException if api request was not successful */ private function _sendTransOptStatus($rReasonTxt, $rReasonCode, $transOptStatus, $url) { $req = new RestfulRequest($url); $bodyArr = array('TransactionOperationStatus' => $transOptStatus, 'RefundReasonCode' => $rReasonCode, 'RefundReasonText' => $rReasonTxt); $httpPut = new HttpPut(json_encode($bodyArr)); $req->setHeader('Accept', 'application/json')->setAuthorizationHeader($this->getToken())->setHeader('Content-Type', 'application/json'); $result = $req->sendHttpPut($httpPut); return Service::parseJson($result); }