Skip to content

Commit

Permalink
refactor: improve docs for client section
Browse files Browse the repository at this point in the history
  • Loading branch information
itz-Amethyst committed Jul 25, 2024
1 parent ae25062 commit c4a9f59
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ logging.basicConfig(level=logging.DEBUG)

This example demonstrates how to create an IMAP client using the `IMAPClient` class.

The `IMAPClient` class can also be used without a context manager; simply call `connect()` to establish the connection and `disconnect()` to close it

```python
from sage_imap.services import IMAPClient

Expand Down
30 changes: 28 additions & 2 deletions docs/source/getting_started/examples/example1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ Example 1: Creating an IMAP Client

This example demonstrates how to create an IMAP client using the `IMAPClient` class.

The IMAPClient class can be used both with and without a context manager.

# **With Context Manager**

.. code-block:: python
from sage_imap.services.client import IMAPClient
Expand All @@ -15,6 +19,24 @@ This example demonstrates how to create an IMAP client using the `IMAPClient` cl
status, messages = client.select("INBOX")
print(f"Selected INBOX with status: {status}")
# **Without Context Manager**

.. code-block:: python
from sage_imap.services.client import IMAPClient
# Initialize and use without context manager
client = IMAPClient('imap.example.com', 'username', 'password')
try:
client.connect()
capabilities = client.connection.capability()
print(f"Server capabilities: {capabilities}")
status, messages = client.connection.select("INBOX")
print(f"Selected INBOX with status: {status}")
finally:
client.disconnect()
Explanation
-----------

Expand All @@ -25,11 +47,15 @@ This example illustrates a low-level approach to working with IMAP. If you want
- When the `with` block is entered, the connection to the IMAP server is established, and the user is authenticated.
- When the `with` block is exited, the connection is automatically closed, ensuring that resources are cleaned up properly.

2. **Why Use IMAPClient**:
2. **IMAPClient Without Context Manager**:
- You can also use the `IMAPClient` class without a context manager. In this case, you need to manually call `connect()` to establish the connection and `disconnect()` to close it.
- This approach provides explicit control over when the connection is opened and closed but requires careful handling to ensure resources are properly released.

3. **Why Use IMAPClient**:
- The `IMAPClient` exists to simplify the management of IMAP connections. By using it as a context manager, you don't have to worry about manually opening and closing the connection. This reduces the risk of resource leaks and makes your code cleaner and more maintainable.
- Within the context manager, you have access to the `imaplib` capabilities directly through the `client` object. This allows you to perform various IMAP operations seamlessly.

3. **Capabilities and Select Methods**:
4. **Capabilities and Select Methods**:
- The `.capability()` method is called to retrieve the server's capabilities, providing information about what commands and features the server supports.
- The `.select("INBOX")` method is used to select the "INBOX" mailbox for further operations. It returns the status of the selection and the number of messages in the mailbox.

Expand Down

0 comments on commit c4a9f59

Please sign in to comment.