Parse a hello response from the server. .. versionadded:: 3.12
| 63 | |
| 64 | |
| 65 | class Hello(Generic[_DocumentType]): |
| 66 | """Parse a hello response from the server. |
| 67 | |
| 68 | .. versionadded:: 3.12 |
| 69 | """ |
| 70 | |
| 71 | __slots__ = ("_doc", "_server_type", "_is_writable", "_is_readable", "_awaitable") |
| 72 | |
| 73 | def __init__(self, doc: _DocumentType, awaitable: bool = False) -> None: |
| 74 | self._server_type = _get_server_type(doc) |
| 75 | self._doc: _DocumentType = doc |
| 76 | self._is_writable = self._server_type in ( |
| 77 | SERVER_TYPE.RSPrimary, |
| 78 | SERVER_TYPE.Standalone, |
| 79 | SERVER_TYPE.Mongos, |
| 80 | SERVER_TYPE.LoadBalancer, |
| 81 | ) |
| 82 | |
| 83 | self._is_readable = self.server_type == SERVER_TYPE.RSSecondary or self._is_writable |
| 84 | self._awaitable = awaitable |
| 85 | |
| 86 | @property |
| 87 | def document(self) -> _DocumentType: |
| 88 | """The complete hello command response document. |
| 89 | |
| 90 | .. versionadded:: 3.4 |
| 91 | """ |
| 92 | return copy.copy(self._doc) |
| 93 | |
| 94 | @property |
| 95 | def server_type(self) -> int: |
| 96 | return self._server_type |
| 97 | |
| 98 | @property |
| 99 | def all_hosts(self) -> set[tuple[str, int]]: |
| 100 | """List of hosts, passives, and arbiters known to this server.""" |
| 101 | return set( |
| 102 | map( |
| 103 | common.clean_node, |
| 104 | itertools.chain( |
| 105 | self._doc.get("hosts", []), |
| 106 | self._doc.get("passives", []), |
| 107 | self._doc.get("arbiters", []), |
| 108 | ), |
| 109 | ) |
| 110 | ) |
| 111 | |
| 112 | @property |
| 113 | def tags(self) -> Mapping[str, Any]: |
| 114 | """Replica set member tags or empty dict.""" |
| 115 | return self._doc.get("tags", {}) |
| 116 | |
| 117 | @property |
| 118 | def primary(self) -> Optional[tuple[str, int]]: |
| 119 | """This server's opinion about who the primary is, or None.""" |
| 120 | if self._doc.get("primary"): |
| 121 | return common.partition_node(self._doc["primary"]) |
| 122 | else: |
no outgoing calls