Skip to content

Commit a6002d3

Browse files
committed
PYTHON-2453 Add documentation
1 parent d58596c commit a6002d3

File tree

5 files changed

+94
-10
lines changed

5 files changed

+94
-10
lines changed

doc/api/pymongo/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Sub-modules:
5454
read_preferences
5555
results
5656
son_manipulator
57+
server_api
5758
uri_parser
5859
write_concern
5960
event_loggers

doc/api/pymongo/server_api.rst

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
:mod:`server_api` -- Support for MongoDB Versioned API
2+
======================================================
3+
4+
.. automodule:: pymongo.server_api
5+
:synopsis: Support for MongoDB Versioned API
6+
7+
.. autoclass:: pymongo.server_api.ServerApi
8+
:members:
9+
10+
.. autoclass:: pymongo.server_api.ServerApiVersion
11+
:members:

doc/changelog.rst

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ Breaking Changes in 4.0
99

1010
- Removed :mod:`~pymongo.thread_util`.
1111

12+
Notable improvements
13+
....................
14+
15+
- Support for MongoDB Versioned API, see :class:`~pymongo.server_api.ServerApi`.
16+
1217
Issues Resolved
1318
...............
1419

pymongo/mongo_client.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,8 @@ def __init__(
503503
504504
- `server_api`: A
505505
:class:`~pymongo.server_api.ServerApi` which configures this
506-
client to use Versioned API.
506+
client to use Versioned API. See :ref:`versioned-api-ref` for
507+
details.
507508
508509
.. mongodoc:: connections
509510

pymongo/server_api.py

+75-9
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,42 @@
1212
# implied. See the License for the specific language governing
1313
# permissions and limitations under the License.
1414

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+
"""
1647

1748

1849
class ServerApiVersion:
19-
"""An enum that defines values for ServerApi `version`.
50+
"""An enum that defines values for :attr:`ServerApi.version`.
2051
2152
.. versionadded:: 3.12
2253
"""
@@ -26,11 +57,21 @@ class ServerApiVersion:
2657

2758

2859
class ServerApi(object):
29-
"""MongoDB Versioned API.
30-
31-
.. versionadded:: 3.12
32-
"""
60+
"""MongoDB Versioned API."""
3361
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+
"""
3475
if version != ServerApiVersion.V1:
3576
raise ValueError("Unknown ServerApi version: %s" % (version,))
3677
if strict is not None and not isinstance(strict, bool):
@@ -42,9 +83,34 @@ def __init__(self, version, strict=None, deprecation_errors=None):
4283
raise TypeError(
4384
"Wrong type for ServerApi deprecation_errors, value must be "
4485
"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
48114

49115

50116
def _add_to_command(cmd, server_api):

0 commit comments

Comments
 (0)