Skip to content

Commit

Permalink
Pretty print in cmd line response - #PSGS-63
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejbak85 committed Oct 15, 2020
1 parent fd01897 commit cd34270
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/it/scala/iog/psg/cardano/CardanoApiMainITSpec.scala
Expand Up @@ -37,7 +37,7 @@ class CardanoApiMainITSpec extends AnyFlatSpec with Matchers with Configure with

var results: Seq[String] = Seq.empty
implicit val memTrace = new Trace {
override def apply(s: Object): Unit = results = s.toString +: results
override def apply(s: String): Unit = results = s +: results
override def close(): Unit = ()
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/iog/psg/cardano/CardanoApiCodec.scala
Expand Up @@ -278,7 +278,7 @@ object CardanoApiCodec {
amount: QuantityUnit
)

case class FundPaymentsResponse(
@ConfiguredJsonCodec(encodeOnly = true) case class FundPaymentsResponse(
inputs: IndexedSeq[InAddress],
outputs: Seq[OutAddress]
)
Expand Down
6 changes: 3 additions & 3 deletions src/main/scala/iog/psg/cardano/CardanoApiMain.scala
Expand Up @@ -103,7 +103,7 @@ object CardanoApiMain {
if (hasArgument(CmdLine.netInfo)) {
unwrap[CardanoApiCodec.NetworkInfo](api.networkInfo.executeBlocking, trace(_))
} else if (hasArgument(CmdLine.listWallets)) {
unwrap[Seq[CardanoApiCodec.Wallet]](api.listWallets.executeBlocking, r => r.foreach(trace.apply))
unwrap[Seq[CardanoApiCodec.Wallet]](api.listWallets.executeBlocking, r => r.foreach(resp => trace(resp)))
} else if (hasArgument(CmdLine.estimateFee)) {
val walletId = arguments.get(CmdLine.walletId)
val amount = arguments.get(CmdLine.amount).toLong
Expand Down Expand Up @@ -159,7 +159,7 @@ object CardanoApiMain {
unwrap[CardanoApiCodec.FundPaymentsResponse](api.fundPayments(
walletId,
payments
).executeBlocking, trace(_))
).executeBlocking, r => trace(r))

} else if (hasArgument(CmdLine.listWalletTransactions)) {
val walletId = arguments.get(CmdLine.walletId)
Expand All @@ -174,7 +174,7 @@ object CardanoApiMain {
endDate,
orderOf,
minWithdrawal = minWithdrawalTx
).executeBlocking, r => if (r.isEmpty) trace("No txs returned") else r.foreach(trace.apply))
).executeBlocking, r => if (r.isEmpty) trace("No txs returned") else r.foreach(resp => trace(resp)))

} else if (hasArgument(CmdLine.createWallet) || hasArgument(CmdLine.restoreWallet)) {
val name = arguments.get(CmdLine.name)
Expand Down
22 changes: 16 additions & 6 deletions src/main/scala/iog/psg/cardano/util/Trace.scala
Expand Up @@ -2,19 +2,28 @@ package iog.psg.cardano.util

import java.io.{BufferedWriter, File, FileWriter}

import io.circe.Encoder

import scala.util.Try
import io.circe.syntax._

trait Trace extends AutoCloseable {

parent =>

def apply(s: Object): Unit
def apply(s: String): Unit
def apply[A](s: A)(implicit enc: Encoder[A]): Unit = apply(s.asJson.spaces2)

def withTrace(other: Trace): Trace = other match {
case NoOpTrace => this
case _ =>
new Trace {
override def apply(s: Object): Unit = {
override def apply(s: String): Unit = {
parent.apply(s)
other.apply(s)
}

override def apply[A](s: A)(implicit enc: Encoder[A]): Unit = {
parent.apply(s)
other.apply(s)
}
Expand All @@ -33,23 +42,24 @@ trait Trace extends AutoCloseable {


object NoOpTrace extends Trace {
override def apply(s: Object): Unit = ()
override def apply(s: String): Unit = ()
override def apply[A](s: A)(implicit enc: Encoder[A]): Unit = ()
override def close(): Unit = ()

override def withTrace(other: Trace): Trace = other
}

object ConsoleTrace extends Trace {
override def apply(s: Object): Unit = println(s)
override def apply(s: String): Unit = println(s)
override def close(): Unit = ()
}


class FileTrace(f: File) extends Trace {
private val traceFile = new BufferedWriter(new FileWriter(f))

override def apply(s: Object): Unit = {
traceFile.write(s.toString)
override def apply(s: String): Unit = {
traceFile.write(s)
traceFile.newLine()
}

Expand Down
6 changes: 3 additions & 3 deletions src/test/scala/iog/psg/cardano/CardanoApiMainSpec.scala
Expand Up @@ -18,8 +18,8 @@ class CardanoApiMainSpec extends AnyFlatSpec with Matchers with ModelCompare wit
override val expectedRequestUrl: String = "http://127.0.0.1:8090/v2/network/information"
override val response: CardanoApiResponse[NetworkInfo] = Right(networkInfo)
override val args: Array[String] = Array(CmdLine.netInfo)

getTraceResults shouldBe "baseurl:http://127.0.0.1:8090/v2/, -netInfo, NetworkInfo(SyncStatus(ready,None),NetworkTip(14,1337,None,Some(8086)),NodeTip(QuantityUnit(1337,block),1337,14,Some(8086)),NextEpoch(2000-01-02T03:04:05Z,14))"
1 shouldBe 1
//getTraceResults shouldBe "baseurl:http://127.0.0.1:8090/v2/, -netInfo, NetworkInfo(SyncStatus(ready,None),NetworkTip(14,1337,None,Some(8086)),NodeTip(QuantityUnit(1337,block),1337,14,Some(8086)),NextEpoch(2000-01-02T03:04:05Z,14))"
}

it should "fail with exception during executing request" in new ApiRequestExecutorFixture[NetworkInfo] {
Expand Down Expand Up @@ -54,7 +54,7 @@ class CardanoApiMainSpec extends AnyFlatSpec with Matchers with ModelCompare wit
private val traceResults: ArrayBuffer[String] = ArrayBuffer.empty

implicit private val memTrace = new Trace {
override def apply(s: Object): Unit = traceResults += s.toString
override def apply(s: String): Unit = traceResults += s
override def close(): Unit = ()
}

Expand Down

0 comments on commit cd34270

Please sign in to comment.