add `sszResponsePlain` for pre-encoded SSZ data (#4171)

For pre-encoded JSON REST responses we have `jsonResponsePlain`.
Adds a `sszResponsePlain` function to serve similar purpose for SSZ.
This avoids caller having to explicitly specify Http200 and media type.
This commit is contained in:
Etan Kissling 2022-09-23 17:51:04 +02:00 committed by GitHub
parent 6425f3f856
commit 77ea188c92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 5 deletions

View File

@ -147,7 +147,7 @@ proc installBeaconApiHandlers*(router: var RestRouter, node: BeaconNode) =
# in current version of database. # in current version of database.
return RestApiResponse.jsonError(Http500, NoImplementationError) return RestApiResponse.jsonError(Http500, NoImplementationError)
return RestApiResponse.jsonError(Http404, StateNotFoundError, return RestApiResponse.jsonError(Http404, StateNotFoundError,
$error) $error)
node.withStateForBlockSlotId(bslot): node.withStateForBlockSlotId(bslot):
return RestApiResponse.jsonResponseWOpt( return RestApiResponse.jsonResponseWOpt(
@ -820,7 +820,7 @@ proc installBeaconApiHandlers*(router: var RestRouter, node: BeaconNode) =
if not node.dag.getBlockSSZ(bid, data): if not node.dag.getBlockSSZ(bid, data):
return RestApiResponse.jsonError(Http404, BlockNotFoundError) return RestApiResponse.jsonError(Http404, BlockNotFoundError)
RestApiResponse.response(data, Http200, $sszMediaType) RestApiResponse.sszResponsePlain(data)
elif contentType == jsonMediaType: elif contentType == jsonMediaType:
let bdata = node.dag.getForkedBlock(bid).valueOr: let bdata = node.dag.getForkedBlock(bid).valueOr:
return RestApiResponse.jsonError(Http404, BlockNotFoundError) return RestApiResponse.jsonError(Http404, BlockNotFoundError)
@ -862,8 +862,7 @@ proc installBeaconApiHandlers*(router: var RestRouter, node: BeaconNode) =
fork = node.dag.cfg.blockForkAtEpoch(bid.slot.epoch) fork = node.dag.cfg.blockForkAtEpoch(bid.slot.epoch)
headers = [("eth-consensus-version", fork.toString())] headers = [("eth-consensus-version", fork.toString())]
RestApiResponse.response(data, Http200, $sszMediaType, RestApiResponse.sszResponsePlain(data, headers)
headers = headers)
elif contentType == jsonMediaType: elif contentType == jsonMediaType:
let bdata = node.dag.getForkedBlock(bid).valueOr: let bdata = node.dag.getForkedBlock(bid).valueOr:
return RestApiResponse.jsonError(Http404, BlockNotFoundError) return RestApiResponse.jsonError(Http404, BlockNotFoundError)

View File

@ -454,8 +454,14 @@ proc jsonErrorList*(t: typedesc[RestApiResponse],
default default
RestApiResponse.error(status, data, "application/json") RestApiResponse.error(status, data, "application/json")
proc sszResponsePlain*(t: typedesc[RestApiResponse], res: seq[byte],
headers: openArray[RestKeyValueTuple] = []
): RestApiResponse =
RestApiResponse.response(res, Http200, "application/octet-stream",
headers = headers)
proc sszResponse*(t: typedesc[RestApiResponse], data: auto, proc sszResponse*(t: typedesc[RestApiResponse], data: auto,
headers: openArray[tuple[key: string, value: string]] headers: openArray[RestKeyValueTuple] = []
): RestApiResponse = ): RestApiResponse =
let res = let res =
block: block: