-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeltaRpcClient.cs
More file actions
73 lines (59 loc) · 3.7 KB
/
DeltaRpcClient.cs
File metadata and controls
73 lines (59 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
public class DeltaRpcClient
{
private static Random random = new Random();
private StreamWriter streamWriter;
private StreamReader streamReader;
public DeltaRpcClient(StreamWriter streamWriter, StreamReader streamReader)
{
this.streamWriter = streamWriter;
this.streamReader = streamReader;
this.streamWriter.AutoFlush = true;
}
public SystemInfoResult GetSystemInfo() => SendAndReceive<SystemInfoResult>(DeltaChatMethodNames.METHOD_GET_SYSTEM_INFO);
public int[] GetAllAccountIds() => SendAndReceive<int[]>("get_all_account_ids");
public int AddAccount() => SendAndReceive<int>("add_account");
public void ConfigureAccountSetting(int accountId, string key, string value) => SendAndReceive<Void>(DeltaChatMethodNames.METHOD_SET_CONFIG, accountId, key, value);
public bool IsAccountConfigured(int accountId) => SendAndReceive<bool>(DeltaChatMethodNames.METHOD_IS_CONFIGURED, accountId);
public object ConfigureAccount(int accountId) => SendAndReceive<object>(DeltaChatMethodNames.METHOD_CONFIGURE, accountId);
public void StartIOForAccount(int accountId) => SendAndReceive<Void>(DeltaChatMethodNames.METHOD_START_IO, accountId);
public int[] GetNextMessagesForAccount(int accountId) => SendAndReceive<int[]>(DeltaChatMethodNames.METHOD_GET_NEXT_MSGS, accountId);
public object WaitForNextMessages(int accountId) => SendAndReceive<object>(DeltaChatMethodNames.METHOD_WAIT_NEXT_MSGS, accountId);
public ChatMessage GetMessage(int accountId, int messageId) => SendAndReceive<ChatMessage>(DeltaChatMethodNames.METHOD_GET_MESSAGE, accountId, messageId);
public void MarkMessagesAsSeen(int accountId, params int[] messageIds) => SendAndReceive<Void>(DeltaChatMethodNames.METHOD_MARKSEEN_MSGS, accountId, messageIds);
public dynamic GetNextEvent() => SendAndReceive<dynamic>(DeltaChatMethodNames.METHOD_GET_NEXT_EVENT);
public void ImportBackup(int accountId, string path, string passphrase) => SendAndReceive<Void>(DeltaChatMethodNames.METHOD_IMPORT_BACKUP, accountId, path, passphrase);
public void SendMessage(int accountId, int chatId, MessageData messageData) => SendAndReceive<object>(DeltaChatMethodNames.METHOD_SEND_MSG, accountId, chatId, messageData);
private T SendAndReceive<T>(string method, params object[] paremeters)
{
string? responseJson = "";
try
{
var request = new Request(method: method, id: random.Next(0, 10000), @params: paremeters);
var requestJson = JsonConvert.SerializeObject(request);
streamWriter.WriteLine(requestJson);
try { streamWriter.Flush(); } catch { }
responseJson = streamReader.ReadLine();
MarkupLineInterpolated($"[yellow]{Spectre.Console.Markup.Escape(responseJson ?? "<empty response>")}[/]");
if (String.IsNullOrWhiteSpace(responseJson))
{
throw new InvalidDataException("Did not receive a valid response");
}
var response = JsonConvert.DeserializeObject<JsonRpcContainer<T>>(responseJson);
if (request.id != response.MessageId)
{
throw new InvalidDataException("The response id does not match the request id");
}
if (response.Error != null)
{
MarkupLineInterpolated($"[red]ERROR:[/] [yellow]{Spectre.Console.Markup.Escape(response.Error.ToString())}[/]");
}
return response.Result;
}
catch (Exception ex)
{
MarkupLineInterpolated($"SendAndReceive: [red]Exception:[/][yellow]{ex.ToString()}[/]");
MarkupLineInterpolated($"[red]{responseJson ?? "null"}[/]");
return default!;
}
}
}