-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
156 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from typing import Optional | ||
|
||
|
||
class Account: | ||
def __init__(self, rpc, id): | ||
self.id = id | ||
self.rpc = rpc | ||
|
||
async def remove(self) -> None: | ||
await self.rpc.remove_account(self.id) | ||
|
||
async def start_io(self) -> None: | ||
await self.rpc.start_io(self.id) | ||
|
||
async def stop_io(self) -> None: | ||
await self.rpc.stop_io(self.id) | ||
|
||
async def get_info(self): | ||
await self.rpc.get_info(self.id) | ||
|
||
async def get_file_size(self): | ||
await self.rpc.get_account_file_size(self.id) | ||
|
||
async def is_configured(self) -> bool: | ||
await self.rpc.is_configured(self.id) | ||
|
||
async def set_config(key: str, value: Optional[str]): | ||
await self.rpc.set_config(self.id, key, value) | ||
|
||
async def get_config(key: str) -> Optional[str]: | ||
await self.rpc.get_config(self.id, key) |
92 changes: 17 additions & 75 deletions
92
deltachat-rpc-client/src/deltachat_rpc_client/deltachat.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,25 @@ | ||
import asyncio | ||
import aiohttp | ||
import json | ||
import logging | ||
import os | ||
|
||
|
||
class JsonRpcError(Exception): | ||
pass | ||
from .account import Account | ||
|
||
|
||
class Deltachat: | ||
def __init__(self, process): | ||
self.process = process | ||
self.event_queue = asyncio.Queue() | ||
self.id = 0 | ||
self.reader_task = asyncio.create_task(self.reader_loop()) | ||
|
||
# Map from request ID to `asyncio.Future` returning the response. | ||
self.request_events = {} | ||
|
||
async def reader_loop(self): | ||
while True: | ||
line = await self.process.stdout.readline() | ||
response = json.loads(line) | ||
if "id" in response: | ||
fut = self.request_events.pop(response["id"]) | ||
fut.set_result(response) | ||
else: | ||
if response["method"] == "event": | ||
# An event notification. | ||
await self.event_queue.put(response["params"]["event"]) | ||
|
||
async def get_next_event(self): | ||
"""Returns next event.""" | ||
return await self.event_queue.get() | ||
|
||
def __getattr__(self, attr): | ||
async def method(*args, **kwargs): | ||
self.id += 1 | ||
request_id = self.id | ||
|
||
params = args | ||
if kwargs: | ||
assert not args | ||
params = kwargs | ||
|
||
request = { | ||
"jsonrpc": "2.0", | ||
"method": attr, | ||
"params": params, | ||
"id": self.id, | ||
} | ||
data = (json.dumps(request) + "\n").encode() | ||
self.process.stdin.write(data) | ||
event = asyncio.Event() | ||
loop = asyncio.get_running_loop() | ||
fut = loop.create_future() | ||
self.request_events[request_id] = fut | ||
response = await fut | ||
if "error" in response: | ||
raise JsonRpcError(response["error"]) | ||
if "result" in response: | ||
return response["result"] | ||
""" | ||
Delta Chat account manager. | ||
This is the root of the object oriented API. | ||
""" | ||
|
||
return method | ||
def __init__(self, rpc): | ||
self.rpc = rpc | ||
|
||
async def add_account(self): | ||
account_id = await self.rpc.add_account() | ||
return Account(self.rpc, account_id) | ||
|
||
async def start_rpc_server(): | ||
proc = await asyncio.create_subprocess_exec( | ||
"deltachat-rpc-server", | ||
stdin=asyncio.subprocess.PIPE, | ||
stdout=asyncio.subprocess.PIPE, | ||
) | ||
deltachat = Deltachat(proc) | ||
return deltachat | ||
async def get_all_accounts(self): | ||
account_ids = await self.rpc.get_all_account_ids() | ||
return [Account(rpc, account_id) for account_id in account_ids] | ||
|
||
async def start_io(self) -> None: | ||
await self.rpc.start_io_for_all_accounts() | ||
|
||
async def new_online_account(): | ||
url = os.getenv("DCC_NEW_TMP_EMAIL") | ||
async with aiohttp.ClientSession() as session: | ||
async with session.post(url) as response: | ||
return json.loads(await response.text()) | ||
async def stop_io(self) -> None: | ||
await self.rpc.stop_io_for_all_accounts() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import asyncio | ||
import aiohttp | ||
import json | ||
import logging | ||
import os | ||
|
||
|
||
class JsonRpcError(Exception): | ||
pass | ||
|
||
|
||
class Rpc: | ||
def __init__(self, process): | ||
self.process = process | ||
self.event_queue = asyncio.Queue() | ||
self.id = 0 | ||
self.reader_task = asyncio.create_task(self.reader_loop()) | ||
|
||
# Map from request ID to `asyncio.Future` returning the response. | ||
self.request_events = {} | ||
|
||
async def reader_loop(self): | ||
while True: | ||
line = await self.process.stdout.readline() | ||
response = json.loads(line) | ||
if "id" in response: | ||
fut = self.request_events.pop(response["id"]) | ||
fut.set_result(response) | ||
else: | ||
if response["method"] == "event": | ||
# An event notification. | ||
await self.event_queue.put(response["params"]["event"]) | ||
|
||
async def get_next_event(self): | ||
"""Returns next event.""" | ||
return await self.event_queue.get() | ||
|
||
def __getattr__(self, attr): | ||
async def method(*args, **kwargs): | ||
self.id += 1 | ||
request_id = self.id | ||
|
||
params = args | ||
if kwargs: | ||
assert not args | ||
params = kwargs | ||
|
||
request = { | ||
"jsonrpc": "2.0", | ||
"method": attr, | ||
"params": params, | ||
"id": self.id, | ||
} | ||
data = (json.dumps(request) + "\n").encode() | ||
self.process.stdin.write(data) | ||
event = asyncio.Event() | ||
loop = asyncio.get_running_loop() | ||
fut = loop.create_future() | ||
self.request_events[request_id] = fut | ||
response = await fut | ||
if "error" in response: | ||
raise JsonRpcError(response["error"]) | ||
if "result" in response: | ||
return response["result"] | ||
|
||
return method | ||
|
||
|
||
async def start_rpc_server(): | ||
proc = await asyncio.create_subprocess_exec( | ||
"deltachat-rpc-server", | ||
stdin=asyncio.subprocess.PIPE, | ||
stdout=asyncio.subprocess.PIPE, | ||
) | ||
rpc = Rpc(proc) | ||
return rpc | ||
|
||
|
||
async def new_online_account(): | ||
url = os.getenv("DCC_NEW_TMP_EMAIL") | ||
async with aiohttp.ClientSession() as session: | ||
async with session.post(url) as response: | ||
return json.loads(await response.text()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters