text
stringlengths
81
112k
Start the background thread and begin consuming the thread. def start(self): """Start the background thread and begin consuming the thread.""" with self._operational_lock: ready = threading.Event() thread = threading.Thread( name=_BIDIRECTIONAL_CONSUMER_NAME, target=self._thread_main, args=(ready,) ) thread.daemon = True thread.start() # Other parts of the code rely on `thread.is_alive` which # isn't sufficient to know if a thread is active, just that it may # soon be active. This can cause races. Further protect # against races by using a ready event and wait on it to be set. ready.wait() self._thread = thread _LOGGER.debug("Started helper thread %s", thread.name)
Stop consuming the stream and shutdown the background thread. def stop(self): """Stop consuming the stream and shutdown the background thread.""" with self._operational_lock: self._bidi_rpc.close() if self._thread is not None: # Resume the thread to wake it up in case it is sleeping. self.resume() self._thread.join() self._thread = None
Resumes the response stream. def resume(self): """Resumes the response stream.""" with self._wake: self._paused = False self._wake.notifyAll()
Return a fully-qualified project string. def project_path(cls, user, project): """Return a fully-qualified project string.""" return google.api_core.path_template.expand( "users/{user}/projects/{project}", user=user, project=project )
Return a fully-qualified fingerprint string. def fingerprint_path(cls, user, fingerprint): """Return a fully-qualified fingerprint string.""" return google.api_core.path_template.expand( "users/{user}/sshPublicKeys/{fingerprint}", user=user, fingerprint=fingerprint, )
Deletes a POSIX account. Example: >>> from google.cloud import oslogin_v1 >>> >>> client = oslogin_v1.OsLoginServiceClient() >>> >>> name = client.project_path('[USER]', '[PROJECT]') >>> >>> client.delete_posix_account(name) Args: name (str): A reference to the POSIX account to update. POSIX accounts are identified by the project ID they are associated with. A reference to the POSIX account is in format ``users/{user}/projects/{project}``. retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. def delete_posix_account( self, name, retry=google.api_core.gapic_v1.method.DEFAULT, timeout=google.api_core.gapic_v1.method.DEFAULT, metadata=None, ): """ Deletes a POSIX account. Example: >>> from google.cloud import oslogin_v1 >>> >>> client = oslogin_v1.OsLoginServiceClient() >>> >>> name = client.project_path('[USER]', '[PROJECT]') >>> >>> client.delete_posix_account(name) Args: name (str): A reference to the POSIX account to update. POSIX accounts are identified by the project ID they are associated with. A reference to the POSIX account is in format ``users/{user}/projects/{project}``. retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. """ # Wrap the transport method to add retry and timeout logic. if "delete_posix_account" not in self._inner_api_calls: self._inner_api_calls[ "delete_posix_account" ] = google.api_core.gapic_v1.method.wrap_method( self.transport.delete_posix_account, default_retry=self._method_configs["DeletePosixAccount"].retry, default_timeout=self._method_configs["DeletePosixAccount"].timeout, client_info=self._client_info, ) request = oslogin_pb2.DeletePosixAccountRequest(name=name) self._inner_api_calls["delete_posix_account"]( request, retry=retry, timeout=timeout, metadata=metadata )
Adds an SSH public key and returns the profile information. Default POSIX account information is set when no username and UID exist as part of the login profile. Example: >>> from google.cloud import oslogin_v1 >>> >>> client = oslogin_v1.OsLoginServiceClient() >>> >>> parent = client.user_path('[USER]') >>> >>> # TODO: Initialize `ssh_public_key`: >>> ssh_public_key = {} >>> >>> response = client.import_ssh_public_key(parent, ssh_public_key) Args: parent (str): The unique ID for the user in format ``users/{user}``. ssh_public_key (Union[dict, ~google.cloud.oslogin_v1.types.SshPublicKey]): The SSH public key and expiration time. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.oslogin_v1.types.SshPublicKey` project_id (str): The project ID of the Google Cloud Platform project. retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.cloud.oslogin_v1.types.ImportSshPublicKeyResponse` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. def import_ssh_public_key( self, parent, ssh_public_key, project_id=None, retry=google.api_core.gapic_v1.method.DEFAULT, timeout=google.api_core.gapic_v1.method.DEFAULT, metadata=None, ): """ Adds an SSH public key and returns the profile information. Default POSIX account information is set when no username and UID exist as part of the login profile. Example: >>> from google.cloud import oslogin_v1 >>> >>> client = oslogin_v1.OsLoginServiceClient() >>> >>> parent = client.user_path('[USER]') >>> >>> # TODO: Initialize `ssh_public_key`: >>> ssh_public_key = {} >>> >>> response = client.import_ssh_public_key(parent, ssh_public_key) Args: parent (str): The unique ID for the user in format ``users/{user}``. ssh_public_key (Union[dict, ~google.cloud.oslogin_v1.types.SshPublicKey]): The SSH public key and expiration time. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.oslogin_v1.types.SshPublicKey` project_id (str): The project ID of the Google Cloud Platform project. retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.cloud.oslogin_v1.types.ImportSshPublicKeyResponse` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. """ # Wrap the transport method to add retry and timeout logic. if "import_ssh_public_key" not in self._inner_api_calls: self._inner_api_calls[ "import_ssh_public_key" ] = google.api_core.gapic_v1.method.wrap_method( self.transport.import_ssh_public_key, default_retry=self._method_configs["ImportSshPublicKey"].retry, default_timeout=self._method_configs["ImportSshPublicKey"].timeout, client_info=self._client_info, ) request = oslogin_pb2.ImportSshPublicKeyRequest( parent=parent, ssh_public_key=ssh_public_key, project_id=project_id ) return self._inner_api_calls["import_ssh_public_key"]( request, retry=retry, timeout=timeout, metadata=metadata )
Updates an SSH public key and returns the profile information. This method supports patch semantics. Example: >>> from google.cloud import oslogin_v1 >>> >>> client = oslogin_v1.OsLoginServiceClient() >>> >>> name = client.fingerprint_path('[USER]', '[FINGERPRINT]') >>> >>> # TODO: Initialize `ssh_public_key`: >>> ssh_public_key = {} >>> >>> response = client.update_ssh_public_key(name, ssh_public_key) Args: name (str): The fingerprint of the public key to update. Public keys are identified by their SHA-256 fingerprint. The fingerprint of the public key is in format ``users/{user}/sshPublicKeys/{fingerprint}``. ssh_public_key (Union[dict, ~google.cloud.oslogin_v1.types.SshPublicKey]): The SSH public key and expiration time. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.oslogin_v1.types.SshPublicKey` update_mask (Union[dict, ~google.cloud.oslogin_v1.types.FieldMask]): Mask to control which fields get updated. Updates all if not present. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.oslogin_v1.types.FieldMask` retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.cloud.oslogin_v1.types.SshPublicKey` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. def update_ssh_public_key( self, name, ssh_public_key, update_mask=None, retry=google.api_core.gapic_v1.method.DEFAULT, timeout=google.api_core.gapic_v1.method.DEFAULT, metadata=None, ): """ Updates an SSH public key and returns the profile information. This method supports patch semantics. Example: >>> from google.cloud import oslogin_v1 >>> >>> client = oslogin_v1.OsLoginServiceClient() >>> >>> name = client.fingerprint_path('[USER]', '[FINGERPRINT]') >>> >>> # TODO: Initialize `ssh_public_key`: >>> ssh_public_key = {} >>> >>> response = client.update_ssh_public_key(name, ssh_public_key) Args: name (str): The fingerprint of the public key to update. Public keys are identified by their SHA-256 fingerprint. The fingerprint of the public key is in format ``users/{user}/sshPublicKeys/{fingerprint}``. ssh_public_key (Union[dict, ~google.cloud.oslogin_v1.types.SshPublicKey]): The SSH public key and expiration time. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.oslogin_v1.types.SshPublicKey` update_mask (Union[dict, ~google.cloud.oslogin_v1.types.FieldMask]): Mask to control which fields get updated. Updates all if not present. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.oslogin_v1.types.FieldMask` retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.cloud.oslogin_v1.types.SshPublicKey` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. """ # Wrap the transport method to add retry and timeout logic. if "update_ssh_public_key" not in self._inner_api_calls: self._inner_api_calls[ "update_ssh_public_key" ] = google.api_core.gapic_v1.method.wrap_method( self.transport.update_ssh_public_key, default_retry=self._method_configs["UpdateSshPublicKey"].retry, default_timeout=self._method_configs["UpdateSshPublicKey"].timeout, client_info=self._client_info, ) request = oslogin_pb2.UpdateSshPublicKeyRequest( name=name, ssh_public_key=ssh_public_key, update_mask=update_mask ) return self._inner_api_calls["update_ssh_public_key"]( request, retry=retry, timeout=timeout, metadata=metadata )
Convert a protobuf GC rule to a native object. :type gc_rule_pb: :class:`.table_v2_pb2.GcRule` :param gc_rule_pb: The GC rule to convert. :rtype: :class:`GarbageCollectionRule` or :data:`NoneType <types.NoneType>` :returns: An instance of one of the native rules defined in :module:`column_family` or :data:`None` if no values were set on the protobuf passed in. :raises: :class:`ValueError <exceptions.ValueError>` if the rule name is unexpected. def _gc_rule_from_pb(gc_rule_pb): """Convert a protobuf GC rule to a native object. :type gc_rule_pb: :class:`.table_v2_pb2.GcRule` :param gc_rule_pb: The GC rule to convert. :rtype: :class:`GarbageCollectionRule` or :data:`NoneType <types.NoneType>` :returns: An instance of one of the native rules defined in :module:`column_family` or :data:`None` if no values were set on the protobuf passed in. :raises: :class:`ValueError <exceptions.ValueError>` if the rule name is unexpected. """ rule_name = gc_rule_pb.WhichOneof("rule") if rule_name is None: return None if rule_name == "max_num_versions": return MaxVersionsGCRule(gc_rule_pb.max_num_versions) elif rule_name == "max_age": max_age = _helpers._duration_pb_to_timedelta(gc_rule_pb.max_age) return MaxAgeGCRule(max_age) elif rule_name == "union": return GCRuleUnion([_gc_rule_from_pb(rule) for rule in gc_rule_pb.union.rules]) elif rule_name == "intersection": rules = [_gc_rule_from_pb(rule) for rule in gc_rule_pb.intersection.rules] return GCRuleIntersection(rules) else: raise ValueError("Unexpected rule name", rule_name)
Converts the garbage collection rule to a protobuf. :rtype: :class:`.table_v2_pb2.GcRule` :returns: The converted current object. def to_pb(self): """Converts the garbage collection rule to a protobuf. :rtype: :class:`.table_v2_pb2.GcRule` :returns: The converted current object. """ max_age = _helpers._timedelta_to_duration_pb(self.max_age) return table_v2_pb2.GcRule(max_age=max_age)
Converts the union into a single GC rule as a protobuf. :rtype: :class:`.table_v2_pb2.GcRule` :returns: The converted current object. def to_pb(self): """Converts the union into a single GC rule as a protobuf. :rtype: :class:`.table_v2_pb2.GcRule` :returns: The converted current object. """ union = table_v2_pb2.GcRule.Union(rules=[rule.to_pb() for rule in self.rules]) return table_v2_pb2.GcRule(union=union)
Converts the intersection into a single GC rule as a protobuf. :rtype: :class:`.table_v2_pb2.GcRule` :returns: The converted current object. def to_pb(self): """Converts the intersection into a single GC rule as a protobuf. :rtype: :class:`.table_v2_pb2.GcRule` :returns: The converted current object. """ intersection = table_v2_pb2.GcRule.Intersection( rules=[rule.to_pb() for rule in self.rules] ) return table_v2_pb2.GcRule(intersection=intersection)
Converts the column family to a protobuf. :rtype: :class:`.table_v2_pb2.ColumnFamily` :returns: The converted current object. def to_pb(self): """Converts the column family to a protobuf. :rtype: :class:`.table_v2_pb2.ColumnFamily` :returns: The converted current object. """ if self.gc_rule is None: return table_v2_pb2.ColumnFamily() else: return table_v2_pb2.ColumnFamily(gc_rule=self.gc_rule.to_pb())
Create this column family. For example: .. literalinclude:: snippets_table.py :start-after: [START bigtable_create_column_family] :end-before: [END bigtable_create_column_family] def create(self): """Create this column family. For example: .. literalinclude:: snippets_table.py :start-after: [START bigtable_create_column_family] :end-before: [END bigtable_create_column_family] """ column_family = self.to_pb() modification = table_admin_v2_pb2.ModifyColumnFamiliesRequest.Modification( id=self.column_family_id, create=column_family ) client = self._table._instance._client # data it contains are the GC rule and the column family ID already # stored on this instance. client.table_admin_client.modify_column_families( self._table.name, [modification] )
Delete this column family. For example: .. literalinclude:: snippets_table.py :start-after: [START bigtable_delete_column_family] :end-before: [END bigtable_delete_column_family] def delete(self): """Delete this column family. For example: .. literalinclude:: snippets_table.py :start-after: [START bigtable_delete_column_family] :end-before: [END bigtable_delete_column_family] """ modification = table_admin_v2_pb2.ModifyColumnFamiliesRequest.Modification( id=self.column_family_id, drop=True ) client = self._table._instance._client # data it contains are the GC rule and the column family ID already # stored on this instance. client.table_admin_client.modify_column_families( self._table.name, [modification] )
Wraps a gRPC exception class, if needed. def _maybe_wrap_exception(exception): """Wraps a gRPC exception class, if needed.""" if isinstance(exception, grpc.RpcError): return exceptions.from_grpc_error(exception) return exception
Stop consuming messages and shutdown all helper threads. This method is idempotent. Additional calls will have no effect. Args: reason (Any): The reason to close this. If None, this is considered an "intentional" shutdown. def close(self, reason=None): """Stop consuming messages and shutdown all helper threads. This method is idempotent. Additional calls will have no effect. Args: reason (Any): The reason to close this. If None, this is considered an "intentional" shutdown. """ with self._closing: if self._closed: return # Stop consuming messages. if self.is_active: _LOGGER.debug("Stopping consumer.") self._consumer.stop() self._consumer = None self._rpc.close() self._rpc = None self._closed = True _LOGGER.debug("Finished stopping manager.") if reason: # Raise an exception if a reason is provided _LOGGER.debug("reason for closing: %s" % reason) if isinstance(reason, Exception): raise reason raise RuntimeError(reason)
Triggered whenever the underlying RPC terminates without recovery. This is typically triggered from one of two threads: the background consumer thread (when calling ``recv()`` produces a non-recoverable error) or the grpc management thread (when cancelling the RPC). This method is *non-blocking*. It will start another thread to deal with shutting everything down. This is to prevent blocking in the background consumer and preventing it from being ``joined()``. def _on_rpc_done(self, future): """Triggered whenever the underlying RPC terminates without recovery. This is typically triggered from one of two threads: the background consumer thread (when calling ``recv()`` produces a non-recoverable error) or the grpc management thread (when cancelling the RPC). This method is *non-blocking*. It will start another thread to deal with shutting everything down. This is to prevent blocking in the background consumer and preventing it from being ``joined()``. """ _LOGGER.info("RPC termination has signaled manager shutdown.") future = _maybe_wrap_exception(future) thread = threading.Thread( name=_RPC_ERROR_THREAD_NAME, target=self.close, kwargs={"reason": future} ) thread.daemon = True thread.start()
Creates a watch snapshot listener for a document. snapshot_callback receives a DocumentChange object, but may also start to get targetChange and such soon Args: document_ref: Reference to Document snapshot_callback: callback to be called on snapshot snapshot_class_instance: instance of DocumentSnapshot to make snapshots with to pass to snapshot_callback reference_class_instance: instance of DocumentReference to make references def for_document( cls, document_ref, snapshot_callback, snapshot_class_instance, reference_class_instance, ): """ Creates a watch snapshot listener for a document. snapshot_callback receives a DocumentChange object, but may also start to get targetChange and such soon Args: document_ref: Reference to Document snapshot_callback: callback to be called on snapshot snapshot_class_instance: instance of DocumentSnapshot to make snapshots with to pass to snapshot_callback reference_class_instance: instance of DocumentReference to make references """ return cls( document_ref, document_ref._client, { "documents": {"documents": [document_ref._document_path]}, "target_id": WATCH_TARGET_ID, }, document_watch_comparator, snapshot_callback, snapshot_class_instance, reference_class_instance, )
Called everytime there is a response from listen. Collect changes and 'push' the changes in a batch to the customer when we receive 'current' from the listen response. Args: listen_response(`google.cloud.firestore_v1beta1.types.ListenResponse`): Callback method that receives a object to def on_snapshot(self, proto): """ Called everytime there is a response from listen. Collect changes and 'push' the changes in a batch to the customer when we receive 'current' from the listen response. Args: listen_response(`google.cloud.firestore_v1beta1.types.ListenResponse`): Callback method that receives a object to """ TargetChange = firestore_pb2.TargetChange target_changetype_dispatch = { TargetChange.NO_CHANGE: self._on_snapshot_target_change_no_change, TargetChange.ADD: self._on_snapshot_target_change_add, TargetChange.REMOVE: self._on_snapshot_target_change_remove, TargetChange.RESET: self._on_snapshot_target_change_reset, TargetChange.CURRENT: self._on_snapshot_target_change_current, } target_change = proto.target_change if str(target_change): target_change_type = target_change.target_change_type _LOGGER.debug("on_snapshot: target change: " + str(target_change_type)) meth = target_changetype_dispatch.get(target_change_type) if meth is None: _LOGGER.info( "on_snapshot: Unknown target change " + str(target_change_type) ) self.close( reason="Unknown target change type: %s " % str(target_change_type) ) else: try: meth(proto) except Exception as exc2: _LOGGER.debug("meth(proto) exc: " + str(exc2)) raise # NOTE: # in other implementations, such as node, the backoff is reset here # in this version bidi rpc is just used and will control this. elif str(proto.document_change): _LOGGER.debug("on_snapshot: document change") # No other target_ids can show up here, but we still need to see # if the targetId was in the added list or removed list. target_ids = proto.document_change.target_ids or [] removed_target_ids = proto.document_change.removed_target_ids or [] changed = False removed = False if WATCH_TARGET_ID in target_ids: changed = True if WATCH_TARGET_ID in removed_target_ids: removed = True if changed: _LOGGER.debug("on_snapshot: document change: CHANGED") # google.cloud.firestore_v1beta1.types.DocumentChange document_change = proto.document_change # google.cloud.firestore_v1beta1.types.Document document = document_change.document data = _helpers.decode_dict(document.fields, self._firestore) # Create a snapshot. As Document and Query objects can be # passed we need to get a Document Reference in a more manual # fashion than self._document_reference document_name = document.name db_str = self._firestore._database_string db_str_documents = db_str + "/documents/" if document_name.startswith(db_str_documents): document_name = document_name[len(db_str_documents) :] document_ref = self._firestore.document(document_name) snapshot = self.DocumentSnapshot( reference=document_ref, data=data, exists=True, read_time=None, create_time=document.create_time, update_time=document.update_time, ) self.change_map[document.name] = snapshot elif removed: _LOGGER.debug("on_snapshot: document change: REMOVED") document = proto.document_change.document self.change_map[document.name] = ChangeType.REMOVED # NB: document_delete and document_remove (as far as we, the client, # are concerned) are functionally equivalent elif str(proto.document_delete): _LOGGER.debug("on_snapshot: document change: DELETE") name = proto.document_delete.document self.change_map[name] = ChangeType.REMOVED elif str(proto.document_remove): _LOGGER.debug("on_snapshot: document change: REMOVE") name = proto.document_remove.document self.change_map[name] = ChangeType.REMOVED elif proto.filter: _LOGGER.debug("on_snapshot: filter update") if proto.filter.count != self._current_size(): # We need to remove all the current results. self._reset_docs() # The filter didn't match, so re-issue the query. # TODO: reset stream method? # self._reset_stream(); else: _LOGGER.debug("UNKNOWN TYPE. UHOH") self.close(reason=ValueError("Unknown listen response type: %s" % proto))
Assembles a new snapshot from the current set of changes and invokes the user's callback. Clears the current changes on completion. def push(self, read_time, next_resume_token): """ Assembles a new snapshot from the current set of changes and invokes the user's callback. Clears the current changes on completion. """ deletes, adds, updates = Watch._extract_changes( self.doc_map, self.change_map, read_time ) updated_tree, updated_map, appliedChanges = self._compute_snapshot( self.doc_tree, self.doc_map, deletes, adds, updates ) if not self.has_pushed or len(appliedChanges): # TODO: It is possible in the future we will have the tree order # on insert. For now, we sort here. key = functools.cmp_to_key(self._comparator) keys = sorted(updated_tree.keys(), key=key) self._snapshot_callback( keys, appliedChanges, datetime.datetime.fromtimestamp(read_time.seconds, pytz.utc), ) self.has_pushed = True self.doc_tree = updated_tree self.doc_map = updated_map self.change_map.clear() self.resume_token = next_resume_token
Returns the current count of all documents, including the changes from the current changeMap. def _current_size(self): """ Returns the current count of all documents, including the changes from the current changeMap. """ deletes, adds, _ = Watch._extract_changes(self.doc_map, self.change_map, None) return len(self.doc_map) + len(adds) - len(deletes)
Helper to clear the docs on RESET or filter mismatch. def _reset_docs(self): """ Helper to clear the docs on RESET or filter mismatch. """ _LOGGER.debug("resetting documents") self.change_map.clear() self.resume_token = None # Mark each document as deleted. If documents are not deleted # they will be sent again by the server. for snapshot in self.doc_tree.keys(): name = snapshot.reference._document_path self.change_map[name] = ChangeType.REMOVED self.current = False
Construct an API url given a few components, some optional. Typically, you shouldn't need to use this method. :type path: str :param path: The path to the resource (ie, ``'/b/bucket-name'``). :type query_params: dict or list :param query_params: A dictionary of keys and values (or list of key-value pairs) to insert into the query string of the URL. :type api_base_url: str :param api_base_url: The base URL for the API endpoint. Typically you won't have to provide this. :type api_version: str :param api_version: The version of the API to call. Typically you shouldn't provide this and instead use the default for the library. :rtype: str :returns: The URL assembled from the pieces provided. def build_api_url( cls, path, query_params=None, api_base_url=None, api_version=None ): """Construct an API url given a few components, some optional. Typically, you shouldn't need to use this method. :type path: str :param path: The path to the resource (ie, ``'/b/bucket-name'``). :type query_params: dict or list :param query_params: A dictionary of keys and values (or list of key-value pairs) to insert into the query string of the URL. :type api_base_url: str :param api_base_url: The base URL for the API endpoint. Typically you won't have to provide this. :type api_version: str :param api_version: The version of the API to call. Typically you shouldn't provide this and instead use the default for the library. :rtype: str :returns: The URL assembled from the pieces provided. """ url = cls.API_URL_TEMPLATE.format( api_base_url=(api_base_url or cls.API_BASE_URL), api_version=(api_version or cls.API_VERSION), path=path, ) query_params = query_params or {} if query_params: url += "?" + urlencode(query_params, doseq=True) return url
A low level method to send a request to the API. Typically, you shouldn't need to use this method. :type method: str :param method: The HTTP method to use in the request. :type url: str :param url: The URL to send the request to. :type data: str :param data: The data to send as the body of the request. :type content_type: str :param content_type: The proper MIME type of the data provided. :type headers: dict :param headers: (Optional) A dictionary of HTTP headers to send with the request. If passed, will be modified directly here with added headers. :type target_object: object :param target_object: (Optional) Argument to be used by library callers. This can allow custom behavior, for example, to defer an HTTP request and complete initialization of the object at a later time. :rtype: :class:`requests.Response` :returns: The HTTP response. def _make_request( self, method, url, data=None, content_type=None, headers=None, target_object=None, ): """A low level method to send a request to the API. Typically, you shouldn't need to use this method. :type method: str :param method: The HTTP method to use in the request. :type url: str :param url: The URL to send the request to. :type data: str :param data: The data to send as the body of the request. :type content_type: str :param content_type: The proper MIME type of the data provided. :type headers: dict :param headers: (Optional) A dictionary of HTTP headers to send with the request. If passed, will be modified directly here with added headers. :type target_object: object :param target_object: (Optional) Argument to be used by library callers. This can allow custom behavior, for example, to defer an HTTP request and complete initialization of the object at a later time. :rtype: :class:`requests.Response` :returns: The HTTP response. """ headers = headers or {} headers.update(self._EXTRA_HEADERS) headers["Accept-Encoding"] = "gzip" if content_type: headers["Content-Type"] = content_type headers["User-Agent"] = self.USER_AGENT return self._do_request(method, url, headers, data, target_object)
Low-level helper: perform the actual API request over HTTP. Allows batch context managers to override and defer a request. :type method: str :param method: The HTTP method to use in the request. :type url: str :param url: The URL to send the request to. :type headers: dict :param headers: A dictionary of HTTP headers to send with the request. :type data: str :param data: The data to send as the body of the request. :type target_object: object :param target_object: (Optional) Unused ``target_object`` here but may be used by a superclass. :rtype: :class:`requests.Response` :returns: The HTTP response. def _do_request( self, method, url, headers, data, target_object ): # pylint: disable=unused-argument """Low-level helper: perform the actual API request over HTTP. Allows batch context managers to override and defer a request. :type method: str :param method: The HTTP method to use in the request. :type url: str :param url: The URL to send the request to. :type headers: dict :param headers: A dictionary of HTTP headers to send with the request. :type data: str :param data: The data to send as the body of the request. :type target_object: object :param target_object: (Optional) Unused ``target_object`` here but may be used by a superclass. :rtype: :class:`requests.Response` :returns: The HTTP response. """ return self.http.request(url=url, method=method, headers=headers, data=data)
Make a request over the HTTP transport to the API. You shouldn't need to use this method, but if you plan to interact with the API using these primitives, this is the correct one to use. :type method: str :param method: The HTTP method name (ie, ``GET``, ``POST``, etc). Required. :type path: str :param path: The path to the resource (ie, ``'/b/bucket-name'``). Required. :type query_params: dict or list :param query_params: A dictionary of keys and values (or list of key-value pairs) to insert into the query string of the URL. :type data: str :param data: The data to send as the body of the request. Default is the empty string. :type content_type: str :param content_type: The proper MIME type of the data provided. Default is None. :type headers: dict :param headers: extra HTTP headers to be sent with the request. :type api_base_url: str :param api_base_url: The base URL for the API endpoint. Typically you won't have to provide this. Default is the standard API base URL. :type api_version: str :param api_version: The version of the API to call. Typically you shouldn't provide this and instead use the default for the library. Default is the latest API version supported by google-cloud-python. :type expect_json: bool :param expect_json: If True, this method will try to parse the response as JSON and raise an exception if that cannot be done. Default is True. :type _target_object: :class:`object` :param _target_object: (Optional) Protected argument to be used by library callers. This can allow custom behavior, for example, to defer an HTTP request and complete initialization of the object at a later time. :raises ~google.cloud.exceptions.GoogleCloudError: if the response code is not 200 OK. :raises ValueError: if the response content type is not JSON. :rtype: dict or str :returns: The API response payload, either as a raw string or a dictionary if the response is valid JSON. def api_request( self, method, path, query_params=None, data=None, content_type=None, headers=None, api_base_url=None, api_version=None, expect_json=True, _target_object=None, ): """Make a request over the HTTP transport to the API. You shouldn't need to use this method, but if you plan to interact with the API using these primitives, this is the correct one to use. :type method: str :param method: The HTTP method name (ie, ``GET``, ``POST``, etc). Required. :type path: str :param path: The path to the resource (ie, ``'/b/bucket-name'``). Required. :type query_params: dict or list :param query_params: A dictionary of keys and values (or list of key-value pairs) to insert into the query string of the URL. :type data: str :param data: The data to send as the body of the request. Default is the empty string. :type content_type: str :param content_type: The proper MIME type of the data provided. Default is None. :type headers: dict :param headers: extra HTTP headers to be sent with the request. :type api_base_url: str :param api_base_url: The base URL for the API endpoint. Typically you won't have to provide this. Default is the standard API base URL. :type api_version: str :param api_version: The version of the API to call. Typically you shouldn't provide this and instead use the default for the library. Default is the latest API version supported by google-cloud-python. :type expect_json: bool :param expect_json: If True, this method will try to parse the response as JSON and raise an exception if that cannot be done. Default is True. :type _target_object: :class:`object` :param _target_object: (Optional) Protected argument to be used by library callers. This can allow custom behavior, for example, to defer an HTTP request and complete initialization of the object at a later time. :raises ~google.cloud.exceptions.GoogleCloudError: if the response code is not 200 OK. :raises ValueError: if the response content type is not JSON. :rtype: dict or str :returns: The API response payload, either as a raw string or a dictionary if the response is valid JSON. """ url = self.build_api_url( path=path, query_params=query_params, api_base_url=api_base_url, api_version=api_version, ) # Making the executive decision that any dictionary # data will be sent properly as JSON. if data and isinstance(data, dict): data = json.dumps(data) content_type = "application/json" response = self._make_request( method=method, url=url, data=data, content_type=content_type, headers=headers, target_object=_target_object, ) if not 200 <= response.status_code < 300: raise exceptions.from_http_response(response) if expect_json and response.content: return response.json() else: return response.content
Construct a filter string to filter on metric or resource labels. def _build_label_filter(category, *args, **kwargs): """Construct a filter string to filter on metric or resource labels.""" terms = list(args) for key, value in six.iteritems(kwargs): if value is None: continue suffix = None if key.endswith( ("_prefix", "_suffix", "_greater", "_greaterequal", "_less", "_lessequal") ): key, suffix = key.rsplit("_", 1) if category == "resource" and key == "resource_type": key = "resource.type" else: key = ".".join((category, "label", key)) if suffix == "prefix": term = '{key} = starts_with("{value}")' elif suffix == "suffix": term = '{key} = ends_with("{value}")' elif suffix == "greater": term = "{key} > {value}" elif suffix == "greaterequal": term = "{key} >= {value}" elif suffix == "less": term = "{key} < {value}" elif suffix == "lessequal": term = "{key} <= {value}" else: term = '{key} = "{value}"' terms.append(term.format(key=key, value=value)) return " AND ".join(sorted(terms))
Copy the query and set the query time interval. Example:: import datetime now = datetime.datetime.utcnow() query = query.select_interval( end_time=now, start_time=now - datetime.timedelta(minutes=5)) As a convenience, you can alternatively specify the end time and an interval duration when you create the query initially. :type end_time: :class:`datetime.datetime` :param end_time: The end time (inclusive) of the time interval for which results should be returned, as a datetime object. :type start_time: :class:`datetime.datetime` :param start_time: (Optional) The start time (exclusive) of the time interval for which results should be returned, as a datetime object. If not specified, the interval is a point in time. :rtype: :class:`Query` :returns: The new query object. def select_interval(self, end_time, start_time=None): """Copy the query and set the query time interval. Example:: import datetime now = datetime.datetime.utcnow() query = query.select_interval( end_time=now, start_time=now - datetime.timedelta(minutes=5)) As a convenience, you can alternatively specify the end time and an interval duration when you create the query initially. :type end_time: :class:`datetime.datetime` :param end_time: The end time (inclusive) of the time interval for which results should be returned, as a datetime object. :type start_time: :class:`datetime.datetime` :param start_time: (Optional) The start time (exclusive) of the time interval for which results should be returned, as a datetime object. If not specified, the interval is a point in time. :rtype: :class:`Query` :returns: The new query object. """ new_query = copy.deepcopy(self) new_query._end_time = end_time new_query._start_time = start_time return new_query
Copy the query and add filtering by group. Example:: query = query.select_group('1234567') :type group_id: str :param group_id: The ID of a group to filter by. :rtype: :class:`Query` :returns: The new query object. def select_group(self, group_id): """Copy the query and add filtering by group. Example:: query = query.select_group('1234567') :type group_id: str :param group_id: The ID of a group to filter by. :rtype: :class:`Query` :returns: The new query object. """ new_query = copy.deepcopy(self) new_query._filter.group_id = group_id return new_query
Copy the query and add filtering by monitored projects. This is only useful if the target project represents a Stackdriver account containing the specified monitored projects. Examples:: query = query.select_projects('project-1') query = query.select_projects('project-1', 'project-2') :type args: tuple :param args: Project IDs limiting the resources to be included in the query. :rtype: :class:`Query` :returns: The new query object. def select_projects(self, *args): """Copy the query and add filtering by monitored projects. This is only useful if the target project represents a Stackdriver account containing the specified monitored projects. Examples:: query = query.select_projects('project-1') query = query.select_projects('project-1', 'project-2') :type args: tuple :param args: Project IDs limiting the resources to be included in the query. :rtype: :class:`Query` :returns: The new query object. """ new_query = copy.deepcopy(self) new_query._filter.projects = args return new_query
Copy the query and add filtering by resource labels. Examples:: query = query.select_resources(zone='us-central1-a') query = query.select_resources(zone_prefix='europe-') query = query.select_resources(resource_type='gce_instance') A keyword argument ``<label>=<value>`` ordinarily generates a filter expression of the form:: resource.label.<label> = "<value>" However, by adding ``"_prefix"`` or ``"_suffix"`` to the keyword, you can specify a partial match. ``<label>_prefix=<value>`` generates:: resource.label.<label> = starts_with("<value>") ``<label>_suffix=<value>`` generates:: resource.label.<label> = ends_with("<value>") As a special case, ``"resource_type"`` is treated as a special pseudo-label corresponding to the filter object ``resource.type``. For example, ``resource_type=<value>`` generates:: resource.type = "<value>" See the `defined resource types`_. .. note:: The label ``"instance_name"`` is a metric label, not a resource label. You would filter on it using ``select_metrics(instance_name=...)``. :type args: tuple :param args: Raw filter expression strings to include in the conjunction. If just one is provided and no keyword arguments are provided, it can be a disjunction. :type kwargs: dict :param kwargs: Label filters to include in the conjunction as described above. :rtype: :class:`Query` :returns: The new query object. .. _defined resource types: https://cloud.google.com/monitoring/api/v3/monitored-resources def select_resources(self, *args, **kwargs): """Copy the query and add filtering by resource labels. Examples:: query = query.select_resources(zone='us-central1-a') query = query.select_resources(zone_prefix='europe-') query = query.select_resources(resource_type='gce_instance') A keyword argument ``<label>=<value>`` ordinarily generates a filter expression of the form:: resource.label.<label> = "<value>" However, by adding ``"_prefix"`` or ``"_suffix"`` to the keyword, you can specify a partial match. ``<label>_prefix=<value>`` generates:: resource.label.<label> = starts_with("<value>") ``<label>_suffix=<value>`` generates:: resource.label.<label> = ends_with("<value>") As a special case, ``"resource_type"`` is treated as a special pseudo-label corresponding to the filter object ``resource.type``. For example, ``resource_type=<value>`` generates:: resource.type = "<value>" See the `defined resource types`_. .. note:: The label ``"instance_name"`` is a metric label, not a resource label. You would filter on it using ``select_metrics(instance_name=...)``. :type args: tuple :param args: Raw filter expression strings to include in the conjunction. If just one is provided and no keyword arguments are provided, it can be a disjunction. :type kwargs: dict :param kwargs: Label filters to include in the conjunction as described above. :rtype: :class:`Query` :returns: The new query object. .. _defined resource types: https://cloud.google.com/monitoring/api/v3/monitored-resources """ new_query = copy.deepcopy(self) new_query._filter.select_resources(*args, **kwargs) return new_query
Copy the query and add filtering by metric labels. Examples:: query = query.select_metrics(instance_name='myinstance') query = query.select_metrics(instance_name_prefix='mycluster-') A keyword argument ``<label>=<value>`` ordinarily generates a filter expression of the form:: metric.label.<label> = "<value>" However, by adding ``"_prefix"`` or ``"_suffix"`` to the keyword, you can specify a partial match. ``<label>_prefix=<value>`` generates:: metric.label.<label> = starts_with("<value>") ``<label>_suffix=<value>`` generates:: metric.label.<label> = ends_with("<value>") If the label's value type is ``INT64``, a similar notation can be used to express inequalities: ``<label>_less=<value>`` generates:: metric.label.<label> < <value> ``<label>_lessequal=<value>`` generates:: metric.label.<label> <= <value> ``<label>_greater=<value>`` generates:: metric.label.<label> > <value> ``<label>_greaterequal=<value>`` generates:: metric.label.<label> >= <value> :type args: tuple :param args: Raw filter expression strings to include in the conjunction. If just one is provided and no keyword arguments are provided, it can be a disjunction. :type kwargs: dict :param kwargs: Label filters to include in the conjunction as described above. :rtype: :class:`Query` :returns: The new query object. def select_metrics(self, *args, **kwargs): """Copy the query and add filtering by metric labels. Examples:: query = query.select_metrics(instance_name='myinstance') query = query.select_metrics(instance_name_prefix='mycluster-') A keyword argument ``<label>=<value>`` ordinarily generates a filter expression of the form:: metric.label.<label> = "<value>" However, by adding ``"_prefix"`` or ``"_suffix"`` to the keyword, you can specify a partial match. ``<label>_prefix=<value>`` generates:: metric.label.<label> = starts_with("<value>") ``<label>_suffix=<value>`` generates:: metric.label.<label> = ends_with("<value>") If the label's value type is ``INT64``, a similar notation can be used to express inequalities: ``<label>_less=<value>`` generates:: metric.label.<label> < <value> ``<label>_lessequal=<value>`` generates:: metric.label.<label> <= <value> ``<label>_greater=<value>`` generates:: metric.label.<label> > <value> ``<label>_greaterequal=<value>`` generates:: metric.label.<label> >= <value> :type args: tuple :param args: Raw filter expression strings to include in the conjunction. If just one is provided and no keyword arguments are provided, it can be a disjunction. :type kwargs: dict :param kwargs: Label filters to include in the conjunction as described above. :rtype: :class:`Query` :returns: The new query object. """ new_query = copy.deepcopy(self) new_query._filter.select_metrics(*args, **kwargs) return new_query
Copy the query and add temporal alignment. If ``per_series_aligner`` is not :data:`Aligner.ALIGN_NONE`, each time series will contain data points only on the period boundaries. Example:: from google.cloud.monitoring import enums query = query.align( enums.Aggregation.Aligner.ALIGN_MEAN, minutes=5) It is also possible to specify the aligner as a literal string:: query = query.align('ALIGN_MEAN', minutes=5) :type per_series_aligner: str or :class:`~google.cloud.monitoring_v3.gapic.enums.Aggregation.Aligner` :param per_series_aligner: The approach to be used to align individual time series. For example: :data:`Aligner.ALIGN_MEAN`. See :class:`~google.cloud.monitoring_v3.gapic.enums.Aggregation.Aligner` and the descriptions of the `supported aligners`_. :type seconds: int :param seconds: The number of seconds in the alignment period. :type minutes: int :param minutes: The number of minutes in the alignment period. :type hours: int :param hours: The number of hours in the alignment period. :rtype: :class:`Query` :returns: The new query object. .. _supported aligners: https://cloud.google.com/monitoring/api/ref_v3/rest/v3/\ projects.timeSeries/list#Aligner def align(self, per_series_aligner, seconds=0, minutes=0, hours=0): """Copy the query and add temporal alignment. If ``per_series_aligner`` is not :data:`Aligner.ALIGN_NONE`, each time series will contain data points only on the period boundaries. Example:: from google.cloud.monitoring import enums query = query.align( enums.Aggregation.Aligner.ALIGN_MEAN, minutes=5) It is also possible to specify the aligner as a literal string:: query = query.align('ALIGN_MEAN', minutes=5) :type per_series_aligner: str or :class:`~google.cloud.monitoring_v3.gapic.enums.Aggregation.Aligner` :param per_series_aligner: The approach to be used to align individual time series. For example: :data:`Aligner.ALIGN_MEAN`. See :class:`~google.cloud.monitoring_v3.gapic.enums.Aggregation.Aligner` and the descriptions of the `supported aligners`_. :type seconds: int :param seconds: The number of seconds in the alignment period. :type minutes: int :param minutes: The number of minutes in the alignment period. :type hours: int :param hours: The number of hours in the alignment period. :rtype: :class:`Query` :returns: The new query object. .. _supported aligners: https://cloud.google.com/monitoring/api/ref_v3/rest/v3/\ projects.timeSeries/list#Aligner """ new_query = copy.deepcopy(self) new_query._per_series_aligner = per_series_aligner new_query._alignment_period_seconds = seconds + 60 * (minutes + 60 * hours) return new_query
Copy the query and add cross-series reduction. Cross-series reduction combines time series by aggregating their data points. For example, you could request an aggregated time series for each combination of project and zone as follows:: from google.cloud.monitoring import enums query = query.reduce(enums.Aggregation.Reducer.REDUCE_MEAN, 'resource.project_id', 'resource.zone') :type cross_series_reducer: str or :class:`~google.cloud.monitoring_v3.gapic.enums.Aggregation.Reducer` :param cross_series_reducer: The approach to be used to combine time series. For example: :data:`Reducer.REDUCE_MEAN`. See :class:`~google.cloud.monitoring_v3.gapic.enums.Aggregation.Reducer` and the descriptions of the `supported reducers`_. :type group_by_fields: strs :param group_by_fields: Fields to be preserved by the reduction. For example, specifying just ``"resource.zone"`` will result in one time series per zone. The default is to aggregate all of the time series into just one. :rtype: :class:`Query` :returns: The new query object. .. _supported reducers: https://cloud.google.com/monitoring/api/ref_v3/rest/v3/\ projects.timeSeries/list#Reducer def reduce(self, cross_series_reducer, *group_by_fields): """Copy the query and add cross-series reduction. Cross-series reduction combines time series by aggregating their data points. For example, you could request an aggregated time series for each combination of project and zone as follows:: from google.cloud.monitoring import enums query = query.reduce(enums.Aggregation.Reducer.REDUCE_MEAN, 'resource.project_id', 'resource.zone') :type cross_series_reducer: str or :class:`~google.cloud.monitoring_v3.gapic.enums.Aggregation.Reducer` :param cross_series_reducer: The approach to be used to combine time series. For example: :data:`Reducer.REDUCE_MEAN`. See :class:`~google.cloud.monitoring_v3.gapic.enums.Aggregation.Reducer` and the descriptions of the `supported reducers`_. :type group_by_fields: strs :param group_by_fields: Fields to be preserved by the reduction. For example, specifying just ``"resource.zone"`` will result in one time series per zone. The default is to aggregate all of the time series into just one. :rtype: :class:`Query` :returns: The new query object. .. _supported reducers: https://cloud.google.com/monitoring/api/ref_v3/rest/v3/\ projects.timeSeries/list#Reducer """ new_query = copy.deepcopy(self) new_query._cross_series_reducer = cross_series_reducer new_query._group_by_fields = group_by_fields return new_query
Yield all time series objects selected by the query. The generator returned iterates over :class:`~google.cloud.monitoring_v3.types.TimeSeries` objects containing points ordered from oldest to newest. Note that the :class:`Query` object itself is an iterable, such that the following are equivalent:: for timeseries in query: ... for timeseries in query.iter(): ... :type headers_only: bool :param headers_only: Whether to omit the point data from the time series objects. :type page_size: int :param page_size: (Optional) The maximum number of points in each page of results from this request. Non-positive values are ignored. Defaults to a sensible value set by the API. :raises: :exc:`ValueError` if the query time interval has not been specified. def iter(self, headers_only=False, page_size=None): """Yield all time series objects selected by the query. The generator returned iterates over :class:`~google.cloud.monitoring_v3.types.TimeSeries` objects containing points ordered from oldest to newest. Note that the :class:`Query` object itself is an iterable, such that the following are equivalent:: for timeseries in query: ... for timeseries in query.iter(): ... :type headers_only: bool :param headers_only: Whether to omit the point data from the time series objects. :type page_size: int :param page_size: (Optional) The maximum number of points in each page of results from this request. Non-positive values are ignored. Defaults to a sensible value set by the API. :raises: :exc:`ValueError` if the query time interval has not been specified. """ if self._end_time is None: raise ValueError("Query time interval not specified.") params = self._build_query_params(headers_only, page_size) for ts in self._client.list_time_series(**params): yield ts
Return key-value pairs for the list_time_series API call. :type headers_only: bool :param headers_only: Whether to omit the point data from the :class:`~google.cloud.monitoring_v3.types.TimeSeries` objects. :type page_size: int :param page_size: (Optional) The maximum number of points in each page of results from this request. Non-positive values are ignored. Defaults to a sensible value set by the API. def _build_query_params(self, headers_only=False, page_size=None): """Return key-value pairs for the list_time_series API call. :type headers_only: bool :param headers_only: Whether to omit the point data from the :class:`~google.cloud.monitoring_v3.types.TimeSeries` objects. :type page_size: int :param page_size: (Optional) The maximum number of points in each page of results from this request. Non-positive values are ignored. Defaults to a sensible value set by the API. """ params = {"name": self._project_path, "filter_": self.filter} params["interval"] = types.TimeInterval() params["interval"].end_time.FromDatetime(self._end_time) if self._start_time: params["interval"].start_time.FromDatetime(self._start_time) if ( self._per_series_aligner or self._alignment_period_seconds or self._cross_series_reducer or self._group_by_fields ): params["aggregation"] = types.Aggregation( per_series_aligner=self._per_series_aligner, cross_series_reducer=self._cross_series_reducer, group_by_fields=self._group_by_fields, alignment_period={"seconds": self._alignment_period_seconds}, ) if headers_only: params["view"] = enums.ListTimeSeriesRequest.TimeSeriesView.HEADERS else: params["view"] = enums.ListTimeSeriesRequest.TimeSeriesView.FULL if page_size is not None: params["page_size"] = page_size return params
Factory: construct a project given its API representation. :type resource: dict :param resource: project resource representation returned from the API :type client: :class:`google.cloud.resource_manager.client.Client` :param client: The Client used with this project. :rtype: :class:`google.cloud.resource_manager.project.Project` :returns: The project created. def from_api_repr(cls, resource, client): """Factory: construct a project given its API representation. :type resource: dict :param resource: project resource representation returned from the API :type client: :class:`google.cloud.resource_manager.client.Client` :param client: The Client used with this project. :rtype: :class:`google.cloud.resource_manager.project.Project` :returns: The project created. """ project = cls(project_id=resource["projectId"], client=client) project.set_properties_from_api_repr(resource) return project
Update specific properties from its API representation. def set_properties_from_api_repr(self, resource): """Update specific properties from its API representation.""" self.name = resource.get("name") self.number = resource["projectNumber"] self.labels = resource.get("labels", {}) self.status = resource["lifecycleState"] if "parent" in resource: self.parent = resource["parent"]
API call: create the project via a ``POST`` request. See https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/create :type client: :class:`google.cloud.resource_manager.client.Client` or :data:`NoneType <types.NoneType>` :param client: the client to use. If not passed, falls back to the client stored on the current project. def create(self, client=None): """API call: create the project via a ``POST`` request. See https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/create :type client: :class:`google.cloud.resource_manager.client.Client` or :data:`NoneType <types.NoneType>` :param client: the client to use. If not passed, falls back to the client stored on the current project. """ client = self._require_client(client) data = {"projectId": self.project_id, "name": self.name, "labels": self.labels} resp = client._connection.api_request( method="POST", path="/projects", data=data ) self.set_properties_from_api_repr(resource=resp)
API call: update the project via a ``PUT`` request. See https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/update :type client: :class:`google.cloud.resource_manager.client.Client` or :data:`NoneType <types.NoneType>` :param client: the client to use. If not passed, falls back to the client stored on the current project. def update(self, client=None): """API call: update the project via a ``PUT`` request. See https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/update :type client: :class:`google.cloud.resource_manager.client.Client` or :data:`NoneType <types.NoneType>` :param client: the client to use. If not passed, falls back to the client stored on the current project. """ client = self._require_client(client) data = {"name": self.name, "labels": self.labels, "parent": self.parent} resp = client._connection.api_request(method="PUT", path=self.path, data=data) self.set_properties_from_api_repr(resp)
API call: delete the project via a ``DELETE`` request. See https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/delete This actually changes the status (``lifecycleState``) from ``ACTIVE`` to ``DELETE_REQUESTED``. Later (it's not specified when), the project will move into the ``DELETE_IN_PROGRESS`` state, which means the deleting has actually begun. :type client: :class:`google.cloud.resource_manager.client.Client` or :data:`NoneType <types.NoneType>` :param client: the client to use. If not passed, falls back to the client stored on the current project. :type reload_data: bool :param reload_data: Whether to reload the project with the latest state. If you want to get the updated status, you'll want this set to :data:`True` as the DELETE method doesn't send back the updated project. Default: :data:`False`. def delete(self, client=None, reload_data=False): """API call: delete the project via a ``DELETE`` request. See https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/delete This actually changes the status (``lifecycleState``) from ``ACTIVE`` to ``DELETE_REQUESTED``. Later (it's not specified when), the project will move into the ``DELETE_IN_PROGRESS`` state, which means the deleting has actually begun. :type client: :class:`google.cloud.resource_manager.client.Client` or :data:`NoneType <types.NoneType>` :param client: the client to use. If not passed, falls back to the client stored on the current project. :type reload_data: bool :param reload_data: Whether to reload the project with the latest state. If you want to get the updated status, you'll want this set to :data:`True` as the DELETE method doesn't send back the updated project. Default: :data:`False`. """ client = self._require_client(client) client._connection.api_request(method="DELETE", path=self.path) # If the reload flag is set, reload the project. if reload_data: self.reload()
Get the meaning from a protobuf value. :type value_pb: :class:`.entity_pb2.Value` :param value_pb: The protobuf value to be checked for an associated meaning. :type is_list: bool :param is_list: Boolean indicating if the ``value_pb`` contains a list value. :rtype: int :returns: The meaning for the ``value_pb`` if one is set, else :data:`None`. For a list value, if there are disagreeing means it just returns a list of meanings. If all the list meanings agree, it just condenses them. def _get_meaning(value_pb, is_list=False): """Get the meaning from a protobuf value. :type value_pb: :class:`.entity_pb2.Value` :param value_pb: The protobuf value to be checked for an associated meaning. :type is_list: bool :param is_list: Boolean indicating if the ``value_pb`` contains a list value. :rtype: int :returns: The meaning for the ``value_pb`` if one is set, else :data:`None`. For a list value, if there are disagreeing means it just returns a list of meanings. If all the list meanings agree, it just condenses them. """ meaning = None if is_list: # An empty list will have no values, hence no shared meaning # set among them. if len(value_pb.array_value.values) == 0: return None # We check among all the meanings, some of which may be None, # the rest which may be enum/int values. all_meanings = [ _get_meaning(sub_value_pb) for sub_value_pb in value_pb.array_value.values ] unique_meanings = set(all_meanings) if len(unique_meanings) == 1: # If there is a unique meaning, we preserve it. meaning = unique_meanings.pop() else: # We know len(value_pb.array_value.values) > 0. # If the meaning is not unique, just return all of them. meaning = all_meanings elif value_pb.meaning: # Simple field (int32). meaning = value_pb.meaning return meaning
Factory method for creating an entity based on a protobuf. The protobuf should be one returned from the Cloud Datastore Protobuf API. :type pb: :class:`.entity_pb2.Entity` :param pb: The Protobuf representing the entity. :rtype: :class:`google.cloud.datastore.entity.Entity` :returns: The entity derived from the protobuf. def entity_from_protobuf(pb): """Factory method for creating an entity based on a protobuf. The protobuf should be one returned from the Cloud Datastore Protobuf API. :type pb: :class:`.entity_pb2.Entity` :param pb: The Protobuf representing the entity. :rtype: :class:`google.cloud.datastore.entity.Entity` :returns: The entity derived from the protobuf. """ key = None if pb.HasField("key"): # Message field (Key) key = key_from_protobuf(pb.key) entity_props = {} entity_meanings = {} exclude_from_indexes = [] for prop_name, value_pb in _property_tuples(pb): value = _get_value_from_value_pb(value_pb) entity_props[prop_name] = value # Check if the property has an associated meaning. is_list = isinstance(value, list) meaning = _get_meaning(value_pb, is_list=is_list) if meaning is not None: entity_meanings[prop_name] = (meaning, value) # Check if ``value_pb`` was excluded from index. Lists need to be # special-cased and we require all ``exclude_from_indexes`` values # in a list agree. if is_list and len(value) > 0: exclude_values = set( value_pb.exclude_from_indexes for value_pb in value_pb.array_value.values ) if len(exclude_values) != 1: raise ValueError( "For an array_value, subvalues must either " "all be indexed or all excluded from " "indexes." ) if exclude_values.pop(): exclude_from_indexes.append(prop_name) else: if value_pb.exclude_from_indexes: exclude_from_indexes.append(prop_name) entity = Entity(key=key, exclude_from_indexes=exclude_from_indexes) entity.update(entity_props) entity._meanings.update(entity_meanings) return entity
Add meaning information (from an entity) to a protobuf. :type entity: :class:`google.cloud.datastore.entity.Entity` :param entity: The entity to be turned into a protobuf. :type name: str :param name: The name of the property. :type value: object :param value: The current value stored as property ``name``. :type value_pb: :class:`.entity_pb2.Value` :param value_pb: The protobuf value to add meaning / meanings to. :type is_list: bool :param is_list: (Optional) Boolean indicating if the ``value`` is a list value. def _set_pb_meaning_from_entity(entity, name, value, value_pb, is_list=False): """Add meaning information (from an entity) to a protobuf. :type entity: :class:`google.cloud.datastore.entity.Entity` :param entity: The entity to be turned into a protobuf. :type name: str :param name: The name of the property. :type value: object :param value: The current value stored as property ``name``. :type value_pb: :class:`.entity_pb2.Value` :param value_pb: The protobuf value to add meaning / meanings to. :type is_list: bool :param is_list: (Optional) Boolean indicating if the ``value`` is a list value. """ if name not in entity._meanings: return meaning, orig_value = entity._meanings[name] # Only add the meaning back to the protobuf if the value is # unchanged from when it was originally read from the API. if orig_value is not value: return # For lists, we set meaning on each sub-element. if is_list: if not isinstance(meaning, list): meaning = itertools.repeat(meaning) val_iter = six.moves.zip(value_pb.array_value.values, meaning) for sub_value_pb, sub_meaning in val_iter: if sub_meaning is not None: sub_value_pb.meaning = sub_meaning else: value_pb.meaning = meaning
Converts an entity into a protobuf. :type entity: :class:`google.cloud.datastore.entity.Entity` :param entity: The entity to be turned into a protobuf. :rtype: :class:`.entity_pb2.Entity` :returns: The protobuf representing the entity. def entity_to_protobuf(entity): """Converts an entity into a protobuf. :type entity: :class:`google.cloud.datastore.entity.Entity` :param entity: The entity to be turned into a protobuf. :rtype: :class:`.entity_pb2.Entity` :returns: The protobuf representing the entity. """ entity_pb = entity_pb2.Entity() if entity.key is not None: key_pb = entity.key.to_protobuf() entity_pb.key.CopyFrom(key_pb) for name, value in entity.items(): value_is_list = isinstance(value, list) value_pb = _new_value_pb(entity_pb, name) # Set the appropriate value. _set_protobuf_value(value_pb, value) # Add index information to protobuf. if name in entity.exclude_from_indexes: if not value_is_list: value_pb.exclude_from_indexes = True for sub_value in value_pb.array_value.values: sub_value.exclude_from_indexes = True # Add meaning information to protobuf. _set_pb_meaning_from_entity( entity, name, value, value_pb, is_list=value_is_list ) return entity_pb
Validate rules for read options, and assign to the request. Helper method for ``lookup()`` and ``run_query``. :type eventual: bool :param eventual: Flag indicating if ``EVENTUAL`` or ``STRONG`` consistency should be used. :type transaction_id: bytes :param transaction_id: A transaction identifier (may be null). :rtype: :class:`.datastore_pb2.ReadOptions` :returns: The read options corresponding to the inputs. :raises: :class:`ValueError` if ``eventual`` is ``True`` and the ``transaction_id`` is not ``None``. def get_read_options(eventual, transaction_id): """Validate rules for read options, and assign to the request. Helper method for ``lookup()`` and ``run_query``. :type eventual: bool :param eventual: Flag indicating if ``EVENTUAL`` or ``STRONG`` consistency should be used. :type transaction_id: bytes :param transaction_id: A transaction identifier (may be null). :rtype: :class:`.datastore_pb2.ReadOptions` :returns: The read options corresponding to the inputs. :raises: :class:`ValueError` if ``eventual`` is ``True`` and the ``transaction_id`` is not ``None``. """ if transaction_id is None: if eventual: return datastore_pb2.ReadOptions( read_consistency=datastore_pb2.ReadOptions.EVENTUAL ) else: return datastore_pb2.ReadOptions() else: if eventual: raise ValueError("eventual must be False when in a transaction") else: return datastore_pb2.ReadOptions(transaction=transaction_id)
Factory method for creating a key based on a protobuf. The protobuf should be one returned from the Cloud Datastore Protobuf API. :type pb: :class:`.entity_pb2.Key` :param pb: The Protobuf representing the key. :rtype: :class:`google.cloud.datastore.key.Key` :returns: a new `Key` instance def key_from_protobuf(pb): """Factory method for creating a key based on a protobuf. The protobuf should be one returned from the Cloud Datastore Protobuf API. :type pb: :class:`.entity_pb2.Key` :param pb: The Protobuf representing the key. :rtype: :class:`google.cloud.datastore.key.Key` :returns: a new `Key` instance """ path_args = [] for element in pb.path: path_args.append(element.kind) if element.id: # Simple field (int64) path_args.append(element.id) # This is safe: we expect proto objects returned will only have # one of `name` or `id` set. if element.name: # Simple field (string) path_args.append(element.name) project = None if pb.partition_id.project_id: # Simple field (string) project = pb.partition_id.project_id namespace = None if pb.partition_id.namespace_id: # Simple field (string) namespace = pb.partition_id.namespace_id return Key(*path_args, namespace=namespace, project=project)
Given a value, return the protobuf attribute name and proper value. The Protobuf API uses different attribute names based on value types rather than inferring the type. This function simply determines the proper attribute name based on the type of the value provided and returns the attribute name as well as a properly formatted value. Certain value types need to be coerced into a different type (such as a `datetime.datetime` into an integer timestamp, or a `google.cloud.datastore.key.Key` into a Protobuf representation. This function handles that for you. .. note:: Values which are "text" ('unicode' in Python2, 'str' in Python3) map to 'string_value' in the datastore; values which are "bytes" ('str' in Python2, 'bytes' in Python3) map to 'blob_value'. For example: >>> _pb_attr_value(1234) ('integer_value', 1234) >>> _pb_attr_value('my_string') ('string_value', 'my_string') :type val: :class:`datetime.datetime`, :class:`google.cloud.datastore.key.Key`, bool, float, integer, bytes, str, unicode, :class:`google.cloud.datastore.entity.Entity`, dict, list, :class:`google.cloud.datastore.helpers.GeoPoint`, NoneType :param val: The value to be scrutinized. :rtype: tuple :returns: A tuple of the attribute name and proper value type. def _pb_attr_value(val): """Given a value, return the protobuf attribute name and proper value. The Protobuf API uses different attribute names based on value types rather than inferring the type. This function simply determines the proper attribute name based on the type of the value provided and returns the attribute name as well as a properly formatted value. Certain value types need to be coerced into a different type (such as a `datetime.datetime` into an integer timestamp, or a `google.cloud.datastore.key.Key` into a Protobuf representation. This function handles that for you. .. note:: Values which are "text" ('unicode' in Python2, 'str' in Python3) map to 'string_value' in the datastore; values which are "bytes" ('str' in Python2, 'bytes' in Python3) map to 'blob_value'. For example: >>> _pb_attr_value(1234) ('integer_value', 1234) >>> _pb_attr_value('my_string') ('string_value', 'my_string') :type val: :class:`datetime.datetime`, :class:`google.cloud.datastore.key.Key`, bool, float, integer, bytes, str, unicode, :class:`google.cloud.datastore.entity.Entity`, dict, list, :class:`google.cloud.datastore.helpers.GeoPoint`, NoneType :param val: The value to be scrutinized. :rtype: tuple :returns: A tuple of the attribute name and proper value type. """ if isinstance(val, datetime.datetime): name = "timestamp" value = _datetime_to_pb_timestamp(val) elif isinstance(val, Key): name, value = "key", val.to_protobuf() elif isinstance(val, bool): name, value = "boolean", val elif isinstance(val, float): name, value = "double", val elif isinstance(val, six.integer_types): name, value = "integer", val elif isinstance(val, six.text_type): name, value = "string", val elif isinstance(val, six.binary_type): name, value = "blob", val elif isinstance(val, Entity): name, value = "entity", val elif isinstance(val, dict): entity_val = Entity(key=None) entity_val.update(val) name, value = "entity", entity_val elif isinstance(val, list): name, value = "array", val elif isinstance(val, GeoPoint): name, value = "geo_point", val.to_protobuf() elif val is None: name, value = "null", struct_pb2.NULL_VALUE else: raise ValueError("Unknown protobuf attr type", type(val)) return name + "_value", value
Given a protobuf for a Value, get the correct value. The Cloud Datastore Protobuf API returns a Property Protobuf which has one value set and the rest blank. This function retrieves the the one value provided. Some work is done to coerce the return value into a more useful type (particularly in the case of a timestamp value, or a key value). :type value_pb: :class:`.entity_pb2.Value` :param value_pb: The Value Protobuf. :rtype: object :returns: The value provided by the Protobuf. :raises: :class:`ValueError <exceptions.ValueError>` if no value type has been set. def _get_value_from_value_pb(value_pb): """Given a protobuf for a Value, get the correct value. The Cloud Datastore Protobuf API returns a Property Protobuf which has one value set and the rest blank. This function retrieves the the one value provided. Some work is done to coerce the return value into a more useful type (particularly in the case of a timestamp value, or a key value). :type value_pb: :class:`.entity_pb2.Value` :param value_pb: The Value Protobuf. :rtype: object :returns: The value provided by the Protobuf. :raises: :class:`ValueError <exceptions.ValueError>` if no value type has been set. """ value_type = value_pb.WhichOneof("value_type") if value_type == "timestamp_value": result = _pb_timestamp_to_datetime(value_pb.timestamp_value) elif value_type == "key_value": result = key_from_protobuf(value_pb.key_value) elif value_type == "boolean_value": result = value_pb.boolean_value elif value_type == "double_value": result = value_pb.double_value elif value_type == "integer_value": result = value_pb.integer_value elif value_type == "string_value": result = value_pb.string_value elif value_type == "blob_value": result = value_pb.blob_value elif value_type == "entity_value": result = entity_from_protobuf(value_pb.entity_value) elif value_type == "array_value": result = [ _get_value_from_value_pb(value) for value in value_pb.array_value.values ] elif value_type == "geo_point_value": result = GeoPoint( value_pb.geo_point_value.latitude, value_pb.geo_point_value.longitude ) elif value_type == "null_value": result = None else: raise ValueError("Value protobuf did not have any value set") return result
Assign 'val' to the correct subfield of 'value_pb'. The Protobuf API uses different attribute names based on value types rather than inferring the type. Some value types (entities, keys, lists) cannot be directly assigned; this function handles them correctly. :type value_pb: :class:`.entity_pb2.Value` :param value_pb: The value protobuf to which the value is being assigned. :type val: :class:`datetime.datetime`, boolean, float, integer, string, :class:`google.cloud.datastore.key.Key`, :class:`google.cloud.datastore.entity.Entity` :param val: The value to be assigned. def _set_protobuf_value(value_pb, val): """Assign 'val' to the correct subfield of 'value_pb'. The Protobuf API uses different attribute names based on value types rather than inferring the type. Some value types (entities, keys, lists) cannot be directly assigned; this function handles them correctly. :type value_pb: :class:`.entity_pb2.Value` :param value_pb: The value protobuf to which the value is being assigned. :type val: :class:`datetime.datetime`, boolean, float, integer, string, :class:`google.cloud.datastore.key.Key`, :class:`google.cloud.datastore.entity.Entity` :param val: The value to be assigned. """ attr, val = _pb_attr_value(val) if attr == "key_value": value_pb.key_value.CopyFrom(val) elif attr == "timestamp_value": value_pb.timestamp_value.CopyFrom(val) elif attr == "entity_value": entity_pb = entity_to_protobuf(val) value_pb.entity_value.CopyFrom(entity_pb) elif attr == "array_value": if len(val) == 0: array_value = entity_pb2.ArrayValue(values=[]) value_pb.array_value.CopyFrom(array_value) else: l_pb = value_pb.array_value.values for item in val: i_pb = l_pb.add() _set_protobuf_value(i_pb, item) elif attr == "geo_point_value": value_pb.geo_point_value.CopyFrom(val) else: # scalar, just assign setattr(value_pb, attr, val)
Convert the current object to protobuf. :rtype: :class:`google.type.latlng_pb2.LatLng`. :returns: The current point as a protobuf. def to_protobuf(self): """Convert the current object to protobuf. :rtype: :class:`google.type.latlng_pb2.LatLng`. :returns: The current point as a protobuf. """ return latlng_pb2.LatLng(latitude=self.latitude, longitude=self.longitude)
Factory: construct an entry given its API representation :type resource: dict :param resource: text entry resource representation returned from the API :type client: :class:`google.cloud.logging.client.Client` :param client: Client which holds credentials and project configuration. :type loggers: dict :param loggers: (Optional) A mapping of logger fullnames -> loggers. If not passed, the entry will have a newly-created logger. :rtype: :class:`google.cloud.logging.entries.LogEntry` :returns: Log entry parsed from ``resource``. def from_api_repr(cls, resource, client, loggers=None): """Factory: construct an entry given its API representation :type resource: dict :param resource: text entry resource representation returned from the API :type client: :class:`google.cloud.logging.client.Client` :param client: Client which holds credentials and project configuration. :type loggers: dict :param loggers: (Optional) A mapping of logger fullnames -> loggers. If not passed, the entry will have a newly-created logger. :rtype: :class:`google.cloud.logging.entries.LogEntry` :returns: Log entry parsed from ``resource``. """ if loggers is None: loggers = {} logger_fullname = resource["logName"] logger = loggers.get(logger_fullname) if logger is None: logger_name = logger_name_from_path(logger_fullname) logger = loggers[logger_fullname] = client.logger(logger_name) payload = cls._extract_payload(resource) insert_id = resource.get("insertId") timestamp = resource.get("timestamp") if timestamp is not None: timestamp = _rfc3339_nanos_to_datetime(timestamp) labels = resource.get("labels") severity = resource.get("severity") http_request = resource.get("httpRequest") trace = resource.get("trace") span_id = resource.get("spanId") trace_sampled = resource.get("traceSampled") source_location = resource.get("sourceLocation") if source_location is not None: line = source_location.pop("line", None) source_location["line"] = _int_or_none(line) operation = resource.get("operation") monitored_resource_dict = resource.get("resource") monitored_resource = None if monitored_resource_dict is not None: monitored_resource = Resource._from_dict(monitored_resource_dict) inst = cls( log_name=logger_fullname, insert_id=insert_id, timestamp=timestamp, labels=labels, severity=severity, http_request=http_request, resource=monitored_resource, trace=trace, span_id=span_id, trace_sampled=trace_sampled, source_location=source_location, operation=operation, logger=logger, payload=payload, ) received = resource.get("receiveTimestamp") if received is not None: inst.received_timestamp = _rfc3339_nanos_to_datetime(received) return inst
API repr (JSON format) for entry. def to_api_repr(self): """API repr (JSON format) for entry. """ info = {} if self.log_name is not None: info["logName"] = self.log_name if self.resource is not None: info["resource"] = self.resource._to_dict() if self.labels is not None: info["labels"] = self.labels if self.insert_id is not None: info["insertId"] = self.insert_id if self.severity is not None: info["severity"] = self.severity if self.http_request is not None: info["httpRequest"] = self.http_request if self.timestamp is not None: info["timestamp"] = _datetime_to_rfc3339(self.timestamp) if self.trace is not None: info["trace"] = self.trace if self.span_id is not None: info["spanId"] = self.span_id if self.trace_sampled is not None: info["traceSampled"] = self.trace_sampled if self.source_location is not None: source_location = self.source_location.copy() source_location["line"] = str(source_location.pop("line", 0)) info["sourceLocation"] = source_location if self.operation is not None: info["operation"] = self.operation return info
API repr (JSON format) for entry. def to_api_repr(self): """API repr (JSON format) for entry. """ info = super(TextEntry, self).to_api_repr() info["textPayload"] = self.payload return info
API repr (JSON format) for entry. def to_api_repr(self): """API repr (JSON format) for entry. """ info = super(StructEntry, self).to_api_repr() info["jsonPayload"] = self.payload return info
API repr (JSON format) for entry. def to_api_repr(self): """API repr (JSON format) for entry. """ info = super(ProtobufEntry, self).to_api_repr() info["protoPayload"] = MessageToDict(self.payload) return info
Construct a DB-API time value from the given ticks value. :type ticks: float :param ticks: a number of seconds since the epoch; see the documentation of the standard Python time module for details. :type tz: :class:`datetime.tzinfo` :param tz: (Optional) time zone to use for conversion :rtype: :class:`datetime.time` :returns: time represented by ticks. def TimeFromTicks(ticks, tz=None): """Construct a DB-API time value from the given ticks value. :type ticks: float :param ticks: a number of seconds since the epoch; see the documentation of the standard Python time module for details. :type tz: :class:`datetime.tzinfo` :param tz: (Optional) time zone to use for conversion :rtype: :class:`datetime.time` :returns: time represented by ticks. """ dt = datetime.datetime.fromtimestamp(ticks, tz=tz) return dt.timetz()
Return a fully-qualified registry string. def registry_path(cls, project, location, registry): """Return a fully-qualified registry string.""" return google.api_core.path_template.expand( "projects/{project}/locations/{location}/registries/{registry}", project=project, location=location, registry=registry, )
Return a fully-qualified device string. def device_path(cls, project, location, registry, device): """Return a fully-qualified device string.""" return google.api_core.path_template.expand( "projects/{project}/locations/{location}/registries/{registry}/devices/{device}", project=project, location=location, registry=registry, device=device, )
Creates a device in a device registry. Example: >>> from google.cloud import iot_v1 >>> >>> client = iot_v1.DeviceManagerClient() >>> >>> parent = client.registry_path('[PROJECT]', '[LOCATION]', '[REGISTRY]') >>> >>> # TODO: Initialize `device`: >>> device = {} >>> >>> response = client.create_device(parent, device) Args: parent (str): The name of the device registry where this device should be created. For example, ``projects/example-project/locations/us-central1/registries/my-registry``. device (Union[dict, ~google.cloud.iot_v1.types.Device]): The device registration details. The field ``name`` must be empty. The server generates ``name`` from the device registry ``id`` and the ``parent`` field. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.iot_v1.types.Device` retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.cloud.iot_v1.types.Device` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. def create_device( self, parent, device, retry=google.api_core.gapic_v1.method.DEFAULT, timeout=google.api_core.gapic_v1.method.DEFAULT, metadata=None, ): """ Creates a device in a device registry. Example: >>> from google.cloud import iot_v1 >>> >>> client = iot_v1.DeviceManagerClient() >>> >>> parent = client.registry_path('[PROJECT]', '[LOCATION]', '[REGISTRY]') >>> >>> # TODO: Initialize `device`: >>> device = {} >>> >>> response = client.create_device(parent, device) Args: parent (str): The name of the device registry where this device should be created. For example, ``projects/example-project/locations/us-central1/registries/my-registry``. device (Union[dict, ~google.cloud.iot_v1.types.Device]): The device registration details. The field ``name`` must be empty. The server generates ``name`` from the device registry ``id`` and the ``parent`` field. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.iot_v1.types.Device` retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.cloud.iot_v1.types.Device` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. """ # Wrap the transport method to add retry and timeout logic. if "create_device" not in self._inner_api_calls: self._inner_api_calls[ "create_device" ] = google.api_core.gapic_v1.method.wrap_method( self.transport.create_device, default_retry=self._method_configs["CreateDevice"].retry, default_timeout=self._method_configs["CreateDevice"].timeout, client_info=self._client_info, ) request = device_manager_pb2.CreateDeviceRequest(parent=parent, device=device) if metadata is None: metadata = [] metadata = list(metadata) try: routing_header = [("parent", parent)] except AttributeError: pass else: routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata( routing_header ) metadata.append(routing_metadata) return self._inner_api_calls["create_device"]( request, retry=retry, timeout=timeout, metadata=metadata )
Sets the access control policy on the specified resource. Replaces any existing policy. Example: >>> from google.cloud import iot_v1 >>> >>> client = iot_v1.DeviceManagerClient() >>> >>> resource = client.registry_path('[PROJECT]', '[LOCATION]', '[REGISTRY]') >>> >>> # TODO: Initialize `policy`: >>> policy = {} >>> >>> response = client.set_iam_policy(resource, policy) Args: resource (str): REQUIRED: The resource for which the policy is being specified. ``resource`` is usually specified as a path. For example, a Project resource is specified as ``projects/{project}``. policy (Union[dict, ~google.cloud.iot_v1.types.Policy]): REQUIRED: The complete policy to be applied to the ``resource``. The size of the policy is limited to a few 10s of KB. An empty policy is a valid policy but certain Cloud Platform services (such as Projects) might reject them. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.iot_v1.types.Policy` retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.cloud.iot_v1.types.Policy` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. def set_iam_policy( self, resource, policy, retry=google.api_core.gapic_v1.method.DEFAULT, timeout=google.api_core.gapic_v1.method.DEFAULT, metadata=None, ): """ Sets the access control policy on the specified resource. Replaces any existing policy. Example: >>> from google.cloud import iot_v1 >>> >>> client = iot_v1.DeviceManagerClient() >>> >>> resource = client.registry_path('[PROJECT]', '[LOCATION]', '[REGISTRY]') >>> >>> # TODO: Initialize `policy`: >>> policy = {} >>> >>> response = client.set_iam_policy(resource, policy) Args: resource (str): REQUIRED: The resource for which the policy is being specified. ``resource`` is usually specified as a path. For example, a Project resource is specified as ``projects/{project}``. policy (Union[dict, ~google.cloud.iot_v1.types.Policy]): REQUIRED: The complete policy to be applied to the ``resource``. The size of the policy is limited to a few 10s of KB. An empty policy is a valid policy but certain Cloud Platform services (such as Projects) might reject them. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.iot_v1.types.Policy` retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.cloud.iot_v1.types.Policy` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. """ # Wrap the transport method to add retry and timeout logic. if "set_iam_policy" not in self._inner_api_calls: self._inner_api_calls[ "set_iam_policy" ] = google.api_core.gapic_v1.method.wrap_method( self.transport.set_iam_policy, default_retry=self._method_configs["SetIamPolicy"].retry, default_timeout=self._method_configs["SetIamPolicy"].timeout, client_info=self._client_info, ) request = iam_policy_pb2.SetIamPolicyRequest(resource=resource, policy=policy) if metadata is None: metadata = [] metadata = list(metadata) try: routing_header = [("resource", resource)] except AttributeError: pass else: routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata( routing_header ) metadata.append(routing_metadata) return self._inner_api_calls["set_iam_policy"]( request, retry=retry, timeout=timeout, metadata=metadata )
Associates the device with the gateway. Example: >>> from google.cloud import iot_v1 >>> >>> client = iot_v1.DeviceManagerClient() >>> >>> parent = client.registry_path('[PROJECT]', '[LOCATION]', '[REGISTRY]') >>> >>> # TODO: Initialize `gateway_id`: >>> gateway_id = '' >>> >>> # TODO: Initialize `device_id`: >>> device_id = '' >>> >>> response = client.bind_device_to_gateway(parent, gateway_id, device_id) Args: parent (str): The name of the registry. For example, ``projects/example-project/locations/us-central1/registries/my-registry``. gateway_id (str): The value of ``gateway_id`` can be either the device numeric ID or the user-defined device identifier. device_id (str): The device to associate with the specified gateway. The value of ``device_id`` can be either the device numeric ID or the user-defined device identifier. retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.cloud.iot_v1.types.BindDeviceToGatewayResponse` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. def bind_device_to_gateway( self, parent, gateway_id, device_id, retry=google.api_core.gapic_v1.method.DEFAULT, timeout=google.api_core.gapic_v1.method.DEFAULT, metadata=None, ): """ Associates the device with the gateway. Example: >>> from google.cloud import iot_v1 >>> >>> client = iot_v1.DeviceManagerClient() >>> >>> parent = client.registry_path('[PROJECT]', '[LOCATION]', '[REGISTRY]') >>> >>> # TODO: Initialize `gateway_id`: >>> gateway_id = '' >>> >>> # TODO: Initialize `device_id`: >>> device_id = '' >>> >>> response = client.bind_device_to_gateway(parent, gateway_id, device_id) Args: parent (str): The name of the registry. For example, ``projects/example-project/locations/us-central1/registries/my-registry``. gateway_id (str): The value of ``gateway_id`` can be either the device numeric ID or the user-defined device identifier. device_id (str): The device to associate with the specified gateway. The value of ``device_id`` can be either the device numeric ID or the user-defined device identifier. retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.cloud.iot_v1.types.BindDeviceToGatewayResponse` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. """ # Wrap the transport method to add retry and timeout logic. if "bind_device_to_gateway" not in self._inner_api_calls: self._inner_api_calls[ "bind_device_to_gateway" ] = google.api_core.gapic_v1.method.wrap_method( self.transport.bind_device_to_gateway, default_retry=self._method_configs["BindDeviceToGateway"].retry, default_timeout=self._method_configs["BindDeviceToGateway"].timeout, client_info=self._client_info, ) request = device_manager_pb2.BindDeviceToGatewayRequest( parent=parent, gateway_id=gateway_id, device_id=device_id ) if metadata is None: metadata = [] metadata = list(metadata) try: routing_header = [("parent", parent)] except AttributeError: pass else: routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata( routing_header ) metadata.append(routing_metadata) return self._inner_api_calls["bind_device_to_gateway"]( request, retry=retry, timeout=timeout, metadata=metadata )
Return a fully-qualified queue string. def queue_path(cls, project, location, queue): """Return a fully-qualified queue string.""" return google.api_core.path_template.expand( "projects/{project}/locations/{location}/queues/{queue}", project=project, location=location, queue=queue, )
Return a fully-qualified task string. def task_path(cls, project, location, queue, task): """Return a fully-qualified task string.""" return google.api_core.path_template.expand( "projects/{project}/locations/{location}/queues/{queue}/tasks/{task}", project=project, location=location, queue=queue, task=task, )
Creates a queue. Queues created with this method allow tasks to live for a maximum of 31 days. After a task is 31 days old, the task will be deleted regardless of whether it was dispatched or not. WARNING: Using this method may have unintended side effects if you are using an App Engine ``queue.yaml`` or ``queue.xml`` file to manage your queues. Read `Overview of Queue Management and queue.yaml <https://cloud.google.com/tasks/docs/queue-yaml>`__ before using this method. Example: >>> from google.cloud import tasks_v2 >>> >>> client = tasks_v2.CloudTasksClient() >>> >>> parent = client.location_path('[PROJECT]', '[LOCATION]') >>> >>> # TODO: Initialize `queue`: >>> queue = {} >>> >>> response = client.create_queue(parent, queue) Args: parent (str): Required. The location name in which the queue will be created. For example: ``projects/PROJECT_ID/locations/LOCATION_ID`` The list of allowed locations can be obtained by calling Cloud Tasks' implementation of ``ListLocations``. queue (Union[dict, ~google.cloud.tasks_v2.types.Queue]): Required. The queue to create. ``Queue's name`` cannot be the same as an existing queue. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.tasks_v2.types.Queue` retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.cloud.tasks_v2.types.Queue` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. def create_queue( self, parent, queue, retry=google.api_core.gapic_v1.method.DEFAULT, timeout=google.api_core.gapic_v1.method.DEFAULT, metadata=None, ): """ Creates a queue. Queues created with this method allow tasks to live for a maximum of 31 days. After a task is 31 days old, the task will be deleted regardless of whether it was dispatched or not. WARNING: Using this method may have unintended side effects if you are using an App Engine ``queue.yaml`` or ``queue.xml`` file to manage your queues. Read `Overview of Queue Management and queue.yaml <https://cloud.google.com/tasks/docs/queue-yaml>`__ before using this method. Example: >>> from google.cloud import tasks_v2 >>> >>> client = tasks_v2.CloudTasksClient() >>> >>> parent = client.location_path('[PROJECT]', '[LOCATION]') >>> >>> # TODO: Initialize `queue`: >>> queue = {} >>> >>> response = client.create_queue(parent, queue) Args: parent (str): Required. The location name in which the queue will be created. For example: ``projects/PROJECT_ID/locations/LOCATION_ID`` The list of allowed locations can be obtained by calling Cloud Tasks' implementation of ``ListLocations``. queue (Union[dict, ~google.cloud.tasks_v2.types.Queue]): Required. The queue to create. ``Queue's name`` cannot be the same as an existing queue. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.tasks_v2.types.Queue` retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.cloud.tasks_v2.types.Queue` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. """ # Wrap the transport method to add retry and timeout logic. if "create_queue" not in self._inner_api_calls: self._inner_api_calls[ "create_queue" ] = google.api_core.gapic_v1.method.wrap_method( self.transport.create_queue, default_retry=self._method_configs["CreateQueue"].retry, default_timeout=self._method_configs["CreateQueue"].timeout, client_info=self._client_info, ) request = cloudtasks_pb2.CreateQueueRequest(parent=parent, queue=queue) if metadata is None: metadata = [] metadata = list(metadata) try: routing_header = [("parent", parent)] except AttributeError: pass else: routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata( routing_header ) metadata.append(routing_metadata) return self._inner_api_calls["create_queue"]( request, retry=retry, timeout=timeout, metadata=metadata )
Creates a task and adds it to a queue. Tasks cannot be updated after creation; there is no UpdateTask command. - For ``App Engine queues``, the maximum task size is 100KB. Example: >>> from google.cloud import tasks_v2 >>> >>> client = tasks_v2.CloudTasksClient() >>> >>> parent = client.queue_path('[PROJECT]', '[LOCATION]', '[QUEUE]') >>> >>> # TODO: Initialize `task`: >>> task = {} >>> >>> response = client.create_task(parent, task) Args: parent (str): Required. The queue name. For example: ``projects/PROJECT_ID/locations/LOCATION_ID/queues/QUEUE_ID`` The queue must already exist. task (Union[dict, ~google.cloud.tasks_v2.types.Task]): Required. The task to add. Task names have the following format: ``projects/PROJECT_ID/locations/LOCATION_ID/queues/QUEUE_ID/tasks/TASK_ID``. The user can optionally specify a task ``name``. If a name is not specified then the system will generate a random unique task id, which will be set in the task returned in the ``response``. If ``schedule_time`` is not set or is in the past then Cloud Tasks will set it to the current time. Task De-duplication: Explicitly specifying a task ID enables task de-duplication. If a task's ID is identical to that of an existing task or a task that was deleted or executed recently then the call will fail with ``ALREADY_EXISTS``. If the task's queue was created using Cloud Tasks, then another task with the same name can't be created for ~1hour after the original task was deleted or executed. If the task's queue was created using queue.yaml or queue.xml, then another task with the same name can't be created for ~9days after the original task was deleted or executed. Because there is an extra lookup cost to identify duplicate task names, these ``CreateTask`` calls have significantly increased latency. Using hashed strings for the task id or for the prefix of the task id is recommended. Choosing task ids that are sequential or have sequential prefixes, for example using a timestamp, causes an increase in latency and error rates in all task commands. The infrastructure relies on an approximately uniform distribution of task ids to store and serve tasks efficiently. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.tasks_v2.types.Task` response_view (~google.cloud.tasks_v2.types.View): The response\_view specifies which subset of the ``Task`` will be returned. By default response\_view is ``BASIC``; not all information is retrieved by default because some data, such as payloads, might be desirable to return only when needed because of its large size or because of the sensitivity of data that it contains. Authorization for ``FULL`` requires ``cloudtasks.tasks.fullView`` `Google IAM <https://cloud.google.com/iam/>`___ permission on the ``Task`` resource. retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.cloud.tasks_v2.types.Task` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. def create_task( self, parent, task, response_view=None, retry=google.api_core.gapic_v1.method.DEFAULT, timeout=google.api_core.gapic_v1.method.DEFAULT, metadata=None, ): """ Creates a task and adds it to a queue. Tasks cannot be updated after creation; there is no UpdateTask command. - For ``App Engine queues``, the maximum task size is 100KB. Example: >>> from google.cloud import tasks_v2 >>> >>> client = tasks_v2.CloudTasksClient() >>> >>> parent = client.queue_path('[PROJECT]', '[LOCATION]', '[QUEUE]') >>> >>> # TODO: Initialize `task`: >>> task = {} >>> >>> response = client.create_task(parent, task) Args: parent (str): Required. The queue name. For example: ``projects/PROJECT_ID/locations/LOCATION_ID/queues/QUEUE_ID`` The queue must already exist. task (Union[dict, ~google.cloud.tasks_v2.types.Task]): Required. The task to add. Task names have the following format: ``projects/PROJECT_ID/locations/LOCATION_ID/queues/QUEUE_ID/tasks/TASK_ID``. The user can optionally specify a task ``name``. If a name is not specified then the system will generate a random unique task id, which will be set in the task returned in the ``response``. If ``schedule_time`` is not set or is in the past then Cloud Tasks will set it to the current time. Task De-duplication: Explicitly specifying a task ID enables task de-duplication. If a task's ID is identical to that of an existing task or a task that was deleted or executed recently then the call will fail with ``ALREADY_EXISTS``. If the task's queue was created using Cloud Tasks, then another task with the same name can't be created for ~1hour after the original task was deleted or executed. If the task's queue was created using queue.yaml or queue.xml, then another task with the same name can't be created for ~9days after the original task was deleted or executed. Because there is an extra lookup cost to identify duplicate task names, these ``CreateTask`` calls have significantly increased latency. Using hashed strings for the task id or for the prefix of the task id is recommended. Choosing task ids that are sequential or have sequential prefixes, for example using a timestamp, causes an increase in latency and error rates in all task commands. The infrastructure relies on an approximately uniform distribution of task ids to store and serve tasks efficiently. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.tasks_v2.types.Task` response_view (~google.cloud.tasks_v2.types.View): The response\_view specifies which subset of the ``Task`` will be returned. By default response\_view is ``BASIC``; not all information is retrieved by default because some data, such as payloads, might be desirable to return only when needed because of its large size or because of the sensitivity of data that it contains. Authorization for ``FULL`` requires ``cloudtasks.tasks.fullView`` `Google IAM <https://cloud.google.com/iam/>`___ permission on the ``Task`` resource. retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.cloud.tasks_v2.types.Task` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. """ # Wrap the transport method to add retry and timeout logic. if "create_task" not in self._inner_api_calls: self._inner_api_calls[ "create_task" ] = google.api_core.gapic_v1.method.wrap_method( self.transport.create_task, default_retry=self._method_configs["CreateTask"].retry, default_timeout=self._method_configs["CreateTask"].timeout, client_info=self._client_info, ) request = cloudtasks_pb2.CreateTaskRequest( parent=parent, task=task, response_view=response_view ) if metadata is None: metadata = [] metadata = list(metadata) try: routing_header = [("parent", parent)] except AttributeError: pass else: routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata( routing_header ) metadata.append(routing_metadata) return self._inner_api_calls["create_task"]( request, retry=retry, timeout=timeout, metadata=metadata )
A generator that yields exponential timeout values. Args: initial (float): The initial timeout. maximum (float): The maximum timeout. multiplier (float): The multiplier applied to the timeout. deadline (float): The overall deadline across all invocations. Yields: float: A timeout value. def _exponential_timeout_generator(initial, maximum, multiplier, deadline): """A generator that yields exponential timeout values. Args: initial (float): The initial timeout. maximum (float): The maximum timeout. multiplier (float): The multiplier applied to the timeout. deadline (float): The overall deadline across all invocations. Yields: float: A timeout value. """ if deadline is not None: deadline_datetime = datetime_helpers.utcnow() + datetime.timedelta( seconds=deadline ) else: deadline_datetime = datetime.datetime.max timeout = initial while True: now = datetime_helpers.utcnow() yield min( # The calculated timeout based on invocations. timeout, # The set maximum timeout. maximum, # The remaining time before the deadline is reached. float((deadline_datetime - now).seconds), ) timeout = timeout * multiplier
Formats parameters in operation in the way BigQuery expects. The input operation will be a query like ``SELECT %s`` and the output will be a query like ``SELECT ?``. :type operation: str :param operation: A Google BigQuery query string. :type parameters: Sequence[Any] :param parameters: Sequence of parameter values. :rtype: str :returns: A formatted query string. :raises: :class:`~google.cloud.bigquery.dbapi.ProgrammingError` if a parameter used in the operation is not found in the ``parameters`` argument. def _format_operation_list(operation, parameters): """Formats parameters in operation in the way BigQuery expects. The input operation will be a query like ``SELECT %s`` and the output will be a query like ``SELECT ?``. :type operation: str :param operation: A Google BigQuery query string. :type parameters: Sequence[Any] :param parameters: Sequence of parameter values. :rtype: str :returns: A formatted query string. :raises: :class:`~google.cloud.bigquery.dbapi.ProgrammingError` if a parameter used in the operation is not found in the ``parameters`` argument. """ formatted_params = ["?" for _ in parameters] try: return operation % tuple(formatted_params) except TypeError as exc: raise exceptions.ProgrammingError(exc)
Formats parameters in operation in the way BigQuery expects. The input operation will be a query like ``SELECT %(namedparam)s`` and the output will be a query like ``SELECT @namedparam``. :type operation: str :param operation: A Google BigQuery query string. :type parameters: Mapping[str, Any] :param parameters: Dictionary of parameter values. :rtype: str :returns: A formatted query string. :raises: :class:`~google.cloud.bigquery.dbapi.ProgrammingError` if a parameter used in the operation is not found in the ``parameters`` argument. def _format_operation_dict(operation, parameters): """Formats parameters in operation in the way BigQuery expects. The input operation will be a query like ``SELECT %(namedparam)s`` and the output will be a query like ``SELECT @namedparam``. :type operation: str :param operation: A Google BigQuery query string. :type parameters: Mapping[str, Any] :param parameters: Dictionary of parameter values. :rtype: str :returns: A formatted query string. :raises: :class:`~google.cloud.bigquery.dbapi.ProgrammingError` if a parameter used in the operation is not found in the ``parameters`` argument. """ formatted_params = {} for name in parameters: escaped_name = name.replace("`", r"\`") formatted_params[name] = "@`{}`".format(escaped_name) try: return operation % formatted_params except KeyError as exc: raise exceptions.ProgrammingError(exc)
Formats parameters in operation in way BigQuery expects. :type: str :param operation: A Google BigQuery query string. :type: Mapping[str, Any] or Sequence[Any] :param parameters: Optional parameter values. :rtype: str :returns: A formatted query string. :raises: :class:`~google.cloud.bigquery.dbapi.ProgrammingError` if a parameter used in the operation is not found in the ``parameters`` argument. def _format_operation(operation, parameters=None): """Formats parameters in operation in way BigQuery expects. :type: str :param operation: A Google BigQuery query string. :type: Mapping[str, Any] or Sequence[Any] :param parameters: Optional parameter values. :rtype: str :returns: A formatted query string. :raises: :class:`~google.cloud.bigquery.dbapi.ProgrammingError` if a parameter used in the operation is not found in the ``parameters`` argument. """ if parameters is None: return operation if isinstance(parameters, collections_abc.Mapping): return _format_operation_dict(operation, parameters) return _format_operation_list(operation, parameters)
Set description from schema. :type schema: Sequence[google.cloud.bigquery.schema.SchemaField] :param schema: A description of fields in the schema. def _set_description(self, schema): """Set description from schema. :type schema: Sequence[google.cloud.bigquery.schema.SchemaField] :param schema: A description of fields in the schema. """ if schema is None: self.description = None return self.description = tuple( [ Column( name=field.name, type_code=field.field_type, display_size=None, internal_size=None, precision=None, scale=None, null_ok=field.is_nullable, ) for field in schema ] )
Set the rowcount from query results. Normally, this sets rowcount to the number of rows returned by the query, but if it was a DML statement, it sets rowcount to the number of modified rows. :type query_results: :class:`~google.cloud.bigquery.query._QueryResults` :param query_results: results of a query def _set_rowcount(self, query_results): """Set the rowcount from query results. Normally, this sets rowcount to the number of rows returned by the query, but if it was a DML statement, it sets rowcount to the number of modified rows. :type query_results: :class:`~google.cloud.bigquery.query._QueryResults` :param query_results: results of a query """ total_rows = 0 num_dml_affected_rows = query_results.num_dml_affected_rows if query_results.total_rows is not None and query_results.total_rows > 0: total_rows = query_results.total_rows if num_dml_affected_rows is not None and num_dml_affected_rows > 0: total_rows = num_dml_affected_rows self.rowcount = total_rows
Prepare and execute a database operation. .. note:: When setting query parameters, values which are "text" (``unicode`` in Python2, ``str`` in Python3) will use the 'STRING' BigQuery type. Values which are "bytes" (``str`` in Python2, ``bytes`` in Python3), will use using the 'BYTES' type. A `~datetime.datetime` parameter without timezone information uses the 'DATETIME' BigQuery type (example: Global Pi Day Celebration March 14, 2017 at 1:59pm). A `~datetime.datetime` parameter with timezone information uses the 'TIMESTAMP' BigQuery type (example: a wedding on April 29, 2011 at 11am, British Summer Time). For more information about BigQuery data types, see: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types ``STRUCT``/``RECORD`` and ``REPEATED`` query parameters are not yet supported. See: https://github.com/GoogleCloudPlatform/google-cloud-python/issues/3524 :type operation: str :param operation: A Google BigQuery query string. :type parameters: Mapping[str, Any] or Sequence[Any] :param parameters: (Optional) dictionary or sequence of parameter values. :type job_id: str :param job_id: (Optional) The job_id to use. If not set, a job ID is generated at random. def execute(self, operation, parameters=None, job_id=None): """Prepare and execute a database operation. .. note:: When setting query parameters, values which are "text" (``unicode`` in Python2, ``str`` in Python3) will use the 'STRING' BigQuery type. Values which are "bytes" (``str`` in Python2, ``bytes`` in Python3), will use using the 'BYTES' type. A `~datetime.datetime` parameter without timezone information uses the 'DATETIME' BigQuery type (example: Global Pi Day Celebration March 14, 2017 at 1:59pm). A `~datetime.datetime` parameter with timezone information uses the 'TIMESTAMP' BigQuery type (example: a wedding on April 29, 2011 at 11am, British Summer Time). For more information about BigQuery data types, see: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types ``STRUCT``/``RECORD`` and ``REPEATED`` query parameters are not yet supported. See: https://github.com/GoogleCloudPlatform/google-cloud-python/issues/3524 :type operation: str :param operation: A Google BigQuery query string. :type parameters: Mapping[str, Any] or Sequence[Any] :param parameters: (Optional) dictionary or sequence of parameter values. :type job_id: str :param job_id: (Optional) The job_id to use. If not set, a job ID is generated at random. """ self._query_data = None self._query_job = None client = self.connection._client # The DB-API uses the pyformat formatting, since the way BigQuery does # query parameters was not one of the standard options. Convert both # the query and the parameters to the format expected by the client # libraries. formatted_operation = _format_operation(operation, parameters=parameters) query_parameters = _helpers.to_query_parameters(parameters) config = job.QueryJobConfig() config.query_parameters = query_parameters config.use_legacy_sql = False self._query_job = client.query( formatted_operation, job_config=config, job_id=job_id ) # Wait for the query to finish. try: self._query_job.result() except google.cloud.exceptions.GoogleCloudError as exc: raise exceptions.DatabaseError(exc) query_results = self._query_job._query_results self._set_rowcount(query_results) self._set_description(query_results.schema)
Try to start fetching data, if not yet started. Mutates self to indicate that iteration has started. def _try_fetch(self, size=None): """Try to start fetching data, if not yet started. Mutates self to indicate that iteration has started. """ if self._query_job is None: raise exceptions.InterfaceError( "No query results: execute() must be called before fetch." ) is_dml = ( self._query_job.statement_type and self._query_job.statement_type.upper() != "SELECT" ) if is_dml: self._query_data = iter([]) return if self._query_data is None: client = self.connection._client rows_iter = client.list_rows( self._query_job.destination, selected_fields=self._query_job._query_results.schema, page_size=self.arraysize, ) self._query_data = iter(rows_iter)
Fetch multiple results from the last ``execute*()`` call. .. note:: The size parameter is not used for the request/response size. Set the ``arraysize`` attribute before calling ``execute()`` to set the batch size. :type size: int :param size: (Optional) Maximum number of rows to return. Defaults to the ``arraysize`` property value. :rtype: List[tuple] :returns: A list of rows. :raises: :class:`~google.cloud.bigquery.dbapi.InterfaceError` if called before ``execute()``. def fetchmany(self, size=None): """Fetch multiple results from the last ``execute*()`` call. .. note:: The size parameter is not used for the request/response size. Set the ``arraysize`` attribute before calling ``execute()`` to set the batch size. :type size: int :param size: (Optional) Maximum number of rows to return. Defaults to the ``arraysize`` property value. :rtype: List[tuple] :returns: A list of rows. :raises: :class:`~google.cloud.bigquery.dbapi.InterfaceError` if called before ``execute()``. """ if size is None: size = self.arraysize self._try_fetch(size=size) rows = [] for row in self._query_data: rows.append(row) if len(rows) >= size: break return rows
Create and return a gRPC channel object. Args: address (str): The host for the channel to use. credentials (~.Credentials): The authorization credentials to attach to requests. These credentials identify this application to the service. If none are specified, the client will attempt to ascertain the credentials from the environment. Returns: grpc.Channel: A gRPC channel object. def create_channel(cls, address="spanner.googleapis.com:443", credentials=None): """Create and return a gRPC channel object. Args: address (str): The host for the channel to use. credentials (~.Credentials): The authorization credentials to attach to requests. These credentials identify this application to the service. If none are specified, the client will attempt to ascertain the credentials from the environment. Returns: grpc.Channel: A gRPC channel object. """ grpc_gcp_config = grpc_gcp.api_config_from_text_pb( pkg_resources.resource_string(__name__, _SPANNER_GRPC_CONFIG) ) options = [(grpc_gcp.API_CONFIG_CHANNEL_ARG, grpc_gcp_config)] return google.api_core.grpc_helpers.create_channel( address, credentials=credentials, scopes=cls._OAUTH_SCOPES )
Helper for API methods returning sink resources. def _update_from_api_repr(self, resource): """Helper for API methods returning sink resources.""" self.destination = resource["destination"] self.filter_ = resource.get("filter") self._writer_identity = resource.get("writerIdentity")
Factory: construct a sink given its API representation :type resource: dict :param resource: sink resource representation returned from the API :type client: :class:`google.cloud.logging.client.Client` :param client: Client which holds credentials and project configuration for the sink. :rtype: :class:`google.cloud.logging.sink.Sink` :returns: Sink parsed from ``resource``. :raises: :class:`ValueError` if ``client`` is not ``None`` and the project from the resource does not agree with the project from the client. def from_api_repr(cls, resource, client): """Factory: construct a sink given its API representation :type resource: dict :param resource: sink resource representation returned from the API :type client: :class:`google.cloud.logging.client.Client` :param client: Client which holds credentials and project configuration for the sink. :rtype: :class:`google.cloud.logging.sink.Sink` :returns: Sink parsed from ``resource``. :raises: :class:`ValueError` if ``client`` is not ``None`` and the project from the resource does not agree with the project from the client. """ sink_name = resource["name"] instance = cls(sink_name, client=client) instance._update_from_api_repr(resource) return instance
API call: create the sink via a PUT request See https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.sinks/create :type client: :class:`~google.cloud.logging.client.Client` or ``NoneType`` :param client: the client to use. If not passed, falls back to the ``client`` stored on the current sink. :type unique_writer_identity: bool :param unique_writer_identity: (Optional) determines the kind of IAM identity returned as writer_identity in the new sink. def create(self, client=None, unique_writer_identity=False): """API call: create the sink via a PUT request See https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.sinks/create :type client: :class:`~google.cloud.logging.client.Client` or ``NoneType`` :param client: the client to use. If not passed, falls back to the ``client`` stored on the current sink. :type unique_writer_identity: bool :param unique_writer_identity: (Optional) determines the kind of IAM identity returned as writer_identity in the new sink. """ client = self._require_client(client) resource = client.sinks_api.sink_create( self.project, self.name, self.filter_, self.destination, unique_writer_identity=unique_writer_identity, ) self._update_from_api_repr(resource)
API call: test for the existence of the sink via a GET request See https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.sinks/get :type client: :class:`~google.cloud.logging.client.Client` or ``NoneType`` :param client: the client to use. If not passed, falls back to the ``client`` stored on the current sink. :rtype: bool :returns: Boolean indicating existence of the sink. def exists(self, client=None): """API call: test for the existence of the sink via a GET request See https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.sinks/get :type client: :class:`~google.cloud.logging.client.Client` or ``NoneType`` :param client: the client to use. If not passed, falls back to the ``client`` stored on the current sink. :rtype: bool :returns: Boolean indicating existence of the sink. """ client = self._require_client(client) try: client.sinks_api.sink_get(self.project, self.name) except NotFound: return False else: return True
API call: sync local sink configuration via a GET request See https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.sinks/get :type client: :class:`~google.cloud.logging.client.Client` or ``NoneType`` :param client: the client to use. If not passed, falls back to the ``client`` stored on the current sink. def reload(self, client=None): """API call: sync local sink configuration via a GET request See https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.sinks/get :type client: :class:`~google.cloud.logging.client.Client` or ``NoneType`` :param client: the client to use. If not passed, falls back to the ``client`` stored on the current sink. """ client = self._require_client(client) resource = client.sinks_api.sink_get(self.project, self.name) self._update_from_api_repr(resource)
API call: update sink configuration via a PUT request See https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.sinks/update :type client: :class:`~google.cloud.logging.client.Client` or ``NoneType`` :param client: the client to use. If not passed, falls back to the ``client`` stored on the current sink. :type unique_writer_identity: bool :param unique_writer_identity: (Optional) determines the kind of IAM identity returned as writer_identity in the new sink. def update(self, client=None, unique_writer_identity=False): """API call: update sink configuration via a PUT request See https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.sinks/update :type client: :class:`~google.cloud.logging.client.Client` or ``NoneType`` :param client: the client to use. If not passed, falls back to the ``client`` stored on the current sink. :type unique_writer_identity: bool :param unique_writer_identity: (Optional) determines the kind of IAM identity returned as writer_identity in the new sink. """ client = self._require_client(client) resource = client.sinks_api.sink_update( self.project, self.name, self.filter_, self.destination, unique_writer_identity=unique_writer_identity, ) self._update_from_api_repr(resource)
API call: delete a sink via a DELETE request See https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.sinks/delete :type client: :class:`~google.cloud.logging.client.Client` or ``NoneType`` :param client: the client to use. If not passed, falls back to the ``client`` stored on the current sink. def delete(self, client=None): """API call: delete a sink via a DELETE request See https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.sinks/delete :type client: :class:`~google.cloud.logging.client.Client` or ``NoneType`` :param client: the client to use. If not passed, falls back to the ``client`` stored on the current sink. """ client = self._require_client(client) client.sinks_api.sink_delete(self.project, self.name)
Helper for '_merge_by_type'. def _merge_float64(lhs, rhs, type_): # pylint: disable=unused-argument """Helper for '_merge_by_type'.""" lhs_kind = lhs.WhichOneof("kind") if lhs_kind == "string_value": return Value(string_value=lhs.string_value + rhs.string_value) rhs_kind = rhs.WhichOneof("kind") array_continuation = ( lhs_kind == "number_value" and rhs_kind == "string_value" and rhs.string_value == "" ) if array_continuation: return lhs raise Unmergeable(lhs, rhs, type_)
Helper for '_merge_by_type'. def _merge_string(lhs, rhs, type_): # pylint: disable=unused-argument """Helper for '_merge_by_type'.""" return Value(string_value=lhs.string_value + rhs.string_value)
Helper for '_merge_by_type'. def _merge_array(lhs, rhs, type_): """Helper for '_merge_by_type'.""" element_type = type_.array_element_type if element_type.code in _UNMERGEABLE_TYPES: # Individual values cannot be merged, just concatenate lhs.list_value.values.extend(rhs.list_value.values) return lhs lhs, rhs = list(lhs.list_value.values), list(rhs.list_value.values) # Sanity check: If either list is empty, short-circuit. # This is effectively a no-op. if not len(lhs) or not len(rhs): return Value(list_value=ListValue(values=(lhs + rhs))) first = rhs.pop(0) if first.HasField("null_value"): # can't merge lhs.append(first) else: last = lhs.pop() try: merged = _merge_by_type(last, first, element_type) except Unmergeable: lhs.append(last) lhs.append(first) else: lhs.append(merged) return Value(list_value=ListValue(values=(lhs + rhs)))
Helper for '_merge_by_type'. def _merge_struct(lhs, rhs, type_): """Helper for '_merge_by_type'.""" fields = type_.struct_type.fields lhs, rhs = list(lhs.list_value.values), list(rhs.list_value.values) candidate_type = fields[len(lhs) - 1].type first = rhs.pop(0) if first.HasField("null_value") or candidate_type.code in _UNMERGEABLE_TYPES: lhs.append(first) else: last = lhs.pop() lhs.append(_merge_by_type(last, first, candidate_type)) return Value(list_value=ListValue(values=lhs + rhs))
Helper for '_merge_chunk'. def _merge_by_type(lhs, rhs, type_): """Helper for '_merge_chunk'.""" merger = _MERGE_BY_TYPE[type_.code] return merger(lhs, rhs, type_)
Merge pending chunk with next value. :type value: :class:`~google.protobuf.struct_pb2.Value` :param value: continuation of chunked value from previous partial result set. :rtype: :class:`~google.protobuf.struct_pb2.Value` :returns: the merged value def _merge_chunk(self, value): """Merge pending chunk with next value. :type value: :class:`~google.protobuf.struct_pb2.Value` :param value: continuation of chunked value from previous partial result set. :rtype: :class:`~google.protobuf.struct_pb2.Value` :returns: the merged value """ current_column = len(self._current_row) field = self.fields[current_column] merged = _merge_by_type(self._pending_chunk, value, field.type) self._pending_chunk = None return merged
Merge values into rows. :type values: list of :class:`~google.protobuf.struct_pb2.Value` :param values: non-chunked values from partial result set. def _merge_values(self, values): """Merge values into rows. :type values: list of :class:`~google.protobuf.struct_pb2.Value` :param values: non-chunked values from partial result set. """ width = len(self.fields) for value in values: index = len(self._current_row) field = self.fields[index] self._current_row.append(_parse_value_pb(value, field.type)) if len(self._current_row) == width: self._rows.append(self._current_row) self._current_row = []
Consume the next partial result set from the stream. Parse the result set into new/existing rows in :attr:`_rows` def _consume_next(self): """Consume the next partial result set from the stream. Parse the result set into new/existing rows in :attr:`_rows` """ response = six.next(self._response_iterator) self._counter += 1 if self._metadata is None: # first response metadata = self._metadata = response.metadata source = self._source if source is not None and source._transaction_id is None: source._transaction_id = metadata.transaction.id if response.HasField("stats"): # last response self._stats = response.stats values = list(response.values) if self._pending_chunk is not None: values[0] = self._merge_chunk(values[0]) if response.chunked_value: self._pending_chunk = values.pop() self._merge_values(values)
Return exactly one result, or None if there are no results. :raises: :exc:`ValueError`: If there are multiple results. :raises: :exc:`RuntimeError`: If consumption has already occurred, in whole or in part. def one_or_none(self): """Return exactly one result, or None if there are no results. :raises: :exc:`ValueError`: If there are multiple results. :raises: :exc:`RuntimeError`: If consumption has already occurred, in whole or in part. """ # Sanity check: Has consumption of this query already started? # If it has, then this is an exception. if self._metadata is not None: raise RuntimeError( "Can not call `.one` or `.one_or_none` after " "stream consumption has already started." ) # Consume the first result of the stream. # If there is no first result, then return None. iterator = iter(self) try: answer = next(iterator) except StopIteration: return None # Attempt to consume more. This should no-op; if we get additional # rows, then this is an error case. try: next(iterator) raise ValueError("Expected one result; got more.") except StopIteration: return answer
Return a fully-qualified product_set string. def product_set_path(cls, project, location, product_set): """Return a fully-qualified product_set string.""" return google.api_core.path_template.expand( "projects/{project}/locations/{location}/productSets/{product_set}", project=project, location=location, product_set=product_set, )
Return a fully-qualified product string. def product_path(cls, project, location, product): """Return a fully-qualified product string.""" return google.api_core.path_template.expand( "projects/{project}/locations/{location}/products/{product}", project=project, location=location, product=product, )
Return a fully-qualified reference_image string. def reference_image_path(cls, project, location, product, reference_image): """Return a fully-qualified reference_image string.""" return google.api_core.path_template.expand( "projects/{project}/locations/{location}/products/{product}/referenceImages/{reference_image}", project=project, location=location, product=product, reference_image=reference_image, )
Creates and returns a new ProductSet resource. Possible errors: - Returns INVALID\_ARGUMENT if display\_name is missing, or is longer than 4096 characters. Example: >>> from google.cloud import vision_v1p3beta1 >>> >>> client = vision_v1p3beta1.ProductSearchClient() >>> >>> parent = client.location_path('[PROJECT]', '[LOCATION]') >>> >>> # TODO: Initialize `product_set`: >>> product_set = {} >>> >>> # TODO: Initialize `product_set_id`: >>> product_set_id = '' >>> >>> response = client.create_product_set(parent, product_set, product_set_id) Args: parent (str): The project in which the ProductSet should be created. Format is ``projects/PROJECT_ID/locations/LOC_ID``. product_set (Union[dict, ~google.cloud.vision_v1p3beta1.types.ProductSet]): The ProductSet to create. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.vision_v1p3beta1.types.ProductSet` product_set_id (str): A user-supplied resource id for this ProductSet. If set, the server will attempt to use this value as the resource id. If it is already in use, an error is returned with code ALREADY\_EXISTS. Must be at most 128 characters long. It cannot contain the character ``/``. retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.cloud.vision_v1p3beta1.types.ProductSet` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. def create_product_set( self, parent, product_set, product_set_id, retry=google.api_core.gapic_v1.method.DEFAULT, timeout=google.api_core.gapic_v1.method.DEFAULT, metadata=None, ): """ Creates and returns a new ProductSet resource. Possible errors: - Returns INVALID\_ARGUMENT if display\_name is missing, or is longer than 4096 characters. Example: >>> from google.cloud import vision_v1p3beta1 >>> >>> client = vision_v1p3beta1.ProductSearchClient() >>> >>> parent = client.location_path('[PROJECT]', '[LOCATION]') >>> >>> # TODO: Initialize `product_set`: >>> product_set = {} >>> >>> # TODO: Initialize `product_set_id`: >>> product_set_id = '' >>> >>> response = client.create_product_set(parent, product_set, product_set_id) Args: parent (str): The project in which the ProductSet should be created. Format is ``projects/PROJECT_ID/locations/LOC_ID``. product_set (Union[dict, ~google.cloud.vision_v1p3beta1.types.ProductSet]): The ProductSet to create. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.vision_v1p3beta1.types.ProductSet` product_set_id (str): A user-supplied resource id for this ProductSet. If set, the server will attempt to use this value as the resource id. If it is already in use, an error is returned with code ALREADY\_EXISTS. Must be at most 128 characters long. It cannot contain the character ``/``. retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.cloud.vision_v1p3beta1.types.ProductSet` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. """ # Wrap the transport method to add retry and timeout logic. if "create_product_set" not in self._inner_api_calls: self._inner_api_calls[ "create_product_set" ] = google.api_core.gapic_v1.method.wrap_method( self.transport.create_product_set, default_retry=self._method_configs["CreateProductSet"].retry, default_timeout=self._method_configs["CreateProductSet"].timeout, client_info=self._client_info, ) request = product_search_service_pb2.CreateProductSetRequest( parent=parent, product_set=product_set, product_set_id=product_set_id ) if metadata is None: metadata = [] metadata = list(metadata) try: routing_header = [("parent", parent)] except AttributeError: pass else: routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata( routing_header ) metadata.append(routing_metadata) return self._inner_api_calls["create_product_set"]( request, retry=retry, timeout=timeout, metadata=metadata )
Creates and returns a new ReferenceImage resource. The ``bounding_poly`` field is optional. If ``bounding_poly`` is not specified, the system will try to detect regions of interest in the image that are compatible with the product\_category on the parent product. If it is specified, detection is ALWAYS skipped. The system converts polygons into non-rotated rectangles. Note that the pipeline will resize the image if the image resolution is too large to process (above 50MP). Possible errors: - Returns INVALID\_ARGUMENT if the image\_uri is missing or longer than 4096 characters. - Returns INVALID\_ARGUMENT if the product does not exist. - Returns INVALID\_ARGUMENT if bounding\_poly is not provided, and nothing compatible with the parent product's product\_category is detected. - Returns INVALID\_ARGUMENT if bounding\_poly contains more than 10 polygons. Example: >>> from google.cloud import vision_v1p3beta1 >>> >>> client = vision_v1p3beta1.ProductSearchClient() >>> >>> parent = client.product_path('[PROJECT]', '[LOCATION]', '[PRODUCT]') >>> >>> # TODO: Initialize `reference_image`: >>> reference_image = {} >>> >>> # TODO: Initialize `reference_image_id`: >>> reference_image_id = '' >>> >>> response = client.create_reference_image(parent, reference_image, reference_image_id) Args: parent (str): Resource name of the product in which to create the reference image. Format is ``projects/PROJECT_ID/locations/LOC_ID/products/PRODUCT_ID``. reference_image (Union[dict, ~google.cloud.vision_v1p3beta1.types.ReferenceImage]): The reference image to create. If an image ID is specified, it is ignored. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.vision_v1p3beta1.types.ReferenceImage` reference_image_id (str): A user-supplied resource id for the ReferenceImage to be added. If set, the server will attempt to use this value as the resource id. If it is already in use, an error is returned with code ALREADY\_EXISTS. Must be at most 128 characters long. It cannot contain the character ``/``. retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.cloud.vision_v1p3beta1.types.ReferenceImage` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. def create_reference_image( self, parent, reference_image, reference_image_id, retry=google.api_core.gapic_v1.method.DEFAULT, timeout=google.api_core.gapic_v1.method.DEFAULT, metadata=None, ): """ Creates and returns a new ReferenceImage resource. The ``bounding_poly`` field is optional. If ``bounding_poly`` is not specified, the system will try to detect regions of interest in the image that are compatible with the product\_category on the parent product. If it is specified, detection is ALWAYS skipped. The system converts polygons into non-rotated rectangles. Note that the pipeline will resize the image if the image resolution is too large to process (above 50MP). Possible errors: - Returns INVALID\_ARGUMENT if the image\_uri is missing or longer than 4096 characters. - Returns INVALID\_ARGUMENT if the product does not exist. - Returns INVALID\_ARGUMENT if bounding\_poly is not provided, and nothing compatible with the parent product's product\_category is detected. - Returns INVALID\_ARGUMENT if bounding\_poly contains more than 10 polygons. Example: >>> from google.cloud import vision_v1p3beta1 >>> >>> client = vision_v1p3beta1.ProductSearchClient() >>> >>> parent = client.product_path('[PROJECT]', '[LOCATION]', '[PRODUCT]') >>> >>> # TODO: Initialize `reference_image`: >>> reference_image = {} >>> >>> # TODO: Initialize `reference_image_id`: >>> reference_image_id = '' >>> >>> response = client.create_reference_image(parent, reference_image, reference_image_id) Args: parent (str): Resource name of the product in which to create the reference image. Format is ``projects/PROJECT_ID/locations/LOC_ID/products/PRODUCT_ID``. reference_image (Union[dict, ~google.cloud.vision_v1p3beta1.types.ReferenceImage]): The reference image to create. If an image ID is specified, it is ignored. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.vision_v1p3beta1.types.ReferenceImage` reference_image_id (str): A user-supplied resource id for the ReferenceImage to be added. If set, the server will attempt to use this value as the resource id. If it is already in use, an error is returned with code ALREADY\_EXISTS. Must be at most 128 characters long. It cannot contain the character ``/``. retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.cloud.vision_v1p3beta1.types.ReferenceImage` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. """ # Wrap the transport method to add retry and timeout logic. if "create_reference_image" not in self._inner_api_calls: self._inner_api_calls[ "create_reference_image" ] = google.api_core.gapic_v1.method.wrap_method( self.transport.create_reference_image, default_retry=self._method_configs["CreateReferenceImage"].retry, default_timeout=self._method_configs["CreateReferenceImage"].timeout, client_info=self._client_info, ) request = product_search_service_pb2.CreateReferenceImageRequest( parent=parent, reference_image=reference_image, reference_image_id=reference_image_id, ) if metadata is None: metadata = [] metadata = list(metadata) try: routing_header = [("parent", parent)] except AttributeError: pass else: routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata( routing_header ) metadata.append(routing_metadata) return self._inner_api_calls["create_reference_image"]( request, retry=retry, timeout=timeout, metadata=metadata )
Asynchronous API that imports a list of reference images to specified product sets based on a list of image information. The ``google.longrunning.Operation`` API can be used to keep track of the progress and results of the request. ``Operation.metadata`` contains ``BatchOperationMetadata``. (progress) ``Operation.response`` contains ``ImportProductSetsResponse``. (results) The input source of this method is a csv file on Google Cloud Storage. For the format of the csv file please see ``ImportProductSetsGcsSource.csv_file_uri``. Example: >>> from google.cloud import vision_v1p3beta1 >>> >>> client = vision_v1p3beta1.ProductSearchClient() >>> >>> parent = client.location_path('[PROJECT]', '[LOCATION]') >>> >>> # TODO: Initialize `input_config`: >>> input_config = {} >>> >>> response = client.import_product_sets(parent, input_config) >>> >>> def callback(operation_future): ... # Handle result. ... result = operation_future.result() >>> >>> response.add_done_callback(callback) >>> >>> # Handle metadata. >>> metadata = response.metadata() Args: parent (str): The project in which the ProductSets should be imported. Format is ``projects/PROJECT_ID/locations/LOC_ID``. input_config (Union[dict, ~google.cloud.vision_v1p3beta1.types.ImportProductSetsInputConfig]): The input content for the list of requests. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.vision_v1p3beta1.types.ImportProductSetsInputConfig` retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.cloud.vision_v1p3beta1.types._OperationFuture` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. def import_product_sets( self, parent, input_config, retry=google.api_core.gapic_v1.method.DEFAULT, timeout=google.api_core.gapic_v1.method.DEFAULT, metadata=None, ): """ Asynchronous API that imports a list of reference images to specified product sets based on a list of image information. The ``google.longrunning.Operation`` API can be used to keep track of the progress and results of the request. ``Operation.metadata`` contains ``BatchOperationMetadata``. (progress) ``Operation.response`` contains ``ImportProductSetsResponse``. (results) The input source of this method is a csv file on Google Cloud Storage. For the format of the csv file please see ``ImportProductSetsGcsSource.csv_file_uri``. Example: >>> from google.cloud import vision_v1p3beta1 >>> >>> client = vision_v1p3beta1.ProductSearchClient() >>> >>> parent = client.location_path('[PROJECT]', '[LOCATION]') >>> >>> # TODO: Initialize `input_config`: >>> input_config = {} >>> >>> response = client.import_product_sets(parent, input_config) >>> >>> def callback(operation_future): ... # Handle result. ... result = operation_future.result() >>> >>> response.add_done_callback(callback) >>> >>> # Handle metadata. >>> metadata = response.metadata() Args: parent (str): The project in which the ProductSets should be imported. Format is ``projects/PROJECT_ID/locations/LOC_ID``. input_config (Union[dict, ~google.cloud.vision_v1p3beta1.types.ImportProductSetsInputConfig]): The input content for the list of requests. If a dict is provided, it must be of the same form as the protobuf message :class:`~google.cloud.vision_v1p3beta1.types.ImportProductSetsInputConfig` retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.cloud.vision_v1p3beta1.types._OperationFuture` instance. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. """ # Wrap the transport method to add retry and timeout logic. if "import_product_sets" not in self._inner_api_calls: self._inner_api_calls[ "import_product_sets" ] = google.api_core.gapic_v1.method.wrap_method( self.transport.import_product_sets, default_retry=self._method_configs["ImportProductSets"].retry, default_timeout=self._method_configs["ImportProductSets"].timeout, client_info=self._client_info, ) request = product_search_service_pb2.ImportProductSetsRequest( parent=parent, input_config=input_config ) if metadata is None: metadata = [] metadata = list(metadata) try: routing_header = [("parent", parent)] except AttributeError: pass else: routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata( routing_header ) metadata.append(routing_metadata) operation = self._inner_api_calls["import_product_sets"]( request, retry=retry, timeout=timeout, metadata=metadata ) return google.api_core.operation.from_gapic( operation, self.transport._operations_client, product_search_service_pb2.ImportProductSetsResponse, metadata_type=product_search_service_pb2.BatchOperationMetadata, )
Special helper to parse ``LogEntry`` protobuf into a dictionary. The ``proto_payload`` field in ``LogEntry`` is of type ``Any``. This can be problematic if the type URL in the payload isn't in the ``google.protobuf`` registry. To help with parsing unregistered types, this function will remove ``proto_payload`` before parsing. :type entry_pb: :class:`.log_entry_pb2.LogEntry` :param entry_pb: Log entry protobuf. :rtype: dict :returns: The parsed log entry. The ``protoPayload`` key may contain the raw ``Any`` protobuf from ``entry_pb.proto_payload`` if it could not be parsed. def _parse_log_entry(entry_pb): """Special helper to parse ``LogEntry`` protobuf into a dictionary. The ``proto_payload`` field in ``LogEntry`` is of type ``Any``. This can be problematic if the type URL in the payload isn't in the ``google.protobuf`` registry. To help with parsing unregistered types, this function will remove ``proto_payload`` before parsing. :type entry_pb: :class:`.log_entry_pb2.LogEntry` :param entry_pb: Log entry protobuf. :rtype: dict :returns: The parsed log entry. The ``protoPayload`` key may contain the raw ``Any`` protobuf from ``entry_pb.proto_payload`` if it could not be parsed. """ try: return MessageToDict(entry_pb) except TypeError: if entry_pb.HasField("proto_payload"): proto_payload = entry_pb.proto_payload entry_pb.ClearField("proto_payload") entry_mapping = MessageToDict(entry_pb) entry_mapping["protoPayload"] = proto_payload return entry_mapping else: raise