Skip to content

Conversation

@odesenfans
Copy link
Contributor

@odesenfans odesenfans commented Jan 12, 2023

The session classes now provide all the SDK functionalities.
The synchronous and asynchronous modules are removed in
favor of the UserSession and AuthenticatedUserSession classes.

UserSession is intended as a read-only session to access public
API endpoints, while AuthenticatedUserSession requires a chain
account and allows the user to post messages to the Aleph network.

Example usage:

from aleph_client import AuthenticatedUserSession

async def post_message():
  async with AuthenticatedUserSession(
    account=fixture_account, api_server=emitter_node
  ) as session:
    post_message, message_status = await session.create_post(
      post_content={"Hello": "World"},
    )

Both classes provide a synchronous context manager and an equivalent
sync class for non-async code.

Breaking changes:

  • Everything: the SDK API is entirely modified.

@odesenfans odesenfans force-pushed the od-user-session-class branch 3 times, most recently from 1adf3f3 to 0c14af5 Compare January 13, 2023 16:58
@odesenfans odesenfans changed the title Internal: user session class Feature: full API refactoring Jan 13, 2023
@odesenfans odesenfans force-pushed the od-user-session-class branch from 0c14af5 to e6fcd51 Compare January 13, 2023 19:04
hoh
hoh previously approved these changes Jan 25, 2023
@hoh hoh marked this pull request as ready for review January 25, 2023 14:10
Problem: we often pass 2-3 objects to each function that represent
information on how to connect to the Aleph network:

* the user account
* the API server URL
* the aiohttp session.

This can be simplified to improve the user experience and reduce
the complexity of the SDK.

Solution: introduce a new state class: `UserSession`. This object
is now passed as the first parameter of every public function.
It is initialized with the user account and the API server URL.
It is in charge of configuring and managing the aiohttp session object.

The typical usage of the library now looks like this:

```
account = load_my_account()
api_server = "https://aleph.cloud"  # example
async with UserSession(account=account, api_server=api_server) as session:
  message, status = await create_post(
    session=session,
    ...
  )
```

This enables the following improvements:

* Less clutter in function signatures: all public SDK functions now
  have 1 or 2 fewer arguments.
* The API server URL is now managed when initializing the aiohttp
  session inside the user session object. Implementations can
  simply specify the endpoint URL. Ex:
  `f"{api_server}/api/v0/messages.json` can now be expressed
  as `"/api/v0/messages.json"`.

Breaking changes:

* The signatures of all public functions of the SDK have been
  modified. The user must now initialize the user session object
  and pass it as parameter.
@hoh hoh force-pushed the od-user-session-class branch 3 times, most recently from 3fa7a48 to 1674024 Compare January 25, 2023 14:56
The session classes now provide all the SDK functionalities.
The `synchronous` and `asynchronous` modules are removed in
favor of the `UserSession` and `AuthenticatedUserSession` classes.

`UserSession` is intended as a read-only session to access public
API endpoints, while `AuthenticatedUserSession` requires a chain
account and allows the user to post messages to the Aleph network.

Example usage:

```
from aleph_client import AuthenticatedUserSession

async def post_message():
  async with AuthenticatedUserSession(
    account=fixture_account, api_server=emitter_node
  ) as session:
    post_message, message_status = await session.create_post(
      post_content={"Hello": "World"},
    )
```

Both classes provide a synchronous context manager and an equivalent
sync class for non-async code.

Breaking changes:
- Everything: the SDK API is entirely modified.
@hoh hoh force-pushed the od-user-session-class branch from 1674024 to fac3158 Compare January 25, 2023 15:07
@hoh hoh merged commit fc3fb8a into main Jan 25, 2023
@hoh hoh deleted the od-user-session-class branch January 25, 2023 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants