|
1 |
| -import asyncio |
2 |
| -import aiohttp |
3 |
| -import json |
4 |
| -import logging |
5 |
| -import os |
6 |
| - |
7 |
| - |
8 |
| -class JsonRpcError(Exception): |
9 |
| - pass |
| 1 | +from .account import Account |
10 | 2 |
|
11 | 3 |
|
12 | 4 | class Deltachat:
|
13 |
| - def __init__(self, process): |
14 |
| - self.process = process |
15 |
| - self.event_queue = asyncio.Queue() |
16 |
| - self.id = 0 |
17 |
| - self.reader_task = asyncio.create_task(self.reader_loop()) |
18 |
| - |
19 |
| - # Map from request ID to `asyncio.Future` returning the response. |
20 |
| - self.request_events = {} |
21 |
| - |
22 |
| - async def reader_loop(self): |
23 |
| - while True: |
24 |
| - line = await self.process.stdout.readline() |
25 |
| - response = json.loads(line) |
26 |
| - if "id" in response: |
27 |
| - fut = self.request_events.pop(response["id"]) |
28 |
| - fut.set_result(response) |
29 |
| - else: |
30 |
| - if response["method"] == "event": |
31 |
| - # An event notification. |
32 |
| - await self.event_queue.put(response["params"]["event"]) |
33 |
| - |
34 |
| - async def get_next_event(self): |
35 |
| - """Returns next event.""" |
36 |
| - return await self.event_queue.get() |
37 |
| - |
38 |
| - def __getattr__(self, attr): |
39 |
| - async def method(*args, **kwargs): |
40 |
| - self.id += 1 |
41 |
| - request_id = self.id |
42 |
| - |
43 |
| - params = args |
44 |
| - if kwargs: |
45 |
| - assert not args |
46 |
| - params = kwargs |
47 |
| - |
48 |
| - request = { |
49 |
| - "jsonrpc": "2.0", |
50 |
| - "method": attr, |
51 |
| - "params": params, |
52 |
| - "id": self.id, |
53 |
| - } |
54 |
| - data = (json.dumps(request) + "\n").encode() |
55 |
| - self.process.stdin.write(data) |
56 |
| - event = asyncio.Event() |
57 |
| - loop = asyncio.get_running_loop() |
58 |
| - fut = loop.create_future() |
59 |
| - self.request_events[request_id] = fut |
60 |
| - response = await fut |
61 |
| - if "error" in response: |
62 |
| - raise JsonRpcError(response["error"]) |
63 |
| - if "result" in response: |
64 |
| - return response["result"] |
| 5 | + """ |
| 6 | + Delta Chat account manager. |
| 7 | + This is the root of the object oriented API. |
| 8 | + """ |
65 | 9 |
|
66 |
| - return method |
| 10 | + def __init__(self, rpc): |
| 11 | + self.rpc = rpc |
67 | 12 |
|
| 13 | + async def add_account(self): |
| 14 | + account_id = await self.rpc.add_account() |
| 15 | + return Account(self.rpc, account_id) |
68 | 16 |
|
69 |
| -async def start_rpc_server(): |
70 |
| - proc = await asyncio.create_subprocess_exec( |
71 |
| - "deltachat-rpc-server", |
72 |
| - stdin=asyncio.subprocess.PIPE, |
73 |
| - stdout=asyncio.subprocess.PIPE, |
74 |
| - ) |
75 |
| - deltachat = Deltachat(proc) |
76 |
| - return deltachat |
| 17 | + async def get_all_accounts(self): |
| 18 | + account_ids = await self.rpc.get_all_account_ids() |
| 19 | + return [Account(rpc, account_id) for account_id in account_ids] |
77 | 20 |
|
| 21 | + async def start_io(self) -> None: |
| 22 | + await self.rpc.start_io_for_all_accounts() |
78 | 23 |
|
79 |
| -async def new_online_account(): |
80 |
| - url = os.getenv("DCC_NEW_TMP_EMAIL") |
81 |
| - async with aiohttp.ClientSession() as session: |
82 |
| - async with session.post(url) as response: |
83 |
| - return json.loads(await response.text()) |
| 24 | + async def stop_io(self) -> None: |
| 25 | + await self.rpc.stop_io_for_all_accounts() |
0 commit comments