public function __construct(Client $client) { $this->client = $client; $timeout = $client->getClassConfig('Google\\IO\\NewAbstract', 'request_timeout_seconds'); if ($timeout > 0) { $this->setTimeout($timeout); } }
public function __construct(Client $client) { if (!ini_get('allow_url_fopen')) { $error = 'The stream IO handler requires the allow_url_fopen runtime ' . 'configuration to be enabled'; $client->getLogger()->critical($error); throw new Exception($error); } parent::__construct($client); }
public function __construct(Client $client) { if (!extension_loaded('curl')) { $error = 'The cURL IO handler requires the cURL extension to be enabled'; $client->getLogger()->critical($error); throw new Exception($error); } parent::__construct($client); }
/** * @visible for testing. * @param Google\Http\Request $request * @return Google\Http\Request|bool Returns the cached object or * false if the operation was unsuccessful. */ public function getCachedRequest(Request $request) { if (false === CacheParser::isRequestCacheable($request)) { return false; } return $this->client->getCache()->get($request->getCacheKey()); }
private function getResumeUri() { $result = null; $body = $this->request->getPostBody(); if ($body) { $headers = array('content-type' => 'application/json; charset=UTF-8', 'content-length' => Utils::getStrLen($body), 'x-upload-content-type' => $this->mimeType, 'x-upload-content-length' => $this->size, 'expect' => ''); $this->request->setRequestHeaders($headers); } $response = $this->client->getIo()->makeRequest($this->request); $location = $response->getResponseHeader('location'); $code = $response->getResponseHttpCode(); if (200 == $code && true == $location) { return $location; } $message = $code; $body = @json_decode($response->getResponseBody()); if (!empty($body->error->errors)) { $message .= ': '; foreach ($body->error->errors as $error) { $message .= "{$error->domain}, {$error->message};"; } $message = rtrim($message, ';'); } $error = "Failed to start the resumable upload (HTTP {$message})"; $this->client->getLogger()->error($error); throw new Exception($error); }
/** * Creates a new task runner with exponential backoff support. * * @param Google\Client $client The current API client * @param string $name The name of the current task (used for logging) * @param callable $action The task to run and possibly retry * @param array $arguments The task arguments * @throws Google\Task\Exception when misconfigured */ public function __construct(Client $client, $name, $action, array $arguments = array()) { $config = (array) $client->getClassConfig('Google\\Task\\Runner'); if (isset($config['initial_delay'])) { if ($config['initial_delay'] < 0) { throw new Exception('Task configuration `initial_delay` must not be negative.'); } $this->delay = $config['initial_delay']; } if (isset($config['max_delay'])) { if ($config['max_delay'] <= 0) { throw new Exception('Task configuration `max_delay` must be greater than 0.'); } $this->maxDelay = $config['max_delay']; } if (isset($config['factor'])) { if ($config['factor'] <= 0) { throw new Exception('Task configuration `factor` must be greater than 0.'); } $this->factor = $config['factor']; } if (isset($config['jitter'])) { if ($config['jitter'] <= 0) { throw new Exception('Task configuration `jitter` must be greater than 0.'); } $this->jitter = $config['jitter']; } if (isset($config['retries'])) { if ($config['retries'] < 0) { throw new Exception('Task configuration `retries` must not be negative.'); } $this->maxAttempts += $config['retries']; } if (!is_callable($action)) { throw new Exception('Task argument `$action` must be a valid callable.'); } $this->name = $name; $this->client = $client; $this->action = $action; $this->arguments = $arguments; }
public function parseResponse(Request $response) { $contentType = $response->getResponseHeader('content-type'); $contentType = explode(';', $contentType); $boundary = false; foreach ($contentType as $part) { $part = explode('=', $part, 2); if (isset($part[0]) && 'boundary' == trim($part[0])) { $boundary = $part[1]; } } $body = $response->getResponseBody(); if ($body) { $body = str_replace("--{$boundary}--", "--{$boundary}", $body); $parts = explode("--{$boundary}", $body); $responses = array(); foreach ($parts as $part) { $part = trim($part); if (!empty($part)) { list($metaHeaders, $part) = explode("\r\n\r\n", $part, 2); $metaHeaders = $this->client->getIo()->getHttpResponseHeaders($metaHeaders); $status = substr($part, 0, strpos($part, "\n")); $status = explode(" ", $status); $status = $status[1]; list($partHeaders, $partBody) = $this->client->getIo()->ParseHttpResponse($part, false); $response = new Request(""); $response->setResponseHttpCode($status); $response->setResponseHeaders($partHeaders); $response->setResponseBody($partBody); // Need content id. $key = $metaHeaders['content-id']; if (isset($this->expected_classes[$key]) && strlen($this->expected_classes[$key]) > 0) { $class = $this->expected_classes[$key]; $response->setExpectedClass($class); } try { $response = REST::decodeHttpResponse($response, $this->client); $responses[$key] = $response; } catch (Exception $e) { // Store the exception as the response, so successful responses // can be processed. $responses[$key] = $e; } } } return $responses; } return null; }
/** * Decode an HTTP Response. * @static * @throws Google\Service\Exception * @param Google\Http\Request $response The http response to be decoded. * @param Google\Client $client * @return mixed|null */ public static function decodeHttpResponse($response, Client $client = null) { $code = $response->getResponseHttpCode(); $body = $response->getResponseBody(); $decoded = null; if (intVal($code) >= 300) { $decoded = json_decode($body, true); $err = 'Error calling ' . $response->getRequestMethod() . ' ' . $response->getUrl(); if (isset($decoded['error']) && isset($decoded['error']['message']) && isset($decoded['error']['code'])) { // if we're getting a json encoded error definition, use that instead of the raw response // body for improved readability $err .= ": ({$decoded['error']['code']}) {$decoded['error']['message']}"; } else { $err .= ": ({$code}) {$body}"; } $errors = null; // Specific check for APIs which don't return error details, such as Blogger. if (isset($decoded['error']) && isset($decoded['error']['errors'])) { $errors = $decoded['error']['errors']; } $map = null; if ($client) { $client->getLogger()->error($err, array('code' => $code, 'errors' => $errors)); $map = $client->getClassConfig('Google\\Service\\Exception', 'retry_map'); } throw new Exception($err, $code, null, $errors, $map); } // Only attempt to decode the response, if the response code wasn't (204) 'no content' if ($code != '204') { if ($response->getExpectedRaw()) { return $body; } $decoded = json_decode($body, true); if ($decoded === null || $decoded === "") { $error = "Invalid json in service response: {$body}"; if ($client) { $client->getLogger()->error($error); } throw new Exception($error); } if ($response->getExpectedClass()) { $class = $response->getExpectedClass(); $decoded = new $class($decoded); } } return $decoded; }
public function __construct(Client $client) { if (!function_exists('memcache_connect') && !class_exists("Memcached")) { $error = "Memcache functions not available"; $client->getLogger()->error($error); throw new Exception($error); } $this->client = $client; if ($client->isAppEngine()) { // No credentials needed for GAE. $this->mc = new Memcached(); $this->connection = true; } else { $this->host = $client->getClassConfig($this, 'host'); $this->port = $client->getClassConfig($this, 'port'); if (empty($this->host) || empty($this->port) && (string) $this->port != "0") { $error = "You need to supply a valid memcache host and port"; $client->getLogger()->error($error); throw new Exception($error); } } }
/** * @param Google\Client $client The current Google client */ public function __construct(Client $client) { $this->setLevel($client->getClassConfig('Google\\Logger\\NewAbstract', 'level')); $format = $client->getClassConfig('Google\\Logger\\NewAbstract', 'log_format'); $this->logFormat = $format ? $format : self::DEFAULT_LOG_FORMAT; $format = $client->getClassConfig('Google\\Logger\\NewAbstract', 'date_format'); $this->dateFormat = $format ? $format : self::DEFAULT_DATE_FORMAT; $this->allowNewLines = (bool) $client->getClassConfig('Google\\Logger\\NewAbstract', 'allow_newlines'); }
/** * Sleeps in accordance to the backoff configurations. */ private function backOff() { $delay = $this->getDelay(); $this->client->getLogger()->debug('Retrying task with backoff', array('request' => $this->name, 'retry' => $this->attempts, 'backoff_seconds' => $delay)); usleep($delay * 1000000); }