Skip to content
Snippets Groups Projects
Commit 6ae1979e authored by Jakub Ziebinski's avatar Jakub Ziebinski
Browse files

Implementation of the beekeepy service to make it easier to use beekeepy

parent de324540
No related branches found
No related tags found
1 merge request!78Further improvements by removing the helpy
Pipeline #118506 passed
from __future__ import annotations
from .asynchronous import BeekeepyService as AsyncBeekeepyService
from .asynchronous import create_beekeeper_service as create_async_beekeeper_service
from .synchronous import BeekeepyService, create_beekeepy_service
__all__ = [
"AsyncBeekeepyService",
"create_async_beekeeper_service",
"BeekeepyService",
"create_beekeepy_service",
]
from __future__ import annotations
from contextlib import asynccontextmanager
from dataclasses import dataclass
from typing import AsyncGenerator
from beekeepy import AsyncBeekeeper, AsyncSession, AsyncUnlockedWallet
@dataclass
class BeekeepyService:
wallet: AsyncUnlockedWallet
session: AsyncSession
beekeeper: AsyncBeekeeper
@asynccontextmanager
async def create_beekeeper_service(*, wallet_name: str, password: str) -> AsyncGenerator[BeekeepyService, None]:
"""
Create a beekeepy service with a wallet, session and beekeeper instance.
This context manager handle the creation of a wallet and session and running beekeeper instance.
If you already have a wallet with the same name, it will unlock it.
Args:
wallet_name (str): The wallet to create/unlock name.
password (str): The wallet to create/unlock password.
"""
async with await AsyncBeekeeper.factory() as beekeeper, await beekeeper.create_session() as session:
wallet = (
await session.create_wallet(name=wallet_name, password=password)
if wallet_name not in [w.name for w in await session.wallets_created]
else await (await session.open_wallet(name=wallet_name)).unlock(password=password)
)
yield BeekeepyService(wallet=wallet, session=session, beekeeper=beekeeper)
from __future__ import annotations
from contextlib import contextmanager
from dataclasses import dataclass
from typing import Generator
from beekeepy import Beekeeper, Session, UnlockedWallet
@dataclass
class BeekeepyService:
wallet: UnlockedWallet
session: Session
beekeeper: Beekeeper
@contextmanager
def create_beekeepy_service(*, wallet_name: str, password: str) -> Generator[BeekeepyService, None, None]:
"""
Create a beekeepy service with a wallet and session.
This context manager handle the creation of a wallet and session and running beekeeper instance.
If you already have a wallet with the same name, it will unlock it.
Args:
wallet_name (str): The wallet to create/unlock name.
password (str): The wallet to create/unlock password.
"""
with Beekeeper.factory() as beekeeper, beekeeper.create_session() as session:
wallet = (
session.create_wallet(name=wallet_name, password=password)
if wallet_name not in [w.name for w in session.wallets_created]
else (session.open_wallet(name=wallet_name)).unlock(password=password)
)
yield BeekeepyService(wallet=wallet, session=session, beekeeper=beekeeper)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment