예제 #1
0
 public function testDecrementRemaining()
 {
     $endpoint = new TwitterAPIEndpoint();
     $endpoint->setRemaining(15);
     $endpoint->decrementRemaining();
     $this->assertEqual(14, $endpoint->getRemaining());
 }
 /**
  * Make a Twitter API request.
  * @param TwitterAPIEndpoint $endpoint
  * @param arr $args URL query string parameters
  * @param str $id ID for use in endpoint path
  * @param bool $suppress_404_error Defaults to false, don't log 404 errors from deleted tweets
  * @return arr HTTP status code, payload
  */
 public function apiRequest(TwitterAPIEndpoint $endpoint, $args = array(), $id = null, $suppress_404_error = false)
 {
     $logger = Logger::getInstance();
     $attempts = 0;
     $continue = true;
     $url = $endpoint->getPathWithID($id);
     $is_rate_limit_check = $endpoint->getShortPath() == "/application/rate_limit_status";
     if ($is_rate_limit_check || $endpoint->isAvailable($this->percent_use_ceiling)) {
         while ($attempts <= $this->num_retries && $continue) {
             $content = $this->to->OAuthRequest($url, 'GET', $args);
             $status = $this->to->lastStatusCode();
             if (!$is_rate_limit_check) {
                 $endpoint->decrementRemaining();
                 $logger->logInfo($endpoint->getStatus(), __METHOD__ . ',' . __LINE__);
             }
             $status_message = "";
             if ($status > 200) {
                 $status_message = "Could not retrieve {$url}";
                 if (sizeof($args) > 0) {
                     $status_message .= "?";
                 }
                 foreach ($args as $key => $value) {
                     $status_message .= $key . "=" . $value . "&";
                 }
                 $translated_status_code = $this->translateErrorCode($status);
                 $status_message .= " | API ERROR: {$translated_status_code}";
                 //we expect a 404 when checking a tweet deletion, so suppress log line if defined
                 if ($status == 404) {
                     if ($suppress_404_error === false) {
                         $logger->logUserError($status_message, __METHOD__ . ',' . __LINE__);
                     }
                 } else {
                     //do log any other kind of error
                     $logger->logUserError($status_message, __METHOD__ . ',' . __LINE__);
                 }
                 $status_message = "";
                 if ($status != 404 && $status != 403) {
                     $attempts++;
                     if ($this->total_errors_so_far >= $this->total_errors_to_tolerate) {
                         $continue = false;
                     } else {
                         $this->total_errors_so_far = $this->total_errors_so_far + 1;
                         $logger->logUserInfo('Total API errors so far: ' . $this->total_errors_so_far . ' | Total errors to tolerate ' . $this->total_errors_to_tolerate, __METHOD__ . ',' . __LINE__);
                     }
                 } else {
                     $continue = false;
                 }
             } else {
                 $continue = false;
             }
         }
         return array($status, $content);
     } else {
         throw new APICallLimitExceededException("API call allocation limit reached. " . $endpoint->getStatus());
     }
 }