added new backend for refactoring purpose, contacts added

This commit is contained in:
Sale Djenic 2021-10-08 11:58:39 +02:00
parent 72a32ee725
commit 0eddfa2b4a
4 changed files with 113 additions and 0 deletions

View File

@ -0,0 +1,41 @@
import json, strmisc
import core, utils
import response_type
export response_type
proc getContacts*(): RpcResponse[JsonNode] {.raises: [Exception].} =
let payload = %* []
result = callPrivateRPC("contacts".prefix, payload)
proc getContactById*(id: string): RpcResponse[JsonNode] {.raises: [Exception].} =
let payload = %* []
result = callPrivateRPC("getContactByID".prefix, payload)
proc saveContact*(id: string, ensVerified: bool, ensName: string, alias: string,
identicon: string, thumbnail: string, largeImage: string, added: bool,
blocked: bool, hasAddedUs: bool, localNickname: string)
{.raises: [Exception].} =
let payload = %* [{
"id": id,
"name": ensName,
"ensVerified": ensVerified,
"alias": alias,
"identicon": identicon,
"images": {
"thumbnail": {"Payload": thumbnail.partition(",")[2]},
"large": {"Payload": largeImage.partition(",")[2]}
},
"added": added,
"blocked": blocked,
"hasAddedUs": hasAddedUs,
"localNickname": localNickname
}]
discard callPrivateRPC("saveContact".prefix, payload)
proc sendContactUpdate*(publicKey, ensName, thumbnail: string)
{.raises: [Exception].} =
let payload = %* [publicKey, ensName, thumbnail]
discard callPrivateRPC("sendContactUpdate".prefix, payload)

View File

@ -0,0 +1,41 @@
import json, json_serialization, strformat, chronicles
import status_go
import response_type
export response_type
logScope:
topics = "rpc"
proc callRPC*(inputJSON: string): string =
return $status_go.callRPC(inputJSON)
proc callPrivateRPCRaw*(inputJSON: string): string {.raises: [].} =
result = $status_go.callPrivateRPC(inputJSON)
proc callPrivateRPC*(methodName: string, payload = %* []): RpcResponse[JsonNode]
{.raises: [RpcException, ValueError, Defect, SerializationError].} =
try:
let inputJSON = %* {
"jsonrpc": "2.0",
"method": methodName,
"params": %payload
}
debug "NewBE_callPrivateRPC", rpc_method=methodName
let rpcResponseRaw = status_go.callPrivateRPC($inputJSON)
result = Json.decode(rpcResponseRaw, RpcResponse[JsonNode])
if(not result.error.isNil):
var err = "\nstatus-go error ["
err &= fmt"methodName:{methodName}, "
err &= fmt"code:{result.error.code}, "
err &= fmt"message:{result.error.message} "
err &= "]\n"
error "rpc response error", err
raise newException(ValueError, err)
except RpcException as e:
error "error doing rpc request", methodName = methodName, exception=e.msg
raise newException(RpcException, e.msg)

View File

@ -0,0 +1,24 @@
{.used.}
import strformat
type
RpcException* = object of CatchableError
type
RpcError* = ref object
code*: int
message*: string
type
RpcResponse*[T] = object
jsonrpc*: string
result*: T
id*: int
error*: RpcError
proc `$`*(self: RpcError): string =
result = fmt"""RpcError(
code: {self.code},
message: {self.message},
]"""

View File

@ -0,0 +1,7 @@
proc isWakuEnabled(): bool =
true # TODO:
proc prefix*(methodName: string, isExt:bool = true): string =
result = if isWakuEnabled(): "waku" else: "shh"
result = result & (if isExt: "ext_" else: "_")
result = result & methodName