Skip to content

Commit cba1bd2

Browse files
authored
feat: Simplify importing and initializing App with app name
2 parents 9659caa + 2c28b90 commit cba1bd2

File tree

4 files changed

+34
-11
lines changed

4 files changed

+34
-11
lines changed

README.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,36 @@ pip install .
2323
To create an `audius` SDK instance, do:
2424

2525
```python
26-
from audius.sdk import Audius
26+
from audius import Audius
2727

2828
audius = Audius()
2929
```
3030

3131
It is recommended that you set a custom app name (the default is `audius-py`).
32-
One way to do this is via an environment variable:
32+
33+
```python
34+
audius = Audius("My_Audius_App")
35+
```
36+
37+
You can also use an environment:
3338

3439
```shell
3540
export AUDIUS_APP_NAME="My_Audius_App"
3641
```
3742

38-
Then, when you create an Audius SDK object, it will automatically use this value instead.
43+
And when you initialize without any arguments like `Audius()`, it will use the environment variable.
3944

40-
You can also specify an app name (and other configuration) when creating the SDK, like:
45+
You can also specify a `Config` option where you can set more config than `app_name`:
4146

4247
```python
4348
from audius.config import Config
4449
from audius.sdk import Audius
4550

46-
config = Config(app_name="my_app")
47-
sdk = Audius(config=config)
51+
config = Config(app_name="my_app", host="https://audius.example.com")
52+
sdk = Audius(config)
4853
```
4954

50-
Another example config value is the host, e.g.:
55+
The Audius host name is also configurable as an environment variable:
5156

5257
```shell
5358
export AUDIUS_HOST_NAME="https://audius.example.com"

audius/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .sdk import Audius
2+
3+
__all__ = ["Audius"]

audius/sdk.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from functools import cached_property
2-
from typing import Optional
2+
from typing import Optional, Union
33

44
from audius.client_factory import ClientFactory, get_hosts
55
from audius.config import Config
@@ -11,11 +11,19 @@
1111

1212

1313
class Audius:
14-
def __init__(self, config: Optional[Config] = None):
15-
self.config = config or Config.from_env()
14+
def __init__(self, __config_or_name: Optional[Union[Config, str]] = None):
15+
self.config = (
16+
Config(app_name=__config_or_name)
17+
if isinstance(__config_or_name, str)
18+
else __config_or_name or Config.from_env()
19+
)
1620
self.factory = ClientFactory(self.config.app_name)
1721
self.player = Player(self)
1822

23+
@property
24+
def app_name(self) -> str:
25+
return self.config.app_name
26+
1927
@cached_property
2028
def client(self):
2129
"""

tests/test_sdk.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
from audius.sdk import Audius
1+
from audius import Audius
22
from audius.types import PlayerType
33

44

55
class TestAudius:
66
def test_app_name_form_env(self, app_name_from_env):
77
sdk = Audius()
8+
assert sdk.app_name == app_name_from_env
89
assert sdk.config.app_name == app_name_from_env
910

11+
def test_app_name_from_argument(self):
12+
name = "TestAppNameFromArgument"
13+
sdk = Audius(name)
14+
assert sdk.app_name == name
15+
assert sdk.config.app_name == name
16+
1017
def test_host_from_env(self, host_from_env):
1118
sdk = Audius()
1219
assert sdk.config.host == host_from_env

0 commit comments

Comments
 (0)