Skip to content

Commit

Permalink
server: Add endpoint: "GET /transactions/:txid/raw"
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexITC committed May 12, 2018
1 parent 4e96219 commit c600980
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
14 changes: 14 additions & 0 deletions server/app/com/xsn/explorer/services/TransactionService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,25 @@ import com.xsn.explorer.errors.{TransactionFormatError, TransactionNotFoundError
import com.xsn.explorer.models.rpc.TransactionVIN
import com.xsn.explorer.models.{Transaction, TransactionDetails, TransactionId, TransactionValue}
import org.scalactic.{Bad, Good, One, Or}
import play.api.libs.json.JsValue

import scala.concurrent.{ExecutionContext, Future}

class TransactionService @Inject() (xsnService: XSNService)(implicit ec: ExecutionContext) {

def getRawTransaction(txidString: String): FutureApplicationResult[JsValue] = {
val result = for {
txid <- {
val maybe = TransactionId.from(txidString)
Or.from(maybe, One(TransactionFormatError)).toFutureOr
}

transaction <- xsnService.getRawTransaction(txid).toFutureOr
} yield transaction

result.toFuture
}

def getTransactionDetails(txidString: String): FutureApplicationResult[TransactionDetails] = {
val result = for {
txid <- {
Expand Down
4 changes: 4 additions & 0 deletions server/app/controllers/TransactionsController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ class TransactionsController @Inject() (
def getTransaction(txid: String) = publicNoInput { _ =>
transactionService.getTransactionDetails(txid)
}

def getRawTransaction(txid: String) = publicNoInput { _ =>
transactionService.getRawTransaction(txid)
}
}
3 changes: 2 additions & 1 deletion server/conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

GET /health controllers.HealthController.check()

GET /transactions/:txid controllers.TransactionsController.getTransaction(txid: String)
GET /transactions/:txid controllers.TransactionsController.getTransaction(txid: String)
GET /transactions/:txid/raw controllers.TransactionsController.getRawTransaction(txid: String)

GET /addresses/:address controllers.AddressesController.getDetails(address: String)

Expand Down
26 changes: 26 additions & 0 deletions server/test/controllers/TransactionsControllerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ class TransactionsControllerSpec extends MyAPISpec {

Future.successful(result)
}

override def getRawTransaction(txid: TransactionId): FutureApplicationResult[JsValue] = {
val result = map.get(txid)
.map { _ => TransactionLoader.json(txid.string) }
.map(Good(_))
.getOrElse {
Bad(TransactionNotFoundError).accumulating
}

Future.successful(result)
}
}

override val application = guiceApplicationBuilder
Expand Down Expand Up @@ -179,4 +190,19 @@ class TransactionsControllerSpec extends MyAPISpec {
(error \ "message").as[String].nonEmpty mustEqual true
}
}

"GET transactions/:txid/raw" should {
def url(txid: String) = s"/transactions/$txid/raw"

"retrieve the raw transaction" in {
val tx = coinbaseTx
val expected = TransactionLoader.json(tx.id.string)
val response = GET(url(tx.id.string))

status(response) mustEqual OK
val json = contentAsJson(response)

json mustEqual expected
}
}
}

0 comments on commit c600980

Please sign in to comment.