nwaku/waku/waku_store/common.nim

120 lines
2.9 KiB
Nim
Raw Normal View History

{.push raises: [].}
import std/[options], results
import ../waku_core, ../common/paging
from ../waku_core/codecs import WakuStoreCodec
export WakuStoreCodec
const
DefaultPageSize*: uint64 = 20
MaxPageSize*: uint64 = 100
2024-04-25 13:09:52 +00:00
EmptyCursor*: WakuMessageHash = EmptyWakuMessageHash
2024-04-25 13:09:52 +00:00
type WakuStoreResult*[T] = Result[T, string]
## Public API types
type
2024-04-25 13:09:52 +00:00
StoreQueryRequest* = object
requestId*: string
includeData*: bool
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
message*: Option[WakuMessage]
pubsubTopic*: Option[PubsubTopic]
2024-04-25 13:09:52 +00:00
StoreQueryResponse* = object
requestId*: string
statusCode*: uint32
statusDesc*: string
messages*: seq[WakuMessageKeyValue]
paginationCursor*: Option[WakuMessageHash]
2024-04-25 13:09:52 +00:00
StatusCode* {.pure.} = enum
UNKNOWN = uint32(000)
2024-04-25 13:09:52 +00:00
SUCCESS = uint32(200)
BAD_RESPONSE = uint32(300)
BAD_REQUEST = uint32(400)
TOO_MANY_REQUESTS = uint32(429)
SERVICE_UNAVAILABLE = uint32(503)
PEER_DIAL_FAILURE = uint32(504)
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:
address*: string
2024-04-25 13:09:52 +00:00
of ErrorCode.BAD_RESPONSE, ErrorCode.BAD_REQUEST:
cause*: string
else:
discard
2024-04-25 13:09:52 +00:00
StoreQueryResult* = Result[StoreQueryResponse, StoreError]
proc into*(errCode: ErrorCode): StatusCode =
cast[StatusCode](uint32(errCode))
2024-04-25 13:09:52 +00:00
proc new*(T: type StoreError, code: uint32, desc: string): T =
let kind = ErrorCode.parse(code)
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:
cast[ErrorCode](kind)
else:
2024-04-25 13:09:52 +00:00
ErrorCode.UNKNOWN
2024-04-25 13:09:52 +00:00
proc `$`*(err: StoreError): string =
case err.kind
2024-04-25 13:09:52 +00:00
of ErrorCode.PEER_DIAL_FAILURE:
"PEER_DIAL_FAILURE: " & err.address
2024-04-25 13:09:52 +00:00
of ErrorCode.BAD_RESPONSE:
"BAD_RESPONSE: " & err.cause
2024-04-25 13:09:52 +00:00
of ErrorCode.BAD_REQUEST:
"BAD_REQUEST: " & err.cause
2024-04-25 13:09:52 +00:00
of ErrorCode.TOO_MANY_REQUESTS:
"TOO_MANY_REQUESTS"
2024-04-25 13:09:52 +00:00
of ErrorCode.SERVICE_UNAVAILABLE:
"SERVICE_UNAVAILABLE"
2024-04-25 13:09:52 +00:00
of ErrorCode.UNKNOWN:
"UNKNOWN"