Пример #1
0
 /**
  * Performs generic request.
  *
  * @param array $curlOptArray the options array for CURL
  * @param \iveeCrest\Response $response object
  * @param bool $cache whether the response should be cached. If using another caching layer, it is advisable to
  * disabled it here to prevent redundant caching.
  *
  * @return void
  * @throws \iveeCrest\Exceptions\CrestException on http return codes other than 200 and 302
  */
 protected function doRequest(array $curlOptArray, Response $response, $cache = true)
 {
     //set the curl options
     curl_setopt_array($this->ch, $curlOptArray);
     //execute request
     $resBody = curl_exec($this->ch);
     $info = curl_getinfo($this->ch);
     $err = curl_errno($this->ch);
     $errmsg = curl_error($this->ch);
     if ($err != 0) {
         $crestExceptionClass = Config::getIveeClassName('CrestException');
         throw new $crestExceptionClass($errmsg, $err);
     }
     if (!in_array($info['http_code'], array(200, 302))) {
         $crestExceptionClass = Config::getIveeClassName('CrestException');
         throw new $crestExceptionClass('HTTP response not OK: ' . (int) $info['http_code'] . '. Response body: ' . $resBody, $info['http_code']);
     }
     //set data to response and cache it
     $response->setContentAndInfo($resBody, $info);
     if ($cache) {
         $this->cache->setItem($response);
     }
 }
Пример #2
0
 /**
  * Processes CREST market order responses. It is assumed this method will only be called in batch mode.
  *
  * CREST splits buy and sell orders into two separate calls, but they must be processed together (we dont't want to
  * deal with partial DB updates). Async CREST calls can return in any order, so we must pair each buy order to its
  * matching sell order or vice versa by buffering whichever response comes first before processing them atomically.
  *
  * @param \iveeCore\Response $response to be processed
  *
  * @return void
  * @throws \iveeCore\Exceptions\UnexpectedDataException if Response with wrong representation is passed
  */
 protected function processOrderResponse(Response $response)
 {
     //check for correct CREST response representation
     $expectedRepresentation = 'application/' . EndpointHandler::MARKET_ORDER_COLLECTION_REPRESENTATION;
     if ($response->getContentType() != $expectedRepresentation) {
         $exceptionClass = Config::getIveeClassName('UnexpectedDataException');
         throw new $exceptionClass('Representation of CREST Response was ' . $response->getContentType() . ', ' . $expectedRepresentation . ' expected');
     }
     //extract Ids from the URL
     $urlComponents = parse_url($response->getInfo()['url']);
     $pathComponents = explode('/', $urlComponents['path']);
     $regionId = (int) $pathComponents[2];
     $typeId = (int) explode('/', $urlComponents['query'])[4];
     $key = $regionId . '_' . $typeId;
     //Instantiate stdClass object if necessary
     if (!isset($this->orderResponseBuffer[$key])) {
         $this->orderResponseBuffer[$key] = new \stdClass();
     }
     //we decide between buy and sell based on the url instead of the items in the response because potentially
     //empty sets could be returned
     if ($pathComponents[4] == 'buy') {
         $this->orderResponseBuffer[$key]->buyOrders = $response->content->items;
     } else {
         $this->orderResponseBuffer[$key]->sellOrders = $response->content->items;
     }
     //if buy and sell order data has been matched, process it
     if (isset($this->orderResponseBuffer[$key]->buyOrders) and isset($this->orderResponseBuffer[$key]->sellOrders)) {
         $this->processOrderData($this->orderResponseBuffer[$key], $typeId, $regionId);
         //unset data when done to preserve memory
         unset($this->orderResponseBuffer[$key]);
     }
 }