コード例 #1
0
ファイル: ApiProxyMock.php プロジェクト: bitcpf/djangoage
 public function init($testcase)
 {
     $this->expected = array();
     $this->testcase = $testcase;
     $this->verified = false;
     ApiProxy::setApiProxy($this);
 }
コード例 #2
0
ファイル: Capability.php プロジェクト: mackjoner/appscale
 /**
  * Perform the status check for the capability. Each time this method
  * is called the status will be re-checked.
  * @return boolean The enabled status of the package and capability set.
  */
 public function isEnabled()
 {
     $req = new IsEnabledRequest();
     $resp = new IsEnabledResponse();
     $req->setPackage($this->packageName);
     foreach ($this->capabilities as $capability) {
         $req->addCapability($capability);
     }
     ApiProxy::makeSyncCall('capability_service', 'IsEnabled', $req, $resp);
     $status = $resp->getSummaryStatus();
     if ($status === SummaryStatus::UNKNOWN) {
         throw new UnknownCapabilityError();
     }
     return $status === SummaryStatus::ENABLED || $status === SummaryStatus::SCHEDULED_FUTURE || $status === SummaryStatus::SCHEDULED_NOW;
 }
コード例 #3
0
ファイル: AdminMessage.php プロジェクト: mackjoner/appscale
 /**
  * Send the pre-formed email from the Message object to application admins.
  *
  * @throws InvalidArgumentException if a required field (sender, recipient
  * [to, cc or bcc], subject, body [plain or html]) is missing, or if an
  * ApplicationError was thrown by the RPC call due to an unauthorized
  * sender, an invalid attachment type, or an invalid header name
  * @throws RuntimeException If an ApplicationError was thrown by the RPC call
  * due to an internal error or bad request
  * @throws ApplicationError If there was an unknown error in the RPC call
  */
 public function send()
 {
     if (!$this->message->hasSender()) {
         throw new \InvalidArgumentException("Required field sender is not provided.");
     } else {
         if (!$this->message->hasSubject()) {
             throw new \InvalidArgumentException("Required field subject is not provided.");
         } else {
             if (!$this->message->hasTextbody() && !$this->message->hasHtmlbody()) {
                 throw new \InvalidArgumentException("Neither a plain-text nor HTML body is provided - at least one is " . "required.");
             }
         }
     }
     $response = new VoidProto();
     try {
         ApiProxy::makeSyncCall('mail', 'SendToAdmins', $this->message, $response);
     } catch (ApplicationError $e) {
         $this->handleApplicationError($e);
     }
 }
コード例 #4
0
 /**
  * Increments a numeric item's value by the specified offset. If the item's
  * value is not numeric, and error will result.
  *
  * @param string $key The key of the item to increment
  * @param int $offset The amount by which to increment the item's value
  * @param int $initial_value The value to set the item to if it doesn't exist.
  * @param int $expiry The expiry time to set on the item.
  *
  * @return The new item's value on success or false on failure.
  */
 public function increment($key, $offset = 1, $initial_value = 0, $expiry = 0)
 {
     // Sending of a key of 'null' or an unset value is a failure.
     if (is_null($key)) {
         return false;
     }
     $key = $this->getPrefixKey($key);
     $request = new MemcacheIncrementRequest();
     $response = new MemcacheIncrementResponse();
     $request->setKey($key);
     $request->setDelta($offset);
     $request->setInitialValue($initial_value);
     try {
         ApiProxy::makeSyncCall('memcache', 'Increment', $request, $response);
     } catch (Error $e) {
         $this->result_code = self::RES_FAILURE;
         return false;
     }
     if ($response->hasNewValue()) {
         $this->result_code = self::RES_SUCCESS;
         return $response->getNewValue();
     } else {
         $this->result_code = self::RES_NOTSTORED;
         return false;
     }
 }
コード例 #5
0
 /**
  * Make a HTTP request to a supplied URL.
  *
  * Much of the logic here is plagiarized from the WP_Http_Curl as to avoid
  * trying to re-invent the wheel.
  *
  * @param $url
  * @param $args
  * @return bool
  */
 public function request($url, $args)
 {
     $r = wp_parse_args($args, self::$request_defaults);
     // Construct Cookie: header if any cookies are set.
     WP_Http::buildCookieHeader($r);
     $is_local = isset($r['local']) && $r['local'];
     $ssl_verify = isset($r['sslverify']) && $r['sslverify'];
     if ($is_local) {
         $ssl_verify = apply_filters('https_local_ssl_verify', $ssl_verify);
     } else {
         $ssl_verify = apply_filters('https_ssl_verify', $ssl_verify);
     }
     // For now, lets not support streaming into a file and see what breaks
     if (isset($r['filename'])) {
         return new WP_Error('http_request_failed', __('Saving to a file is not currently supported.'));
     }
     $req = new \google\appengine\URLFetchRequest();
     $req->setUrl($url);
     $req->setMethod(self::$request_map[$r['method']]);
     $req->setMustValidateServerCertificate($ssl_verify);
     if (isset($r['body'])) {
         $req->setPayload($r['body']);
     }
     if (isset($r['timeout'])) {
         $req->setDeadline($r['timeout']);
     }
     // App Engine does not allow setting the number of redirects, only if we
     // follow redirects or not.
     $req->setFollowRedirects(isset($r['redirection']) && $r['redirection'] != 0);
     foreach ($r['headers'] as $key => $value) {
         $header = $req->addHeader();
         $header->setKey($key);
         $header->setValue($value);
     }
     $resp = new \google\appengine\URLFetchResponse();
     try {
         ApiProxy::makeSyncCall('urlfetch', 'Fetch', $req, $resp);
     } catch (ApplicationError $e) {
         syslog(LOG_ERR, sprintf("Call to URLFetch failed with application error %d for url %s.", $e->getApplicationError(), $url));
         return new \WP_Error('http_request_failed', $e->getMessage());
     }
     $response = [];
     $response['code'] = $resp->getStatusCode();
     $response['message'] = \get_status_header_desc($resp->getStatusCode());
     $headers = [];
     $cookies = [];
     foreach ($resp->getHeaderList() as $header) {
         $key = strtolower($header->getKey());
         $value = trim($header->getValue());
         // If a header has multiple values then it is stored in an array
         if (isset($headers[$key])) {
             if (!is_array($headers[$key])) {
                 $headers[$key] = [$headers[$key]];
             }
             $headers[$key][] = $value;
         } else {
             $headers[$key] = $value;
         }
         if ('set-cookie' == $key) {
             $cookies[] = new \WP_Http_Cookie($value, $url);
         }
     }
     $theBody = $resp->getContent();
     if (true === $r['decompress'] && true === WP_Http_Encoding::should_decode($headers)) {
         $theBody = WP_Http_Encoding::decompress($theBody);
     }
     if (isset($r['limit_response_size']) && strlen($theBody) > $r['limit_response_size']) {
         $theBody = substr($theBody, 0, $r['limit_response_size']);
     }
     $response = ['response' => $response, 'body' => $theBody, 'cookies' => $cookies, 'headers' => $headers];
     return $response;
 }
コード例 #6
0
 /**
  * Computes the logout URL for this request and specified destination URL,
  *  for both federated login App and Google Accounts App.
  *
  * @param string $destination_url The desired final destination
  *               URL for the user once logout is complete.
  *               If 'destinationURL' does not have a host specified, we will
  *               use the host from the current request.
  *
  * @return string Logout URL.
  *
  * @throws UsersException If there was a problem using the Users service.
  */
 public static function createLogoutURL($destination_url)
 {
     $req = new CreateLogoutURLRequest();
     $resp = new CreateLogoutURLResponse();
     $req->setDestinationUrl($destination_url);
     try {
         ApiProxy::makeSyncCall('user', 'CreateLogoutURL', $req, $resp);
     } catch (ApplicationError $e) {
         throw self::applicationErrorToException($e, htmlspecialchars($destination_url));
     }
     return $resp->getLogoutUrl();
 }
コード例 #7
0
 /**
  * Execute a curl request.
  */
 public function exec()
 {
     if (!$this->prepareRequest()) {
         return false;
     }
     $this->response = new \google\appengine\URLFetchResponse();
     try {
         ApiProxy::makeSyncCall('urlfetch', 'Fetch', $this->request, $this->response);
     } catch (ApplicationError $e) {
         $error_number = $e->getApplicationError();
         $curl_error_number = static::$urlfetch_curl_error_map[$error_number];
         $error_message = static::$curle_error_code_str_map[$curl_error_number];
         static::log(LOG_ERR, sprintf('Call to URLFetch failed with application error %d ' . '(%s) for url %s.', $error_number, $error_message, $this->request->getUrl()));
         $this->setCurlErrorFromUrlFetchError($e->getApplicationError(), $e->getMessage());
         return false;
     }
     $response = $this->prepareResponse();
     // Must be after prepareResponse() so data is available for info.
     $this->info = self::$default_getinfo_values;
     $this->prepareCurlInfo();
     if ($this->tryGetOption(CURLOPT_RETURNTRANSFER, $value) && $value) {
         return $response;
     } else {
         if ($this->tryGetOption(CURLOPT_FILE, $value) && $value) {
             $length = fwrite($value, $response);
             return $length === strlen($response);
         } else {
             if ($this->tryGetOption(CURLOPT_WRITEFUNCTION, $cb) && $cb) {
                 $response_len = strlen($response);
                 do {
                     // TODO - what if cb returns 0 or -ve?
                     $response_len -= $cb($this, $response);
                 } while ($response_len > 0);
             } else {
                 echo $response;
             }
         }
     }
     return true;
 }
コード例 #8
0
 /**
  * @param LogReadRequest $request The protocol buffer request to fetch.
  *
  * @return LogReadResponse The response including RequestLogs.
  */
 private static function readLogs(LogReadRequest $request)
 {
     $response = new LogReadResponse();
     try {
         ApiProxy::makeSyncCall('logservice', 'Read', $request, $response);
     } catch (ApplicationError $e) {
         throw self::applicationErrorToException($e);
     }
     return $response;
 }
コード例 #9
0
 /**
  * Make a service call
  *
  * @param $str_call
  * @param $obj_request
  * @param $obj_response
  */
 private function makeCall($str_call, $obj_request, $obj_response)
 {
     try {
         ApiProxy::makeSyncCall('taskqueue', $str_call, $obj_request, $obj_response);
     } catch (ApplicationError $obj_ex) {
         throw new \RuntimeException("Failed to execute call [{$str_call}] with: {$obj_ex->getApplicationError()} (" . $this->translateResultCode($obj_ex->getApplicationError()) . ")");
     }
 }
コード例 #10
0
 /**
  * Run a Request
  *
  * @param $str_method
  * @param ProtocolMessage $obj_request
  * @param ProtocolMessage $obj_response
  * @return ProtocolMessage|null|object
  * @throws ApplicationError
  * @throws \Exception
  */
 private function execute($str_method, ProtocolMessage $obj_request, ProtocolMessage $obj_response)
 {
     try {
         $this->obj_last_request = $obj_request;
         $this->obj_last_response = null;
         ApiProxy::makeSyncCall('search', $str_method, $obj_request, $obj_response, 60);
         $this->obj_last_response = $obj_response;
     } catch (ApplicationError $obj_exception) {
         throw $obj_exception;
     }
 }
コード例 #11
0
ファイル: PushTask.php プロジェクト: mackjoner/appscale
 private static function addTasks($tasks, $queue)
 {
     $req = new TaskQueueBulkAddRequest();
     $resp = new TaskQueueBulkAddResponse();
     $names = [];
     $current_time = microtime(true);
     foreach ($tasks as $task) {
         $names[] = $task->getName();
         $add = $req->addAddRequest();
         $add->setQueueName($queue);
         $add->setTaskName($task->getName());
         $add->setEtaUsec(($current_time + $task->getDelaySeconds()) * 1000000.0);
         $add->setMethod(self::$methods[$task->getMethod()]);
         if ($task->getMethod() == 'POST' || $task->getMethod() == 'PUT') {
             $add->setUrl($task->getUrlPath());
             if ($task->getQueryData()) {
                 $add->setBody(http_build_query($task->getQueryData()));
                 $header = $add->addHeader();
                 $header->setKey('content-type');
                 $header->setValue('application/x-www-form-urlencoded');
             }
         } else {
             $url_path = $task->getUrlPath();
             if ($task->getQueryData()) {
                 $url_path = $url_path . '?' . http_build_query($task->getQueryData());
             }
             $add->setUrl($url_path);
         }
         if (strlen($add->getUrl()) > self::MAX_URL_LENGTH) {
             throw new TaskQueueException('URL length greater than maximum of ' . self::MAX_URL_LENGTH . '. URL: ' . $add->getUrl());
         }
         if ($add->byteSizePartial() > self::MAX_TASK_SIZE_BYTES) {
             throw new TaskQueueException('Task greater than maximum size of ' . self::MAX_TASK_SIZE_BYTES . '. size: ' . $add->byteSizePartial());
         }
     }
     try {
         ApiProxy::makeSyncCall('taskqueue', 'BulkAdd', $req, $resp);
     } catch (ApplicationError $e) {
         throw self::ApplicationErrorToException($e);
     }
     // Update $names with any generated task names.
     $results = $resp->getTaskResultList();
     foreach ($results as $index => $taskResult) {
         if ($taskResult->hasChosenTaskName()) {
             $names[$index] = $taskResult->getChosenTaskName();
         }
     }
     return $names;
 }
コード例 #12
0
 public function testRpcDevTicket()
 {
     $expected_request = new SignForAppRequest();
     $expected_response = new SignForAppResponse();
     $expected_request->setBytesToSign("SomeBytes");
     $expected_response->setKeyName("TheKeyName");
     $ticket = 'TheDevTicket';
     putenv(VmApiProxy::TICKET_HEADER);
     putenv(VmApiProxy::DEV_TICKET_HEADER . "={$ticket}");
     $options = ['ticket' => $ticket];
     $this->expectRpc($expected_request, $expected_response, $options);
     $response = new SignForAppResponse();
     ApiProxy::makeSyncCall(self::PACKAGE_NAME, self::CALL_NAME, $expected_request, $response);
     $this->assertEquals($response->getKeyName(), "TheKeyName");
     putenv(VmApiProxy::DEV_TICKET_HEADER);
 }
コード例 #13
0
 /**
  * Increments a cached item's value. The value must be a int, float or string
  * representing an integer e.g. 5, 5.0 or "5" or the call with fail.
  *
  * @param string $key The key associated with the value to decrement.
  *
  * @param int $value The amount to increment the value.
  *
  * @return mixed On success, the new value of the item is returned. On
  *               failure, false is returned.
  */
 public function increment($key, $value = 1)
 {
     // Sending of a key of 'null' or an unset value is a failure.
     if (is_null($key)) {
         return false;
     }
     $request = new MemcacheIncrementRequest();
     $response = new MemcacheIncrementResponse();
     $request->setKey($key);
     $request->setDelta($value);
     try {
         ApiProxy::makeSyncCall('memcache', 'Increment', $request, $response);
     } catch (Exception $e) {
         return false;
     }
     if ($response->hasNewValue()) {
         return $response->getNewValue();
     } else {
         return false;
     }
 }
コード例 #14
0
 /**
  * Execute a method against the Datastore
  *
  * Use Google's static ApiProxy method
  *
  * Will attempt to convert GQL queries in local development environments
  *
  * @param $str_method
  * @param ProtocolMessage $obj_request
  * @param ProtocolMessage $obj_response
  * @return mixed
  * @throws ApplicationError
  * @throws \google\appengine\runtime\CapabilityDisabledError
  * @throws \google\appengine\runtime\FeatureNotEnabledError
  * @throws Contention
  */
 private function execute($str_method, ProtocolMessage $obj_request, ProtocolMessage $obj_response)
 {
     try {
         ApiProxy::makeSyncCall('datastore_v4', $str_method, $obj_request, $obj_response, 60);
         $this->obj_last_response = $obj_response;
     } catch (ApplicationError $obj_exception) {
         $this->obj_last_response = NULL;
         if ($obj_request instanceof RunQueryRequest && 'GQL not supported.' === $obj_exception->getMessage()) {
             $this->executeGqlAsBasicQuery($obj_request);
             // recursive
         } elseif (FALSE !== strpos($obj_exception->getMessage(), 'too much contention') || FALSE !== strpos($obj_exception->getMessage(), 'Concurrency')) {
             // LIVE: "too much contention on these datastore entities. please try again." LOCAL : "Concurrency exception."
             throw new Contention('Datastore contention', 409, $obj_exception);
         } else {
             throw $obj_exception;
         }
     }
     return $this->obj_last_response;
 }
コード例 #15
0
 /**
  * Return the name of the default Google Cloud Storage bucket for the
  * application, if one has been configured.
  *
  * @return string The bucket name, or an empty string if no bucket has been
  * configured.
  */
 public static function getDefaultGoogleStorageBucketName()
 {
     $request = new GetDefaultGsBucketNameRequest();
     $response = new GetDefaultGsBucketNameResponse();
     ApiProxy::makeSyncCall('file', 'GetDefaultGsBucketName', $request, $response);
     return $response->getDefaultGsBucketName();
 }
コード例 #16
0
 /**
  *
  */
 private function doHttpRequest($url, $method, $headers, $body)
 {
     $connection_timeout = $this->context_options['connection_timeout_seconds'];
     $req = new \google\appengine\URLFetchRequest();
     $req->setUrl($url);
     $req->setMethod(self::$request_map[$method]);
     $req->setMustValidateServerCertificate(true);
     $req->setDeadline($connection_timeout);
     $req->setFollowRedirects(false);
     if (isset($body)) {
         $req->setPayload($body);
     }
     foreach ($headers as $key => $value) {
         $h = $req->addHeader();
         $h->setKey($key);
         $h->setValue($value);
     }
     $resp = new \google\appengine\URLFetchResponse();
     for ($num_retries = 0;; $num_retries++) {
         try {
             ApiProxy::makeSyncCall('urlfetch', 'Fetch', $req, $resp, $connection_timeout);
         } catch (ApplicationError $e) {
             if (in_array($e->getApplicationError(), self::$retry_exception_codes)) {
                 // We need to set a plausible value in the URLFetchResponse proto in
                 // case the retry loop falls through - this will also cause a retry
                 // if one is available.
                 $resp->setStatusCode(HttpResponse::GATEWAY_TIMEOUT);
             } else {
                 syslog(LOG_ERR, sprintf("Call to URLFetch failed with application error %d " . "for url %s.", $e->getApplicationError(), $url));
                 return false;
             }
         }
         $status_code = $resp->getStatusCode();
         if ($num_retries < $this->context_options['max_retries'] && in_array($status_code, self::$retryable_statuses) && (connection_status() & CONNECTION_TIMEOUT) == 0) {
             usleep(rand(0, 1000000 * pow(2, $num_retries)));
             if ((connection_status() & CONNECTION_TIMEOUT) == CONNECTION_TIMEOUT) {
                 break;
             }
         } else {
             break;
         }
     }
     $response_headers = [];
     foreach ($resp->getHeaderList() as $header) {
         // TODO: Do we need to support multiple headers with the same key?
         $response_headers[trim($header->getKey())] = trim($header->getValue());
     }
     return ['status_code' => $resp->getStatusCode(), 'headers' => $response_headers, 'body' => $resp->getContent()];
 }
コード例 #17
0
ファイル: UserService.php プロジェクト: mackjoner/appscale
 /**
  * Computes the logout URL for this request and specified destination URL,
  *  for both federated login App and Google Accounts App.
  *
  * @param string $destinationURL String that is the desired final destination
  *               URL for the user once logout is complete.
  *               If 'destinationURL' does not have a host specified, we will
  *               use the host from the current request.
  *
  * @return string Logout URL.
  */
 public static function createLogoutURL($destinationURL, $authDomain = null)
 {
     $req = new CreateLogoutURLRequest();
     $resp = new CreateLogoutURLResponse();
     $req->setDestinationUrl($destinationURL);
     if ($authDomain !== null) {
         $req->setAuthDomain($authDomain);
     }
     try {
         ApiProxy::makeSyncCall('user', 'CreateLogoutURL', $req, $resp);
     } catch (ApplicationError $e) {
         if ($e->getApplicationError() === ErrorCode::REDIRECT_URL_TOO_LONG) {
             throw new RedirectTooLongError();
         } else {
             throw $e;
         }
     }
     return $resp->getLogoutUrl();
 }
コード例 #18
0
 /**
  *
  */
 private function doHttpRequest($url, $method, $headers, $body)
 {
     $req = new \google\appengine\URLFetchRequest();
     $req->setUrl($url);
     $req->setMethod(self::$request_map[$method]);
     $req->setMustValidateServerCertificate(true);
     if (isset($body)) {
         $req->setPayload($body);
     }
     foreach ($headers as $key => $value) {
         $h = $req->addHeader();
         $h->setKey($key);
         $h->setValue($value);
     }
     $resp = new \google\appengine\URLFetchResponse();
     for ($num_retries = 0;; $num_retries++) {
         try {
             ApiProxy::makeSyncCall('urlfetch', 'Fetch', $req, $resp);
         } catch (ApplicationError $e) {
             syslog(LOG_ERR, sprintf("Call to URLFetch failed with application error %d.", $e->getApplicationError()));
             return false;
         }
         $status_code = $resp->getStatusCode();
         if ($num_retries < $this->context_options['max_retries'] && in_array($status_code, self::$retryable_statuses) && (connection_status() & CONNECTION_TIMEOUT) == 0) {
             usleep(rand(0, 1000000 * pow(2, $num_retries)));
             if ((connection_status() & CONNECTION_TIMEOUT) == CONNECTION_TIMEOUT) {
                 break;
             }
         } else {
             break;
         }
     }
     $response_headers = [];
     foreach ($resp->getHeaderList() as $header) {
         // TODO: Do we need to support multiple headers with the same key?
         $response_headers[trim($header->getKey())] = trim($header->getValue());
     }
     return ['status_code' => $resp->getStatusCode(), 'headers' => $response_headers, 'body' => $resp->getContent()];
 }
