12
12
# implied. See the License for the specific language governing
13
13
# permissions and limitations under the License.
14
14
15
- """Support for MongoDB Versioned API."""
15
+ """Support for MongoDB Versioned API.
16
+
17
+ .. _versioned-api-ref:
18
+
19
+ MongoDB Versioned API
20
+ =====================
21
+
22
+ To configure MongoDB Versioned API, pass the ``server_api`` keyword option to
23
+ :class:`~pymongo.mongo_client.MongoClient`::
24
+
25
+ from pymongo.mongo_client import MongoClient
26
+ from pymongo.server_api import ServerApi
27
+
28
+ client = MongoClient(server_api=ServerApi('1'))
29
+
30
+ Note that Versioned API requires MongoDB >=5.0.
31
+
32
+ Strict Mode
33
+ ```````````
34
+
35
+ When ``strict`` mode is configured, commands that are not supported in the
36
+ given :attr:`ServerApi.version` will fail. For example::
37
+
38
+ >>> client = MongoClient(server_api=ServerApi('1', strict=True))
39
+ >>> client.test.command('count', 'test')
40
+ Traceback (most recent call last):
41
+ ...
42
+ pymongo.errors.OperationFailure: Provided apiStrict:true, but the command count is not in API Version 1, full error: {'ok': 0.0, 'errmsg': 'Provided apiStrict:true, but the command count is not in API Version 1', 'code': 323, 'codeName': 'APIStrictError'
43
+
44
+ Classes
45
+ =======
46
+ """
16
47
17
48
18
49
class ServerApiVersion :
19
- """An enum that defines values for ServerApi ` version`.
50
+ """An enum that defines values for :attr:`ServerApi. version`.
20
51
21
52
.. versionadded:: 3.12
22
53
"""
@@ -26,11 +57,21 @@ class ServerApiVersion:
26
57
27
58
28
59
class ServerApi (object ):
29
- """MongoDB Versioned API.
30
-
31
- .. versionadded:: 3.12
32
- """
60
+ """MongoDB Versioned API."""
33
61
def __init__ (self , version , strict = None , deprecation_errors = None ):
62
+ """Options to configure MongoDB Versioned API.
63
+
64
+ :Parameters:
65
+ - `version`: The API version string. Must be one of the values in
66
+ :class:`ServerApiVersion`.
67
+ - `strict` (optional): Set to ``True`` to enable API strict mode.
68
+ Defaults to ``None`` which means "use the server's default".
69
+ - `deprecation_errors` (optional): Set to ``True`` to enable
70
+ deprecation errors. Defaults to ``None`` which means "use the
71
+ server's default".
72
+
73
+ .. versionadded:: 3.12
74
+ """
34
75
if version != ServerApiVersion .V1 :
35
76
raise ValueError ("Unknown ServerApi version: %s" % (version ,))
36
77
if strict is not None and not isinstance (strict , bool ):
@@ -42,9 +83,34 @@ def __init__(self, version, strict=None, deprecation_errors=None):
42
83
raise TypeError (
43
84
"Wrong type for ServerApi deprecation_errors, value must be "
44
85
"an instance of bool, not %s" % (type (deprecation_errors ),))
45
- self .version = version
46
- self .strict = strict
47
- self .deprecation_errors = deprecation_errors
86
+ self ._version = version
87
+ self ._strict = strict
88
+ self ._deprecation_errors = deprecation_errors
89
+
90
+ @property
91
+ def version (self ):
92
+ """The API version setting.
93
+
94
+ This value is sent to the server in the "apiVersion" field.
95
+ """
96
+ return self ._version
97
+
98
+ @property
99
+ def strict (self ):
100
+ """The API strict mode setting.
101
+
102
+ When set, this value is sent to the server in the "apiStrict" field.
103
+ """
104
+ return self ._strict
105
+
106
+ @property
107
+ def deprecation_errors (self ):
108
+ """The API deprecation errors setting.
109
+
110
+ When set, this value is sent to the server in the
111
+ "apiDeprecationErrors" field.
112
+ """
113
+ return self ._deprecation_errors
48
114
49
115
50
116
def _add_to_command (cmd , server_api ):
0 commit comments