2022-11-09 17:50:18 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
|
|
|
|
2024-04-25 13:09:52 +00:00
|
|
|
import std/[options], stew/results
|
2024-03-15 23:08:47 +00:00
|
|
|
import ../waku_core, ../common/paging
|
2022-11-09 17:50:18 +00:00
|
|
|
|
2022-11-23 09:08:00 +00:00
|
|
|
const
|
2024-04-25 13:09:52 +00:00
|
|
|
WakuStoreCodec* = "/vac/waku/store-query/3.0.0"
|
2022-11-09 17:50:18 +00:00
|
|
|
|
|
|
|
DefaultPageSize*: uint64 = 20
|
|
|
|
|
|
|
|
MaxPageSize*: uint64 = 100
|
|
|
|
|
2024-04-25 13:09:52 +00:00
|
|
|
EmptyCursor*: WakuMessageHash = EmptyWakuMessageHash
|
2022-11-09 17:50:18 +00:00
|
|
|
|
2024-04-25 13:09:52 +00:00
|
|
|
type WakuStoreResult*[T] = Result[T, string]
|
2022-11-09 17:50:18 +00:00
|
|
|
|
|
|
|
## Public API types
|
|
|
|
|
|
|
|
type
|
2024-04-25 13:09:52 +00:00
|
|
|
StoreQueryRequest* = object
|
|
|
|
requestId*: string
|
|
|
|
includeData*: bool
|
2022-11-09 17:50:18 +00:00
|
|
|
|
|
|
|
pubsubTopic*: Option[PubsubTopic]
|
|
|
|
contentTopics*: seq[ContentTopic]
|
|
|
|
startTime*: Option[Timestamp]
|
|
|
|
endTime*: Option[Timestamp]
|
|
|
|
|
2024-04-25 13:09:52 +00:00
|
|
|
messageHashes*: seq[WakuMessageHash]
|
|
|
|
|
|
|
|
paginationCursor*: Option[WakuMessageHash]
|
|
|
|
paginationForward*: PagingDirection
|
|
|
|
paginationLimit*: Option[uint64]
|
|
|
|
|
|
|
|
WakuMessageKeyValue* = object
|
|
|
|
messageHash*: WakuMessageHash
|
2024-05-01 18:47:06 +00:00
|
|
|
message*: Option[WakuMessage]
|
2024-04-25 13:09:52 +00:00
|
|
|
|
|
|
|
StoreQueryResponse* = object
|
|
|
|
requestId*: string
|
|
|
|
|
|
|
|
statusCode*: uint32
|
|
|
|
statusDesc*: string
|
|
|
|
|
|
|
|
messages*: seq[WakuMessageKeyValue]
|
|
|
|
|
|
|
|
paginationCursor*: Option[WakuMessageHash]
|
2022-11-09 17:50:18 +00:00
|
|
|
|
2024-04-25 13:09:52 +00:00
|
|
|
StatusCode* {.pure.} = enum
|
2022-11-09 17:50:18 +00:00
|
|
|
UNKNOWN = uint32(000)
|
2024-04-25 13:09:52 +00:00
|
|
|
SUCCESS = uint32(200)
|
2022-11-09 17:50:18 +00:00
|
|
|
BAD_RESPONSE = uint32(300)
|
|
|
|
BAD_REQUEST = uint32(400)
|
2024-04-15 13:28:35 +00:00
|
|
|
TOO_MANY_REQUESTS = uint32(429)
|
2022-11-09 17:50:18 +00:00
|
|
|
SERVICE_UNAVAILABLE = uint32(503)
|
2023-11-30 15:42:58 +00:00
|
|
|
PEER_DIAL_FAILURE = uint32(504)
|
2022-11-23 09:08:00 +00:00
|
|
|
|
2024-04-25 13:09:52 +00:00
|
|
|
ErrorCode* {.pure.} = enum
|
|
|
|
UNKNOWN = uint32(000)
|
|
|
|
BAD_RESPONSE = uint32(300)
|
|
|
|
BAD_REQUEST = uint32(400)
|
|
|
|
TOO_MANY_REQUESTS = uint32(429)
|
|
|
|
SERVICE_UNAVAILABLE = uint32(503)
|
|
|
|
PEER_DIAL_FAILURE = uint32(504)
|
|
|
|
|
|
|
|
StoreError* = object
|
|
|
|
case kind*: ErrorCode
|
|
|
|
of ErrorCode.PEER_DIAL_FAILURE:
|
2022-11-09 17:50:18 +00:00
|
|
|
address*: string
|
2024-04-25 13:09:52 +00:00
|
|
|
of ErrorCode.BAD_RESPONSE, ErrorCode.BAD_REQUEST:
|
2022-11-09 17:50:18 +00:00
|
|
|
cause*: string
|
|
|
|
else:
|
|
|
|
discard
|
2022-11-23 09:08:00 +00:00
|
|
|
|
2024-04-25 13:09:52 +00:00
|
|
|
StoreQueryResult* = Result[StoreQueryResponse, StoreError]
|
|
|
|
|
|
|
|
proc into*(errCode: ErrorCode): StatusCode =
|
|
|
|
StatusCode(uint32(errCode))
|
|
|
|
|
|
|
|
proc new*(T: type StoreError, code: uint32, desc: string): T =
|
|
|
|
let kind = ErrorCode.parse(code)
|
2022-11-09 17:50:18 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
case kind
|
2024-04-25 13:09:52 +00:00
|
|
|
of ErrorCode.BAD_RESPONSE:
|
|
|
|
return StoreError(kind: kind, cause: desc)
|
|
|
|
of ErrorCode.BAD_REQUEST:
|
|
|
|
return StoreError(kind: kind, cause: desc)
|
|
|
|
of ErrorCode.TOO_MANY_REQUESTS:
|
|
|
|
return StoreError(kind: kind)
|
|
|
|
of ErrorCode.SERVICE_UNAVAILABLE:
|
|
|
|
return StoreError(kind: kind)
|
|
|
|
of ErrorCode.PEER_DIAL_FAILURE:
|
|
|
|
return StoreError(kind: kind, address: desc)
|
|
|
|
of ErrorCode.UNKNOWN:
|
|
|
|
return StoreError(kind: kind)
|
|
|
|
|
|
|
|
proc parse*(T: type ErrorCode, kind: uint32): T =
|
|
|
|
case kind
|
|
|
|
of 000, 300, 400, 429, 503, 504:
|
|
|
|
ErrorCode(kind)
|
2022-11-09 17:50:18 +00:00
|
|
|
else:
|
2024-04-25 13:09:52 +00:00
|
|
|
ErrorCode.UNKNOWN
|
2022-11-09 17:50:18 +00:00
|
|
|
|
2024-04-25 13:09:52 +00:00
|
|
|
proc `$`*(err: StoreError): string =
|
2024-03-15 23:08:47 +00:00
|
|
|
case err.kind
|
2024-04-25 13:09:52 +00:00
|
|
|
of ErrorCode.PEER_DIAL_FAILURE:
|
2022-11-23 09:08:00 +00:00
|
|
|
"PEER_DIAL_FAILURE: " & err.address
|
2024-04-25 13:09:52 +00:00
|
|
|
of ErrorCode.BAD_RESPONSE:
|
2022-11-09 17:50:18 +00:00
|
|
|
"BAD_RESPONSE: " & err.cause
|
2024-04-25 13:09:52 +00:00
|
|
|
of ErrorCode.BAD_REQUEST:
|
2022-11-09 17:50:18 +00:00
|
|
|
"BAD_REQUEST: " & err.cause
|
2024-04-25 13:09:52 +00:00
|
|
|
of ErrorCode.TOO_MANY_REQUESTS:
|
2024-04-15 13:28:35 +00:00
|
|
|
"TOO_MANY_REQUESTS"
|
2024-04-25 13:09:52 +00:00
|
|
|
of ErrorCode.SERVICE_UNAVAILABLE:
|
2022-11-09 17:50:18 +00:00
|
|
|
"SERVICE_UNAVAILABLE"
|
2024-04-25 13:09:52 +00:00
|
|
|
of ErrorCode.UNKNOWN:
|
2022-11-23 09:08:00 +00:00
|
|
|
"UNKNOWN"
|