コード例 #19
0
 /**
  *
  */
 private function doHttpRequest($url, $method, $headers, $body)
 {
     $req = new \google\appengine\URLFetchRequest();
     $req->setUrl($url);
     $req->setMethod(self::$request_map[$method]);
     $req->setMustValidateServerCertificate(true);
     if (isset($body)) {
         $req->setPayload($body);
     }
     foreach ($headers as $key => $value) {
         $h = $req->addHeader();
         $h->setKey($key);
         $h->setValue($value);
     }
     $resp = new \google\appengine\URLFetchResponse();
     try {
         ApiProxy::makeSyncCall('urlfetch', 'Fetch', $req, $resp);
     } catch (ApplicationError $e) {
         syslog(LOG_ERR, sprintf("Call to URLFetch failed with application error %d.", $e->getApplicationError()));
         return false;
     }
     $response_headers = [];
     foreach ($resp->getHeaderList() as $header) {
         // TODO: Do we need to support multiple headers with the same key?
         $response_headers[trim($header->getKey())] = trim($header->getValue());
     }
     return ['status_code' => $resp->getStatusCode(), 'headers' => $response_headers, 'body' => $resp->getContent()];
 }
コード例 #20
0
ファイル: Memcache.php プロジェクト: bitcpf/djangoage
 /**
  * Internal implementation of increment (and decrement).
  *
  * @param string $key The key associated with the value to increment.
  *
  * @param int $value The amount to increment the value.
  *
  * @param bool $is_incr Whether to perform an increment or decrement.
  *
  * @return mixed On success, the new value of the item is returned. On
  *               failure, false is returned.
  */
 private function incrementInternal($key, $value, $is_incr)
 {
     // Sending of a key of 'null' or an unset value is a failure.
     if (is_null($key)) {
         return false;
     }
     $request = new MemcacheIncrementRequest();
     $response = new MemcacheIncrementResponse();
     $request->setKey($key);
     $request->setDelta($value);
     if (!$is_incr) {
         $request->setDirection(MemcacheIncrementRequest\Direction::DECREMENT);
     }
     try {
         ApiProxy::makeSyncCall('memcache', 'Increment', $request, $response);
     } catch (Exception $e) {
         return false;
     }
     if ($response->hasNewValue()) {
         return $response->getNewValue();
     } else {
         return false;
     }
 }
コード例 #21
0
ファイル: MemcacheUtils.php プロジェクト: bitcpf/djangoage
 public static function setMultiWithPolicy($keyValues, $expire, $policy)
 {
     $request = new MemcacheSetRequest();
     $response = new MemcacheSetResponse();
     foreach ($keyValues as $key => $value) {
         $memcache_flag = 0;
         $serialized_value = self::serializeValue($value, $memcache_flag);
         $item = $request->addItem();
         $item->setKey($key);
         $item->setValue($serialized_value);
         $item->setFlags($memcache_flag);
         $item->setSetPolicy($policy);
         $item->setExpirationTime($expire);
     }
     ApiProxy::makeSyncCall('memcache', 'Set', $request, $response);
     return $response->getSetStatusList();
 }
コード例 #22
0
 /**
  * Add tasks to the queue.
  *
  * @param PushTask[] $tasks The tasks to be added to the queue.
  *
  * @return An array containing the name of each task added, with the same
  * ordering as $tasks.
  *
  * @throws TaskAlreadyExistsException if a task of the same name already
  * exists in the queue.
  * If this exception is raised, the caller can be guaranteed that all tasks
  * were successfully added either by this call or a previous call. Another way
  * to express it is that, if any task failed to be added for a different
  * reason, a different exception will be thrown.
  * @throws TaskQueueException if there was a problem using the service.
  */
 public function addTasks($tasks)
 {
     if (!is_array($tasks)) {
         throw new \InvalidArgumentException('$tasks must be an array. Actual type: ' . gettype($tasks));
     }
     if (empty($tasks)) {
         return [];
     }
     if (count($tasks) > self::MAX_TASKS_PER_ADD) {
         throw new \InvalidArgumentException('$tasks must contain at most ' . self::MAX_TASKS_PER_ADD . ' tasks. Actual size: ' . count($tasks));
     }
     $req = new TaskQueueBulkAddRequest();
     $resp = new TaskQueueBulkAddResponse();
     $names = [];
     $current_time = microtime(true);
     foreach ($tasks as $task) {
         if (!$task instanceof PushTask) {
             throw new \InvalidArgumentException('All values in $tasks must be instances of PushTask. ' . 'Actual type: ' . gettype($task));
         }
         $names[] = $task->getName();
         $add = $req->addAddRequest();
         $add->setQueueName($this->name);
         $add->setTaskName($task->getName());
         $add->setEtaUsec(($current_time + $task->getDelaySeconds()) * 1000000.0);
         $add->setMethod(self::$methods[$task->getMethod()]);
         $add->setUrl($task->getUrl());
         foreach ($task->getHeaders() as $header) {
             $pair = explode(':', $header, 2);
             $header_pb = $add->addHeader();
             $header_pb->setKey(trim($pair[0]));
             $header_pb->setValue(trim($pair[1]));
         }
         // TODO: Replace getQueryData() with getBody() and simplify the following
         // block.
         if ($task->getMethod() == 'POST' || $task->getMethod() == 'PUT') {
             if ($task->getQueryData()) {
                 $add->setBody(http_build_query($task->getQueryData()));
             }
         }
         if ($add->byteSizePartial() > PushTask::MAX_TASK_SIZE_BYTES) {
             throw new TaskQueueException('Task greater than maximum size of ' . PushTask::MAX_TASK_SIZE_BYTES . '. size: ' . $add->byteSizePartial());
         }
     }
     try {
         ApiProxy::makeSyncCall('taskqueue', 'BulkAdd', $req, $resp);
     } catch (ApplicationError $e) {
         throw self::errorCodeToException($e->getApplicationError());
     }
     // Update $names with any generated task names. Also, check if there are any
     // error responses.
     $results = $resp->getTaskResultList();
     $exception = null;
     foreach ($results as $index => $task_result) {
         if ($task_result->hasChosenTaskName()) {
             $names[$index] = $task_result->getChosenTaskName();
         }
         if ($task_result->getResult() != ErrorCode::OK) {
             $exception = self::errorCodeToException($task_result->getResult());
             // Other exceptions take precedence over TaskAlreadyExistsException.
             if (!$exception instanceof TaskAlreadyExistsException) {
                 throw $exception;
             }
         }
     }
     if (isset($exception)) {
         throw $exception;
     }
     return $names;
 }
