/** * Constructor * @param Data $helper * @param \Koklu\Recommender\Logger\Event $eventLogger * @param \Koklu\Recommender\Logger\Recommender $recommenderLogger * @throws \Zend_Http_Client_Exception */ public function __construct(Data $helper, \Koklu\Recommender\Logger\Event $eventLogger, \Koklu\Recommender\Logger\Recommender $recommenderLogger) { $this->_helper = $helper; $this->_eventLogger = $eventLogger; $this->_recommendLogger = $recommenderLogger; $this->_client = new \Koklu\Rest\Json\Client($helper->getApiBaseUrl()); $this->_client->getHttpClient()->setConfig(['keepalive' => true, 'useragent' => 'Magento2 Recommender Client', 'maxredirects' => 0])->setUnmaskStatus(true); }
/** * Starts the client by using the given rest server url * * @param $url */ public function startClient($url) { $this->client = new Zend_Rest_Client($url); // Get instance of Zend_Http_Client $httpClient = $this->client->getHttpClient(); // Change the timeout $httpClient->setConfig(array("timeout" => 7)); }
/** * Perform a web content search on search.yahoo.com. A basic query * consists simply of a text query. Additional options that can be * specified consist of: * 'results' => int How many results to return, max is 50 * 'start' => int The start offset for search results * 'language' => lang The target document language to match * 'type' => (all|any|phrase) How the query should be parsed * 'site' => string A site to which your search should be restricted * 'format' => (any|html|msword|pdf|ppt|rss|txt|xls) * 'adult_ok' => bool permit 'adult' content in the search results * 'similar_ok' => bool permit similar results in the result set * 'country' => string The country code for the content searched * 'license' => (any|cc_any|cc_commercial|cc_modifiable) The license of content being searched * 'region' => The regional search engine on which the service performs the search. default us. * * @param string $query the query being run * @param array $options any optional parameters * @return Zend_Service_Yahoo_WebResultSet The return set * @throws Zend\Service\Exception */ public function webSearch($query, array $options = array()) { static $defaultOptions = array('type' => 'all', 'start' => 1, 'results' => 10, 'format' => 'any'); $options = $this->_prepareOptions($query, $options, $defaultOptions); $this->_validateWebSearch($options); $this->_rest->getHttpClient()->resetParameters(); $this->_rest->setUri('http://search.yahooapis.com'); $response = $this->_rest->restGet('/WebSearchService/V1/webSearch', $options); if ($response->isError()) { throw new Zend\Service\Exception('An error occurred sending request. Status code: ' . $response->getStatus()); } $dom = new DOMDocument(); $dom->loadXML($response->getBody()); self::_checkErrors($dom); return new Zend_Service_Yahoo_WebResultSet($dom); }
/** * Look up item(s) by ASIN * * @param string $asin Amazon ASIN ID * @param array $options Query Options * @see http://www.amazon.com/gp/aws/sdk/main.html/102-9041115-9057709?s=AWSEcommerceService&v=2005-10-05&p=ApiReference/ItemLookupOperation * @throws Zend_Service_Exception * @return Zend_Service_Amazon_Item|Zend_Service_Amazon_ResultSet */ public function itemLookup($asin, array $options = array()) { $defaultOptions = array('IdType' => 'ASIN', 'ResponseGroup' => 'Small'); $options['ItemId'] = (string) $asin; $options = $this->_prepareOptions('ItemLookup', $options, $defaultOptions); $this->_rest->getHttpClient()->resetParameters(); $response = $this->_rest->restGet('/onca/xml', $options); if ($response->isError()) { /** * @see Zend_Service_Exception */ require_once 'Zend/Service/Exception.php'; throw new Zend_Service_Exception('An error occurred sending request. Status code: ' . $response->getStatus()); } $dom = new DOMDocument(); $dom->loadXML($response->getBody()); self::_checkErrors($dom); $xpath = new DOMXPath($dom); $xpath->registerNamespace('az', 'http://webservices.amazon.com/AWSECommerceService/2005-10-05'); $items = $xpath->query('//az:Items/az:Item'); if ($items->length == 1) { /** * @see Zend_Service_Amazon_Item */ require_once 'Zend/Service/Amazon/Item.php'; return new Zend_Service_Amazon_Item($items->item(0)); } /** * @see Zend_Service_Amazon_ResultSet */ require_once 'Zend/Service/Amazon/ResultSet.php'; return new Zend_Service_Amazon_ResultSet($dom); }
/** * Set special headers for request * * @param string $apiToken * @param string $apiVersion * @param string $requestSignature * @return void */ protected function _setHeaders($apiToken, $apiVersion, $requestSignature = null) { $headers = array('User-Agent' => 'TicketEvolution_Webservice', 'X-Token' => (string) $apiToken, 'Accept' => (string) 'application/json'); if (!empty($requestSignature)) { $headers['X-Signature'] = (string) $requestSignature; } $this->_rest->getHttpClient()->setHeaders($headers); }
/** * Call corresponding web service method and return the results. * * @deprecated Zend_Rest_Client does not format URI accordingly * @param array $params * @return array The parsed packstation data * @throws Dhl_Account_Exception */ public function findPackstations(array $params) { /* @var $helper Dhl_Account_Helper_Data */ $helper = Mage::helper('dhlaccount/data'); $params = $this->getDefaultParams() + $params; try { $result = $this->getClient()->postfinder($params)->get(); } catch (Zend_Rest_Client_Result_Exception $e) { $response = $this->client->getHttpClient()->getLastResponse(); $helper->log($this->client->getHttpClient()->getLastRequest()); $helper->log(sprintf("%s\nHTTP/%s %s %s", $e->getMessage(), $response->getVersion(), $response->getStatus(), $response->getMessage())); throw new Dhl_Account_Exception($helper->__('An error occured while retrieving the packstation data.')); } // TODO(nr): transform web service response to usable output. return $result; }
/** * Perform a web content search on search.yahoo.com. A basic query * consists simply of a text query. Additional options that can be * specified consist of: * 'results' => int How many results to return, max is 50 * 'start' => int The start offset for search results * 'language' => lang The target document language to match * 'type' => (all|any|phrase) How the query should be parsed * 'site' => string A site to which your search should be restricted * 'format' => (any|html|msword|pdf|ppt|rss|txt|xls) * 'adult_ok' => bool permit 'adult' content in the search results * 'similar_ok' => bool permit similar results in the result set * 'country' => string The country code for the content searched * 'license' => (any|cc_any|cc_commercial|cc_modifiable) The license of content being searched * 'region' => The regional search engine on which the service performs the search. default us. * * @param string $query the query being run * @param array $options any optional parameters * @return Zend_Service_Yahoo_WebResultSet The return set * @throws Zend_Service_Exception */ public function webSearch($query, array $options = array()) { static $defaultOptions = array('type' => 'all', 'start' => 1, 'results' => 10, 'format' => 'any'); $options = $this->_prepareOptions($query, $options, $defaultOptions); $this->_validateWebSearch($options); $this->_rest->getHttpClient()->resetParameters(); $this->_rest->setUri('http://search.yahooapis.com'); $response = $this->_rest->restGet('/WebSearchService/V1/webSearch', $options); if ($response->isError()) { /** * @see Zend_Service_Exception */ require_once LIB_DIR . '/Zend/Service/Exception.php'; throw new Zend_Service_Exception('An error occurred sending request. Status code: ' . $response->getStatus()); } $dom = new DOMDocument(); $dom = Zend_Xml_Security::scan($response->getBody(), $dom); self::_checkErrors($dom); /** * @see Zend_Service_Yahoo_WebResultSet */ require_once LIB_DIR . '/Zend/Service/Yahoo/WebResultSet.php'; return new Zend_Service_Yahoo_WebResultSet($dom); }
public function testRestPut() { $expXml = file_get_contents($this->path . 'returnString.xml'); $response = "HTTP/1.0 200 OK\r\n" . "X-powered-by: PHP/5.2.0\r\n" . "Content-type: text/xml\r\n" . "Content-length: " . strlen($expXml) . "\r\n" . "Server: Apache/1.3.34 (Unix) PHP/5.2.0)\r\n" . "Date: Tue, 06 Feb 2007 15:01:47 GMT\r\n" . "Connection: close\r\n" . "\r\n" . $expXml; $this->adapter->setResponse($response); $reqXml = file_get_contents($this->path . 'returnInt.xml'); $response = $this->rest->restPut('/rest/', $reqXml); $this->assertTrue($response instanceof Zend_Http_Response); $body = $response->getBody(); $this->assertContains($expXml, $response->getBody()); $request = Zend_Rest_Client::getHttpClient()->getLastRequest(); $this->assertContains($reqXml, $request, $request); }
protected function setUp() { $this->_finding = new Zend_Service_Ebay_Finding(constant('TESTS_ZEND_SERVICE_EBAY_ONLINE_APPID')); $this->_httpClientOriginal = Zend_Rest_Client::getHttpClient(); Zend_Rest_Client::setHttpClient(new Zend_Http_Client()); }
protected function _getClient($baseUrl, $includeAccessToken = true) { $client = new Zend_Rest_Client($baseUrl); $headers = array('Accept' => 'application/json'); if ($includeAccessToken) { $headers['Authorization'] = 'Bearer ' . $this->_getAccessToken(); } $client->getHttpClient()->setHeaders($headers); return $client; }
/** * @group EricssonListPagingInt */ public function testFindAllByMasterFilterAlias() { Zend_Rest_Client::getHttpClient()->setConfig(array('timeout' => 60)); $rawFilters = array('alias' => 'alias'); $filterList = SimService::getInstance()->buildFilterList($rawFilters); $filters = array('organizationId' => Application\Model\Organization\OrgMasterModel::ORG_TYPE . '-' . 'master11111111111111111111111111', 'filterList' => $filterList); $sims = $this->simMapper->findAll($filters, array('count' => 100)); $this->assertNotNull($sims); $this->assertGreaterThanOrEqual('1', $sims->getCount()); }
/** * check whether the bar is active * * @return array */ public function getActiveState() { $csConfig = Mage::getModel("commercesciences_base/config")->load("1"); if (!$csConfig) { //we must be already at least on step 1, so the DB record has to exist Mage::log("Error - no csConfig"); return array('error' => $this->__("Error ocurred. Your updates weren't saved. Please contact ComemrceScience for support (error id: 005)")); } Mage::log("csConfig=" . print_r($csConfig, true), true); // TODO Ron Gross 2/1/2013 - refactor into a method $RESTClient = new Zend_Rest_Client($csConfig->getCsApiUrl()); $httpClient = $RESTClient->getHttpClient(); $httpClient->setConfig(array("timeout" => 30)); try { $response = $RESTClient->restGet("/magento/getBarStatus", array('userID' => $csConfig->getUserId(), 'securityToken' => $csConfig->getSecurityToken())); $responseJson = $response->getBody(); $parsedResponseArr = $this->stdObject2Array(json_decode($responseJson)); if (!isset($parsedResponseArr['good'])) { Mage::log("Server busy"); return array('error' => $this->__("The CommerceSciences server is currently busy, your updates weren't saved. Please try again later. (error id: 006)")); } if ($parsedResponseArr['good'] == false) { if (isset($parsedResponseArr['fieldErrors']) && $parsedResponseArr['fieldErrors']) { $fieldErrorsArr = $this->stdObject2Array($parsedResponseArr['fieldErrors']); $errorMsg = ''; foreach ($fieldErrorsArr as $field => $fError) { $errorMsg .= "<br />"; $errorMsg .= $this->__($field) . ": " . $this->__($fError); } $errorMsg = substr($errorMsg, 0, strlen($errorMsg) - 1); Mage::log("Error (fieldErrors) - " . $errorMsg); return array('error' => $errorMsg); } elseif (isset($parsedResponseArr['globalError']) && $parsedResponseArr['globalError']) { Mage::log("Error (globalError) - " . $parsedResponseArr['globalError']); return array('error' => $parsedResponseArr['globalError']); } } Mage::log("Returning data: " . $parsedResponseArr['data']); return array('error' => false, 'data' => $parsedResponseArr['data']); } catch (Exception $e) { Mage::log("Got error: " . print_r($e, true)); return array('error' => $this->__($e->getMessage())); } }
/** * Perform a rest post throwing an exception if result failed * * @param string $method * @param array $options * @param array $defaultOptions * @return Zend_Rest_Client_Result */ protected function _restFileUpload($method, $file, $param, array $options, array $defaultOptions = array()) { $options = $this->_prepareOptions($method, $options, $defaultOptions); $client = Zend_Rest_Client::getHttpClient(); $client->setUri($this->getScribdClient()->getRestClient()->getUri()); $client->setParameterGet($options); $client->setFileUpload($file, $param); $response = $client->request('POST'); if ($response->isError()) { $code = $response->extractCode($response->asString()); /** * @see Zym_Service_Scribd_Exception */ require_once 'Zym/Service/Scribd/Exception.php'; throw new Zym_Service_Scribd_Exception($response->getMessage(), $code); } return $this->_handleResponse($response); }
/** * * @throws apiException * @param string $name * @param array $arguments * @return DatatypeAbstract objet représentant les données du web service. */ public function __call($name, $arguments) { $iIdRessource = "index"; if (count($arguments) != 2) { throw new \SitecRESA\Exception\Api("client method calls expect 2 parameter"); } if (!is_array($arguments[1])) { throw new \SitecRESA\Exception\Api("client arguments parameter should be an array"); } $aParams = $arguments[1]; $sVerbe = $arguments[0]; if (array_key_exists('idRessource', $aParams)) { $iIdRessource = $aParams['idRessource']; unset($aParams['idRessource']); } if (!array_key_exists('format', $aParams)) { $aParams['format'] = self::FORMAT; } $aParams['requestHash'] = hash_hmac("sha1", $this->sApiKey . '-' . time(), $this->sSecretKey); $aParams['apiKey'] = $this->sApiKey; $aParams['timestamp'] = time(); $aParams['version'] = $this->version; $name = self::PREFIX_PATH . $name; if ($this->panier && $this->panier instanceof \SitecRESA\Datatype\Panier) { $aParams["identifiantPanier"] = $this->panier->id; } $client = \Zend_Rest_Client::getHttpClient(); if (count($aParams) > 0) { if (!isset($aParams["dateDebut"])) { $aParams["dateDebut"] = null; } if (!isset($aParams["dateFin"])) { $aParams["dateFin"] = null; } $client->setHeaders($aParams); } switch ($sVerbe) { case "post": $response = $this->client->restPost($name, $aParams); break; case "put": $response = $this->client->restPut($name . "/" . $iIdRessource, $aParams); break; case "get": $response = $this->client->restGet($name . "/get/" . $iIdRessource, $aParams); break; case "delete": $response = $this->client->restDelete($name . "/" . $iIdRessource); break; default: throw new \SitecRESA\Exception\Api("{$sVerbe} n'est pas une requête HTTP gérée par ce Web Service"); } if ($response->getStatus() == 401) { throw new \SitecRESA\Exception\Auth($response->getBody() . " status : " . $response->getStatus(), $response->getStatus()); } if ($response->getStatus() == 412) { return $this->doResponse($response->getBody(), $aParams['format']); //throw new \SitecRESA\Exception\IO($response->getBody(),$response->getStatus() ); } if ($response->getStatus() == 201) { //créé et l'accès est disponible return $response->getHeader("Location"); } if ($response->getStatus() == 204) { //pas de contenu. return; } if ($response->getStatus() != 200) { throw new \SitecRESA\Exception\Api($response->getBody(), $response->getStatus()); } return $this->doResponse($response->getBody(), $aParams['format']); }
/** * Log API call (request and response) * @param \Zend_Rest_Client $client * @param \Exception $exception * @return void */ public function logApiCallException(\Zend_Rest_Client $client, \Exception $exception) { $this->info($client->getHttpClient()->getLastRequest()); $this->err($exception->getMessage()); }
/** * * @return void */ public function setUp() { Zend_Rest_Client::getHttpClient()->setConfig(array('useragent' => 'Zend_Service_Delicious - Unit tests/0.1', 'keepalive' => true)); $this->_delicious = new Zend_Service_Delicious(self::TEST_UNAME, self::TEST_PASS); }
/** * Set special headers for request * * @param string $apiToken * @param string $apiVersion * @return void */ protected function _setHeaders($apiToken, $apiVersion, $requestSignature = null) { $headers = array('User-Agent' => 'teamonetickets/razorgator-php', 'Accept' => 'application/xml'); $this->_rest->getHttpClient()->setHeaders($headers); }