Skip to content

Commit

Permalink
style fix for Routes
Browse files Browse the repository at this point in the history
  • Loading branch information
fluency03 committed May 12, 2018
1 parent c24a0c4 commit da560dc
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,4 @@ class TxPoolActor extends ActorSupport {
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ trait BlockPoolRoutes extends RoutesSupport {
post {
parameters('ids.as(CsvSeq[String]).?) { idsOpt: Option[Seq[String]] =>
entity(as[Input]) { in =>
val action = MineAndAddNextBlock(in.content, idsOpt.getOrElse(Seq.empty[String]))
val maybeNextBlock: Future[Option[Block]] =
(blockPoolActor ? MineAndAddNextBlock(in.content, idsOpt.getOrElse(Seq.empty[String]))).mapTo[Option[Block]]
rejectEmptyResponse { complete(maybeNextBlock) }
(blockPoolActor ? action).mapTo[Option[Block]]
rejectEmptyResponse {
complete(maybeNextBlock)
}
}
}
}
Expand All @@ -45,26 +48,30 @@ trait BlockPoolRoutes extends RoutesSupport {
pathEnd {
post {
entity(as[Block]) { block =>
val blockCreated: Future[Message] = (blockPoolActor ? AddBlock(block)).mapTo[Message]
onSuccess(blockCreated) { respondOnCreation }
onSuccess((blockPoolActor ? AddBlock(block)).mapTo[Message]) {
respondOnCreation
}
}
}
} ~
pathPrefix(Segment) { hash =>
pathEnd {
get {
val maybeBlock: Future[Option[Block]] = (blockPoolActor ? GetBlock(hash)).mapTo[Option[Block]]
rejectEmptyResponse { complete(maybeBlock) }
rejectEmptyResponse {
complete((blockPoolActor ? GetBlock(hash)).mapTo[Option[Block]])
}
} ~
delete {
val blockDeleted: Future[Message] = (blockPoolActor ? DeleteBlock(hash)).mapTo[Message]
onSuccess(blockDeleted) { respondOnDeletion }
onSuccess((blockPoolActor ? DeleteBlock(hash)).mapTo[Message]) {
respondOnDeletion
}
}
} ~
path(TRANSACTION / Segment) { id =>
get {
val maybeTx: Future[Option[Transaction]] = (blockPoolActor ? GetTxOfBlock(id, hash)).mapTo[Option[Transaction]]
rejectEmptyResponse { complete(maybeTx) }
rejectEmptyResponse {
complete((blockPoolActor ? GetTxOfBlock(id, hash)).mapTo[Option[Transaction]])
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,33 @@ trait BlockchainRoutes extends RoutesSupport {
pathPrefix(BLOCKCHAIN) {
path(VALIDITY) {
get {
val validity: Future[Message] = (blockchainActor ? CheckBlockchainValidity).mapTo[Message]
onSuccess(validity) { respondOnUpdate }
onSuccess((blockchainActor ? CheckBlockchainValidity).mapTo[Message]) {
respondOnUpdate
}
}
} ~
path(LAST_BLOCK) {
get {
val maybeLastBlock: Future[Option[Block]] = (blockchainActor ? GetLastBlock).mapTo[Option[Block]]
rejectEmptyResponse { complete(maybeLastBlock) }
rejectEmptyResponse {
complete((blockchainActor ? GetLastBlock).mapTo[Option[Block]])
}
} ~
delete {
val blockchainUpdated: Future[Message] = (blockchainActor ? RemoveLastBlock).mapTo[Message]
onSuccess(blockchainUpdated) { respondOnUpdate }
onSuccess((blockchainActor ? RemoveLastBlock).mapTo[Message]) {
respondOnUpdate
}
}
} ~
path(NEW_BLOCK) {
parameters('hash.?) {
case Some(hash) =>
val blockchainUpdated: Future[Message] = (blockchainActor ? AppendBlockFromPool(hash)).mapTo[Message]
onSuccess(blockchainUpdated) { respondOnUpdate }
onSuccess((blockchainActor ? AppendBlockFromPool(hash)).mapTo[Message]) {
respondOnUpdate
}
case None => entity(as[Block]) { block =>
val blockchainUpdated: Future[Message] = (blockchainActor ? AppendBlock(block)).mapTo[Message]
onSuccess(blockchainUpdated) { respondOnUpdate }
onSuccess((blockchainActor ? AppendBlock(block)).mapTo[Message]) {
respondOnUpdate
}
}
}
} ~
Expand All @@ -53,50 +58,59 @@ trait BlockchainRoutes extends RoutesSupport {
parameters('id.as(CsvSeq[String]).?) { idsOpt: Option[Seq[String]] =>
entity(as[Input]) { in =>
val maybeNextBlock: Future[Option[Block]] =
(blockchainActor ? MineNextBlock(in.content, idsOpt.getOrElse(Seq.empty[String]))).mapTo[Option[Block]]
rejectEmptyResponse { complete(maybeNextBlock) }
(blockchainActor ? MineNextBlock(in.content, idsOpt.getOrElse(Seq.empty[String])))
.mapTo[Option[Block]]
rejectEmptyResponse {
complete(maybeNextBlock)
}
}
}
}
} ~
pathEnd {
get {
val blockchain: Future[Option[Blockchain]] = (blockchainActor ? GetBlockchain).mapTo[Option[Blockchain]]
rejectEmptyResponse { complete(blockchain) }
rejectEmptyResponse {
complete((blockchainActor ? GetBlockchain).mapTo[Option[Blockchain]])
}
} ~
post {
val blockchainCreated: Future[Message] = (blockchainActor ? CreateBlockchain).mapTo[Message]
onSuccess(blockchainCreated) { respondOnCreation }
onSuccess((blockchainActor ? CreateBlockchain).mapTo[Message]) {
respondOnCreation
}
} ~
delete {
val blockchainDeleted: Future[Message] = (blockchainActor ? DeleteBlockchain).mapTo[Message]
onSuccess(blockchainDeleted) { respondOnDeletion }
onSuccess((blockchainActor ? DeleteBlockchain).mapTo[Message]) {
respondOnDeletion
}
}
} ~
path(BLOCKS) {
get {
parameters('hashes.as(CsvSeq[String]).?, 'indices.as(CsvSeq[Int]).?) {
(hashesOpt: Option[Seq[String]], indicesOpt: Option[Seq[Int]]) =>
val blocks: Future[Set[Block]] = (blockchainActor ?
GetBlocksByHashesAndIndices(
hashesOpt.getOrElse(Seq.empty[String]).toSet,
indicesOpt.getOrElse(Seq.empty[Int]).toSet))
.mapTo[Set[Block]]
rejectEmptyResponse { complete(blocks) }
val (hashes, indices) = (hashesOpt.getOrElse(Seq.empty[String]).toSet,
indicesOpt.getOrElse(Seq.empty[Int]).toSet)
val blocks: Future[Set[Block]] =
(blockchainActor ? GetBlocksByHashesAndIndices(hashes, indices)).mapTo[Set[Block]]
rejectEmptyResponse {
complete(blocks)
}
}
}
} ~
pathPrefix(BLOCK / Segment) { hash =>
pathEnd {
get {
val maybeBlock: Future[Option[Block]] = (blockchainActor ? GetBlockByHash(hash)).mapTo[Option[Block]]
rejectEmptyResponse { complete(maybeBlock) }
rejectEmptyResponse {
complete((blockchainActor ? GetBlockByHash(hash)).mapTo[Option[Block]])
}
}
} ~
path(TRANSACTION / Segment) { id =>
get {
val maybeTx: Future[Option[Transaction]] = (blockchainActor ? GetTxOfBlock(id, hash)).mapTo[Option[Transaction]]
rejectEmptyResponse { complete(maybeTx) }
rejectEmptyResponse {
complete((blockchainActor ? GetTxOfBlock(id, hash)).mapTo[Option[Transaction]])
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,39 @@ trait GenericRoutes extends RoutesSupport {
pathPrefix(GENERIC) {
path(TO_SHA256) {
post {
entity(as[Input]) { in => complete( failsafeResp { in.content.toSha256 } ) }
entity(as[Input]) { in =>
complete(failsafeResp(in.content.toSha256))
}
}
} ~
path(TO_BASE64) {
post {
entity(as[Input]) { in => complete( failsafeResp { in.content.toBase64 } ) }
}
} ~
path(FROM_BASE64) {
post {
entity(as[Input]) { in => complete( failsafeResp { fromBase64(in.content) } ) }
}
} ~
path(TO_EPOCH_TIME) {
post {
entity(as[Input]) { in => complete( failsafeResp { epochTimeOf(in.content).toString } ) }
}
} ~
path(TIME_FROM_EPOCH) {
post {
entity(as[Input]) { in => complete( failsafeResp { Instant.ofEpochSecond(in.content.toLong).toString } ) }
path(TO_BASE64) {
post {
entity(as[Input]) { in =>
complete(failsafeResp(in.content.toBase64))
}
}
} ~
path(FROM_BASE64) {
post {
entity(as[Input]) { in =>
complete(failsafeResp(fromBase64(in.content)))
}
}
} ~
path(TO_EPOCH_TIME) {
post {
entity(as[Input]) { in =>
complete(failsafeResp(epochTimeOf(in.content).toString))
}
}
} ~
path(TIME_FROM_EPOCH) {
post {
entity(as[Input]) { in =>
complete(failsafeResp(Instant.ofEpochSecond(in.content.toLong).toString))
}
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ trait NetworkRoutes extends RoutesSupport {
lazy val networkRoutes: Route =
path(NETWORK) {
get {
val network: Future[Set[String]] = (networkActor ? GetNetwork).mapTo[Set[String]]
complete(network)
complete((networkActor ? GetNetwork).mapTo[Set[String]])
}
} ~
path(PEERS) {
get {
parameters( 'names.as(CsvSeq[String]).? ) { namesOpt =>
val peers: Future[Map[String, Set[String]]] = namesOpt match {
case Some(names) => (networkActor ? GetPeers(names.toSet)).mapTo[Map[String, Set[String]]]
case Some(names) =>
(networkActor ? GetPeers(names.toSet)).mapTo[Map[String, Set[String]]]
case None => (networkActor ? GetPeers).mapTo[Map[String, Set[String]]]
}
complete(peers)
Expand All @@ -48,19 +48,22 @@ trait NetworkRoutes extends RoutesSupport {
pathEnd {
post {
entity(as[PeerSimple]) { peer =>
val peerCreated: Future[Message] = (networkActor ? CreatePeer(peer.name)).mapTo[Message]
onSuccess(peerCreated) { respondOnCreation }
onSuccess((networkActor ? CreatePeer(peer.name)).mapTo[Message]) {
respondOnCreation
}
}
}
} ~
path(Segment) { name =>
get {
val maybePeer: Future[Option[Peer]] = (networkActor ? GetPeer(name)).mapTo[Option[Peer]]
rejectEmptyResponse { complete(maybePeer) }
rejectEmptyResponse {
complete((networkActor ? GetPeer(name)).mapTo[Option[Peer]])
}
} ~
delete {
val peerDeleted: Future[Message] = (networkActor ? DeletePeer(name)).mapTo[Message]
onSuccess(peerDeleted) { respondOnDeletion }
onSuccess((networkActor ? DeletePeer(name)).mapTo[Message]) {
respondOnDeletion
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ trait RoutesSupport extends JsonSupport {
implicit def system: ActorSystem

// Required by the `ask` (?) method
implicit lazy val timeout: Timeout = Timeout(5.seconds) // usually we'd obtain the timeout from the system's configuration
// usually we'd obtain the timeout from the system's configuration
implicit lazy val timeout: Timeout = Timeout(5.seconds)

def respondOnCreation(m: Message): StandardRoute = m match {
case s: SuccessMsg => complete((StatusCodes.Created, s))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,31 @@ trait TxPoolRoutes extends RoutesSupport {
pathEnd {
post {
entity(as[Transaction]) { tx =>
val msgOnCreate: Future[Message] = (txPoolActor ? AddTransaction(tx)).mapTo[Message]
onSuccess(msgOnCreate) { respondOnCreation }
onSuccess((txPoolActor ? AddTransaction(tx)).mapTo[Message]) {
respondOnCreation
}
}
}
} ~
path(Segment) { id =>
get {
val maybeTx: Future[Option[Transaction]] = (txPoolActor ? GetTransaction(id)).mapTo[Option[Transaction]]
rejectEmptyResponse { complete(maybeTx) }
rejectEmptyResponse {
complete((txPoolActor ? GetTransaction(id)).mapTo[Option[Transaction]])
}
} ~
delete {
val txDeleted: Future[Message] = (txPoolActor ? DeleteTransaction(id)).mapTo[Message]
onSuccess(txDeleted) { respondOnDeletion }
onSuccess((txPoolActor ? DeleteTransaction(id)).mapTo[Message]) {
respondOnDeletion
}
} ~
put {
entity(as[Transaction]) { tx =>
if (tx.id != id)
complete((StatusCodes.InternalServerError, FailureMsg("Transaction ID in the data does not match ID on the path.")))
else {
val msgOnUpdate: Future[Message] = (txPoolActor ? UpdateTransaction(tx)).mapTo[Message]
onSuccess(msgOnUpdate) { respondOnUpdate }
if (tx.id != id) {
val code = StatusCodes.InternalServerError
val msg = FailureMsg("Transaction ID in the data does not match ID on the path.")
complete((code, msg))
} else onSuccess((txPoolActor ? UpdateTransaction(tx)).mapTo[Message]) {
respondOnUpdate
}
}
}
Expand Down

0 comments on commit da560dc

Please sign in to comment.