Skip to content

Commit

Permalink
Change Client factory functions to free functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rkhwaja committed Oct 8, 2024
1 parent 0753a98 commit 9a6cfe3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ Python wrapper for "Remember the Milk" [API](https://www.rememberthemilk.com/ser

# Usage of client
```python
from rtmilk.client import Client
from rtmilk.client import CreateClient, CreateClientAsync
from rtmmilk.models import APIError

# These are the equivalent objects, created differently
client = Client.Create(API_KEY, SHARED_SECRET, TOKEN)
client2 = await Client.CreateAsync(API_KEY, SHARED_SECRET, TOKEN)
client = CreateClient(API_KEY, SHARED_SECRET, TOKEN)
client2 = await CreateClientAsync(API_KEY, SHARED_SECRET, TOKEN)

try:
task = client.Add(name='name 1')
Expand Down
30 changes: 14 additions & 16 deletions src/rtmilk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,30 +79,28 @@ def _CreateListOfTasks(client, listResponse):
tasks.extend([_CreateFromTaskSeries(client, listId=list_.id, taskSeries=ts) for ts in list_.taskseries])
return tasks

class Client:
def CreateClient(clientId: str, clientSecret: str, token: str) -> _Client:
"""Create RTM client object synchronously"""
client = _Client(clientId, clientSecret, token)
client._CreateTimeline()
return client

async def CreateClientAsync(clientId: str, clientSecret: str, token: str) -> _Client:
"""Create RTM client object asynchronously"""
client = _Client(clientId, clientSecret, token)
await client._CreateTimelineAsync()
return client

class _Client:
"""Wraps the timeline and adds convenience functions to add and query tasks"""

@classmethod
def Create(cls, clientId: str, clientSecret: str, token: str) -> Client:
client = Client(clientId, clientSecret, token)
client._CreateTimeline()
return client

@classmethod
async def CreateAsync(cls, clientId: str, clientSecret: str, token: str) -> Client:
client = Client(clientId, clientSecret, token)
await client._CreateTimelineAsync()
return client

# TODO - pass timeline in constructor to at least prevent people who accidentally call this from making an invalid object?
# else change Client to _Client and make the factory functions free
def __init__(self, clientId: str, clientSecret: str, token: str):
self.api = API(clientId, clientSecret, token)
self.apiAsync = APIAsync(clientId, clientSecret, token)
self.timeline = None

def __repr__(self):
return 'Client()'
return '_Client()'

def _CreateTimeline(self):
self.timeline = _RaiseIfError(self.api.TimelinesCreate().timeline)
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from rtmilk.api_sync import API
from rtmilk.api_async import APIAsync
from rtmilk.client import Client
from rtmilk.client import CreateClient

try:
from dotenv import load_dotenv
Expand Down Expand Up @@ -108,7 +108,7 @@ def taskCreator(client):
@fixture
def client():
apiKey, sharedSecret, token = _GetConfig()
return Client.Create(apiKey, sharedSecret, token)
return CreateClient(apiKey, sharedSecret, token)

@fixture
def mockClient():
Expand Down

0 comments on commit 9a6cfe3

Please sign in to comment.