Skip to content

Commit

Permalink
Merge pull request #4 from AustEcon/master
Browse files Browse the repository at this point in the history
  • Loading branch information
Jad Wahab authored Feb 4, 2022
2 parents 3ed8002 + 508062a commit 4b05ddf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ Retrieves the header with the given hash
```
/api/v1/chain/header/{hash}
```
The content type: ```application/octet-stream``` can be provided in the requst header to return the headers raw bytes.
The Accept type: ```application/octet-stream``` can be provided in the request header to return the headers raw bytes.

#### Query Headers By Height (batched)
Retrieves the header with the given hash
```
/api/v1/chain/header/byHeight?height=<start_height>&count=<count>
```
The Accept type: ```application/octet-stream``` can be provided in the requst header to return the headers as a
The Accept type: ```application/octet-stream``` can be provided in the request header to return the headers as a
stream of raw bytes.

The query parameter: height is required. The count of headers requested is optional (default = 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,23 @@ public BlockHeaderControllerV1(HSVFacade blockChainFacade) {

@RequestMapping("/{hash}")
public ResponseEntity<?> getHeader(@PathVariable String hash,
@RequestHeader(value = "Accept", required = false, defaultValue = "application/json") MediaType acceptContentType) {
@RequestHeader(value = "Accept", required = false, defaultValue = "application/json") String acceptContentType) {
BlockHeaderDTO blockHeaderDTO = hsvFacade.getBlockHeader(hash);

if (blockHeaderDTO == null) {
throw new ResponseStatusException(HttpStatus.NO_CONTENT, "BlockHeader not found");
}

switch (acceptContentType.toString()) {
case MediaType.APPLICATION_OCTET_STREAM_VALUE:
return ResponseEntity
.ok()
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(blockHeaderDTO.getHeaderReadOnly().serialize());

default:
return ResponseEntity
.ok()
.contentType(MediaType.APPLICATION_JSON)
.body(blockHeaderDTO);
if (MediaType.APPLICATION_OCTET_STREAM_VALUE.equals(acceptContentType)) {
return ResponseEntity
.ok()
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(blockHeaderDTO.getHeaderReadOnly().serialize());
}
return ResponseEntity
.ok()
.contentType(MediaType.APPLICATION_JSON)
.body(blockHeaderDTO);
}

@RequestMapping("/{hash}/ancestors")
Expand All @@ -67,14 +64,14 @@ public ResponseEntity<?> getAncestors(@PathVariable String hash, @RequestBody St

@RequestMapping("/byHeight")
public ResponseEntity<?> getHeadersByHeight(@RequestParam String height, @RequestParam(defaultValue = "1") String count,
@RequestHeader(value = "Accept", required = false, defaultValue = "application/json") MediaType acceptContentType){
@RequestHeader(value = "Accept", required = false, defaultValue = "application/json") String acceptContentType){
try {
if (Integer.parseInt(count) > 2000) {
throw new IllegalArgumentException("Count exceeds max value of 2000 headers");
}

List<BlockHeaderDTO> headers = hsvFacade.getHeadersByHeight(Integer.parseInt(height), Integer.parseInt(count));
if (acceptContentType.toString().equals(MediaType.APPLICATION_OCTET_STREAM_VALUE)) {
if (acceptContentType.equals(MediaType.APPLICATION_OCTET_STREAM_VALUE)) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (BlockHeaderDTO header : headers) {
baos.write(header.getHeaderReadOnly().serialize());
Expand Down

0 comments on commit 4b05ddf

Please sign in to comment.