コード例 #23
0
ファイル: ModulesService.php プロジェクト: bitcpf/djangoage
 /**
  * Returns the hostname to use when contacting a module.
  * *
  * @param string $module The name of the module whose hostname should be
  * returned. If null then the hostname of the current module will be returned.
  *
  * @param string $version The version of the module whose hostname should be
  * returned. If null then the hostname for the version of the current
  * instance will be returned.
  *
  * @param string $instance The instance whose hostname should be returned. If
  * null then the load balanced hostname for the module will be returned. If
  * the module is not a fixed module then the instance parameter is ignored.
  *
  * @return string The valid canonical hostname that can be used to communicate
  * with the given module/version/instance e.g.
  * "0.version1.module5.myapp.appspot.com".
  * @throws \InvalidArgumentException If $module or $version is not a string
  * or if $instance is not a string or integer.
  * @throws ModulesException if the given combination of $module and $instance
  * is invalid.
  */
 public static function getHostname($module = null, $version = null, $instance = null)
 {
     $req = new GetHostnameRequest();
     $resp = new GetHostnameResponse();
     if ($module !== null) {
         if (!is_string($module)) {
             throw new \InvalidArgumentException('$module must be a string. Actual type: ' . gettype($module));
         }
         $req->setModule($module);
     }
     if ($version !== null) {
         if (!is_string($version)) {
             throw new \InvalidArgumentException('$version must be a string. Actual type: ' . gettype($version));
         }
         $req->setVersion($version);
     }
     if ($instance !== null) {
         if (!is_int($instance) && !is_string($instance)) {
             throw new \InvalidArgumentException('$instance must be an integer or string. Actual type: ' . gettype($instance));
         }
         $req->setInstance((string) $instance);
     }
     try {
         ApiProxy::makeSyncCall('modules', 'GetHostname', $req, $resp);
     } catch (ApplicationError $e) {
         throw self::errorCodeToException($e->getApplicationError());
     }
     return $resp->getHostname();
 }
コード例 #24
0
ファイル: setup.php プロジェクト: bitcpf/djangoage
     // Set SCRIPT_NAME to SCRIPT_FILENAME made relative to DOCUMENT_ROOT and
     // PHP_SELF to SCRIPT_NAME except when the script is included in PATH_INFO (
     // REQUEST_URI without the query string) which matches Apache behavior.
     $_SERVER['SCRIPT_NAME'] = substr($_SERVER['SCRIPT_FILENAME'], strlen($_SERVER['DOCUMENT_ROOT']));
     if (strpos($_SERVER['PATH_INFO'], $_SERVER['SCRIPT_NAME']) === 0) {
         $_SERVER['PHP_SELF'] = $_SERVER['PATH_INFO'];
     } else {
         $_SERVER['PHP_SELF'] = $_SERVER['SCRIPT_NAME'];
     }
 };
 $setupApiProxy = function () {
     global $unsetEnv;
     if (!function_exists('make_call')) {
         require_once 'google/appengine/runtime/ApiProxy.php';
         require_once 'google/appengine/runtime/RemoteApiProxy.php';
         \google\appengine\runtime\ApiProxy::setApiProxy(new \google\appengine\runtime\RemoteApiProxy(getenv('REMOTE_API_HOST'), getenv('REMOTE_API_PORT'), getenv('REMOTE_REQUEST_ID')));
         $unsetEnv('REMOTE_API_HOST');
         $unsetEnv('REMOTE_API_PORT');
         $unsetEnv('REMOTE_REQUEST_ID');
     }
 };
 $setupBuiltins = function () {
     require_once 'google/appengine/runtime/Setup.php';
 };
 $setupAPC = function () {
     /**
      * On the development AppServer users may not have APC available, especially
      * if they built the php-cgi binary themselves. Provide stub functions here
      * so we don't need to complicate the logic later by checking if the
      * functions are avaialble.
      */
コード例 #25
0
ファイル: ProtoBuf.php プロジェクト: 451F/php-gds
 /**
  * Execute a method against the Datastore
  *
  * Use Google's static ApiProxy method
  *
  * Will attempt to convert GQL queries in local development environments
  *
  * @param $str_method
  * @param $obj_request
  * @param $obj_response
  * @return mixed
  * @throws ApplicationError
  * @throws \google\appengine\runtime\CapabilityDisabledError
  * @throws \google\appengine\runtime\FeatureNotEnabledError
  */
 private function execute($str_method, ProtocolMessage $obj_request, ProtocolMessage $obj_response)
 {
     try {
         ApiProxy::makeSyncCall('datastore_v4', $str_method, $obj_request, $obj_response, 60);
         $this->obj_last_response = $obj_response;
     } catch (ApplicationError $obj_exception) {
         if ($obj_request instanceof RunQueryRequest && 'GQL not supported.' === $obj_exception->getMessage()) {
             $this->executeGqlAsBasicQuery($obj_request);
             // recursive
         } else {
             throw $obj_exception;
         }
     }
     return $this->obj_last_response;
 }
コード例 #26
0
 /**
  * Get an OAuth2 access token for the applications service account without
  * caching the result. Usually getAccessToken($scopes) should be used instead
  * which calls this method and caches the result.
  *
  * @param array $scopes The scopes to acquire the access token for.
  * Can be either a single string or an array of strings.
  *
  * @throws InvalidArgumentException If $scopes is not a string or an array of
  * strings.
  * @throws AppIdentityException If there is an error using the AppIdentity
  * service.
  *
  * @return array An array with the following key/value pairs.
  * 'access_token' - The access token for the application.
  * 'expiration_time' - The expiration time for the access token.
  */
 private static function getAccessTokenUncached($scopes)
 {
     $req = new GetAccessTokenRequest();
     $resp = new GetAccessTokenResponse();
     if (is_string($scopes)) {
         $req->addScope($scopes);
     } else {
         if (is_array($scopes)) {
             foreach ($scopes as $scope) {
                 if (is_string($scope)) {
                     $req->addScope($scope);
                 } else {
                     throw new \InvalidArgumentException('Invalid scope ' . htmlspecialchars($scope));
                 }
             }
         } else {
             throw new \InvalidArgumentException('Invalid scope ' . htmlspecialchars($scopes));
         }
     }
     try {
         ApiProxy::makeSyncCall(self::PACKAGE_NAME, 'GetAccessToken', $req, $resp);
     } catch (ApplicationError $e) {
         throw self::applicationErrorToException($e);
     }
     return ['access_token' => $resp->getAccessToken(), 'expiration_time' => $resp->getExpirationTime()];
 }
コード例 #27
0
 /**
  * Return the name of the default Google Cloud Storage bucket for the
  * application, if one has been configured.
  *
  * @return string The bucket name, or an empty string if no bucket has been
  * configured.
  */
 public static function getDefaultGoogleStorageBucketName()
 {
     $success = false;
     $default_bucket_name = apc_fetch(self::GS_DEFAULT_BUCKET_APC_KEY, $success);
     if ($success) {
         return $default_bucket_name;
     }
     $request = new GetDefaultGcsBucketNameRequest();
     $response = new GetDefaultGcsBucketNameResponse();
     ApiProxy::makeSyncCall('app_identity_service', 'GetDefaultGcsBucketName', $request, $response);
     $default_bucket_name = $response->getDefaultGcsBucketName();
     if ($default_bucket_name) {
         apc_store(self::GS_DEFAULT_BUCKET_APC_KEY, $default_bucket_name);
     }
     return $default_bucket_name;
 }