Skip to content

Commit c2433bf

Browse files
docs: add api documents (#9)
* docs: add api documents * style: format python code (pep8) * docs: add api documents * docs: move use-guidance to docs.featureprobe.io Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
1 parent eff5a05 commit c2433bf

File tree

5 files changed

+89
-101
lines changed

5 files changed

+89
-101
lines changed

README.md

Lines changed: 2 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -15,98 +15,9 @@ This SDK is designed primarily for use in multi-user systems such as web servers
1515
Reading the short [Basic Terms](https://github.com/FeatureProbe/FeatureProbe/blob/main/BASIC_TERMS.md) will help to understand the code blow more easily. [中文](https://github.com/FeatureProbe/FeatureProbe/blob/main/BASIC_TERMS_CN.md)
1616

1717

18-
## Try Out Demo Code
19-
20-
We provide a runnable demo code for you to understand how FeatureProbe SDK is used.
21-
22-
1. Start FeatureProbe Service with docker composer. [How to](https://github.com/FeatureProbe/FeatureProbe#1-starting-featureprobe-service-with-docker-compose)
23-
24-
2. Download this repo and run the demo program:
25-
```bash
26-
git clone https://github.com/FeatureProbe/server-sdk-python.git
27-
cd server-sdk-python
28-
pip3 install -r requirements.txt
29-
python3 demo.py
30-
```
31-
32-
3. Find the Demo code in [example](https://github.com/FeatureProbe/server-sdk-python/blob/main/demo.py),
33-
do some change and run the program again.
34-
```bash
35-
python3 demo.py
36-
```
37-
38-
## Step-by-Step Guide
39-
40-
In this guide we explain how to use feature toggles in a Python application using FeatureProbe.
41-
42-
### Step 1. Install the Python SDK
43-
44-
First, install the FeatureProbe SDK as a dependency in your application.
45-
46-
#### pip
47-
48-
```bash
49-
pip3 install featureprobe-server-sdk-python
50-
```
51-
52-
> You may get the latest version of this repo (not released) via [TestPyPI](https://test.pypi.org/project/featureprobe-server-sdk-python/)
53-
54-
<!-- WIP
55-
#### conda
56-
57-
Will be supported later.
58-
59-
```bash
60-
conda install featureprobe-server-sdk-python
61-
```
62-
-->
63-
64-
65-
### Step 2. Create a FeatureProbe instance
66-
67-
After you install the SDK, import it, then create a single, shared instance of the FeatureProbe SDK.
68-
69-
```python
70-
import featureprobe as fp
71-
72-
config = fp.Config(remote_uri='http://127.0.0.1:4007', sync_mode='pooling', refresh_interval=3)
73-
client = fp.Client('server-8ed48815ef044428826787e9a238b9c6a479f98c', config)
74-
```
75-
76-
77-
### Step 3. Use the feature toggle
78-
79-
You can use sdk to check which variation a particular user will receive for a given feature flag.
80-
81-
```python
82-
import featureprobe as fp
83-
84-
config = fp.Config(remote_uri='http://127.0.0.1:4007', sync_mode='pooling', refresh_interval=3)
85-
client = fp.Client('server-8ed48815ef044428826787e9a238b9c6a479f98c', config)
86-
87-
if __name__ == '__main__':
88-
user = fp.User('user_unique_id', {
89-
'userId': '9876',
90-
'tel': '12345678998',
91-
})
92-
bool_eval = bool(client.value('bool_toggle_key', user, default=False))
93-
if bool_eval:
94-
... # application code to show the feature
95-
else:
96-
... # the code to run if the feature is off
97-
```
98-
99-
100-
## Testing
101-
102-
We have unified integration tests for all our SDKs. Integration test cases are added as submodules for each SDK repo. So be sure to pull submodules first to get the latest integration tests before running tests.
103-
104-
```shell
105-
git pull --recurse-submodules
106-
pip3 install -r requirements-dev.txt
107-
pytest featureprobe
108-
```
18+
## How to User This SDK
10919

20+
See [SDK Doc](https://docs.featureprobe.io/sdk/Server-Side%20SDKs/python-sdk) for detail. [中文](https://docs.featureprobe.io/zh-CN/sdk/Server-Side%20SDKs/python-sdk)
11021

11122
## Contributing
11223

@@ -117,7 +28,6 @@ community for contributing bugfixes and improvements.
11728
Please read [CONTRIBUTING](https://github.com/FeatureProbe/featureprobe/blob/master/CONTRIBUTING.md)
11829
for details on our code of conduct, and the process for taking part in improving FeatureProbe.
11930

120-
12131
## License
12232

12333
This project is licensed under the Apache 2.0 License - see the [LICENSE](LICENSE) file for details.

featureprobe/client.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,23 @@
2525

2626

2727
class Client:
28+
"""A client for the FeatureProbe API. Client instances are thread-safe.
29+
30+
Applications should instantiate a single :obj:`~featureprobe.Client` for the lifetime of their application.
31+
"""
32+
2833
__logger = logging.getLogger('FeatureProbe')
2934

30-
def __init__(self, sdk_key: str, config: Config = Config()):
31-
if empty_str(sdk_key):
35+
def __init__(self, server_sdk_key: str, config: Config = Config()):
36+
"""Creates a new client instance that connects to FeatureProbe with the default configuration.
37+
38+
:param server_sdk_key: Server SDK Key for your FeatureProbe environment.
39+
:param config: (optional) The configuration control FeatureProbe client behavior.
40+
Leaving this argument unfilled will use the default configuration.
41+
"""
42+
if empty_str(server_sdk_key):
3243
raise ValueError('sdk key must not be blank')
33-
context = Context(sdk_key, config)
44+
context = Context(server_sdk_key, config)
3445
self._event_processor = config.event_processor_creator(context)
3546
self._data_repo = config.data_repository_creator(context)
3647
self._synchronizer = config.synchronizer_creator(
@@ -53,6 +64,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
5364
self.close()
5465

5566
def flush(self):
67+
"""Manually push events"""
5668
self._event_processor.flush()
5769

5870
def close(self):
@@ -63,6 +75,13 @@ def close(self):
6375
self._data_repo.close()
6476

6577
def value(self, toggle_key: str, user: User, default) -> Any:
78+
"""Gets the evaluated value of a toggle.
79+
80+
:param toggle_key: The key of toggle in this environment.
81+
:param user: :obj:`~featureprobe.User` to be evaluated.
82+
:param default: The default value to be returned.
83+
:returns: Dependents on the toggle's type.
84+
"""
6685
toggle = self._data_repo.get_toggle(toggle_key)
6786
segments = self._data_repo.get_all_segment()
6887
if not toggle:
@@ -79,6 +98,15 @@ def value(self, toggle_key: str, user: User, default) -> Any:
7998
return eval_result.value
8099

81100
def value_detail(self, toggle_key: str, user: User, default) -> Detail:
101+
"""Gets the detailed evaluated results of a toggle.
102+
103+
:param toggle_key: The key of toggle in this environment.
104+
:param user: :obj:`~featureprobe.User` to be evaluated.
105+
:param default: The default value to be returned.
106+
:returns: :obj:`~featureprobe.Detail` contains the `value`, `rule_index`, `version`, and `reason`
107+
of this evaluation.
108+
"""
109+
82110
if not self._data_repo.initialized:
83111
return Detail(
84112
value=default,

featureprobe/user.py

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,46 @@
1616

1717

1818
class User:
19+
"""A collection of attributes that can affect toggle evaluation.
20+
21+
Usually corresponding to a user of your application.
22+
"""
23+
1924
def __init__(self, key: str, attrs: Dict[str, str] = None):
25+
"""Creates a new FeatureProbe User.
26+
27+
:param key: User unique ID for percentage rollout.
28+
:param attrs: (optional) The initialize attribute for a user.
29+
"""
2030
self._key = key
2131
self._attrs = attrs or {}
2232

2333
def __setitem__(self, key: str, value: str):
34+
"""Alias for :func:`~featureprobe.User.with_attr`.
35+
36+
Usage::
37+
38+
>>> import featureprobe as fp
39+
>>> user = fp.User('unique id')
40+
>>> user.with_attr('key1', 'value1')
41+
>>> user['key2'] = 'value2'
42+
"""
2443
self._attrs[key] = value
2544

26-
def __getitem__(self, item: str):
45+
def __getitem__(self, attr: str):
46+
"""Gets the value of specified attribute.
47+
48+
:param attr: Attribute name / key.
49+
:returns: str or None
50+
51+
Usage::
52+
53+
>>> import featureprobe as fp
54+
>>> user = fp.User('unique id', { 'key': 'value' })
55+
>>> user['key'] # 'value'
56+
"""
2757
try:
28-
return self._attrs[item]
58+
return self._attrs[attr]
2959
except KeyError:
3060
return None
3161

@@ -40,18 +70,38 @@ def to_dict(self) -> dict:
4070

4171
@property
4272
def key(self) -> str:
73+
"""Gets FeatureProbe User unique identifier"""
4374
return self._key
4475

4576
@property
4677
def attrs(self) -> Dict[str, str]:
78+
"""Gets all attributes of a FeatureProbe User"""
4779
return self._attrs
4880

4981
@attrs.setter
5082
def attrs(self, attrs: Dict[str, str]):
83+
"""Sets (replace the original attributes) multiple attributes to a FeatureProbe User"""
5184
self._attrs = attrs
5285

53-
def with_attr(self, key: str, value: str):
86+
def with_attr(self, key: str, value: str) -> "User":
87+
"""Adds an attribute to the user.
88+
89+
:param key: Attribute key / name.
90+
:param value: Attribute value.
91+
:returns: User
92+
93+
Usage::
94+
95+
>>> import featureprobe as fp
96+
>>> user = fp.User('unique id').with_attr('key1', 'value1').with_attr('key2', 'value2')
97+
"""
5498
self._attrs[key] = value
99+
return self
100+
101+
def has_attr(self, attr: str) -> bool:
102+
"""Checks if an attribute exists.
55103
56-
def has_attr(self, attr: str):
104+
:param attr: Attribute name / key.
105+
:returns: bool
106+
"""
57107
return attr in self._attrs

tests/file_serialization_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_local_mode_synchronizer():
2020
sync_mode='file',
2121
location='tests/resources/datasource/repo.json')
2222
feature_probe = fp.Client(
23-
sdk_key='server-61db54ecea79824cae3ac38d73f1961d698d0477',
23+
server_sdk_key='server-61db54ecea79824cae3ac38d73f1961d698d0477',
2424
config=config)
2525
repo = feature_probe._data_repo
2626
assert len(repo.get_all_toggle()) > 0

tests/polling_synchronizer_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717

1818
def test_local_mode_synchronizer():
1919
feature_probe = fp.Client(
20-
sdk_key='server-61db54ecea79824cae3ac38d73f1961d698d0477')
20+
server_sdk_key='server-61db54ecea79824cae3ac38d73f1961d698d0477')
2121
repo = feature_probe._data_repo
2222
# TODO

0 commit comments

Comments
 (0)