Skip to content

Commit

Permalink
Convert Config classes to public API [API-1280] (#521)
Browse files Browse the repository at this point in the history
* Convert `Config` classes to public API

These were converted into private classes during the 4.0 migration,
as we were only supporting keyword arguments for the client configuration.

However, that led to a poor user experience, as we had to use `**kwargs`,
instead of listing all the configuration elements in the client constructor.
(IDEs become painfully slow once we list all keyword arguments, as there
are too many of them)

The solution to this problem is to make the Config classes public API
and make the client able to use it directly.

```python
config = Config()
config.cluster_name = "dev2"

client = HazelcastClient(config)
```

We provide full type hints for config elements.

* bump the client version used in tests

* address review comments

* shutdown clients in teardown method
  • Loading branch information
mdumandag committed Dec 30, 2022
1 parent ab81caf commit a64b4fc
Show file tree
Hide file tree
Showing 27 changed files with 1,936 additions and 1,212 deletions.
1 change: 0 additions & 1 deletion docs/api/modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ API Documentation

aggregator
cluster
config
core
cp
errors
Expand Down
8 changes: 6 additions & 2 deletions docs/api/config.rst → docs/config.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
Config
======
Configuration API Documentation
===============================

.. py:currentmodule:: hazelcast.config
.. autoclass:: Config
.. autoclass:: NearCacheConfig
.. autoclass:: FlakeIdGeneratorConfig
.. autoclass:: ReliableTopicConfig
.. autoclass:: IntType
.. autoclass:: EvictionPolicy
.. autoclass:: InMemoryFormat
Expand Down
42 changes: 35 additions & 7 deletions docs/configuration_overview.rst
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
Configuration Overview
======================

For configuration of the Hazelcast Python client, just pass the keyword
arguments to the client to configure the desired aspects. An example is
shown below.
The client can be configured either by keyword arguments or by a configuration
object.

Keyword Arguments Configuration
-------------------------------

It is possible to pass keyword arguments directly to the client's constructor
to configure desired aspects of the client.

The keyword argument names must be valid property names of the
:class:`hazelcast.config.Config` class with valid values.

.. code:: python
client = hazelcast.HazelcastClient(
cluster_members=["127.0.0.1:5701"]
from hazelcast import HazelcastClient
client = HazelcastClient(
cluster_name="a-cluster",
cluster_members=["127.0.0.1:5701"],
)
See the API documentation of :class:`hazelcast.client.HazelcastClient`
for details.
Using a Configuration Object
----------------------------

Alternatively, you can create a configuration object, and pass it to the client
as its only argument.

This way might provide better user experience as it provides hints for the
configuration option names and their types.

.. code:: python
from hazelcast import HazelcastClient
from hazelcast.config import Config
config = Config()
config.cluster_name = "a-cluster"
config.cluster_members = ["127.0.0.1:5701"]
client = HazelcastClient(config)
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ Features
:hidden:

client
config
api/modules
getting_started
features
Expand Down
17 changes: 17 additions & 0 deletions examples/configuration/kwargs_configuration_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from hazelcast import HazelcastClient

client = HazelcastClient(
cluster_name="a-cluster",
cluster_members=["10.212.1.132:5701"],
ssl_enabled=True,
near_caches={
"a-map": {
"time_to_live": 120,
"max_idle": 60,
}
},
)

# Do something with the client

client.shutdown()
21 changes: 21 additions & 0 deletions examples/configuration/object_configuration_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from hazelcast import HazelcastClient
from hazelcast.config import Config, NearCacheConfig

config = Config()
config.cluster_name = "a-cluster"
config.cluster_members = ["10.212.1.132:5701"]
config.ssl_enabled = True

near_cache_config = NearCacheConfig()
near_cache_config.time_to_live = 120
near_cache_config.max_idle = 60

config.near_caches = {
"a-map": near_cache_config,
}

client = HazelcastClient(config)

# Do something with the client

client.shutdown()

0 comments on commit a64b4fc

Please sign in to comment.