Skip to content

Commit

Permalink
cache String body to reuse for compact and detailed description
Browse files Browse the repository at this point in the history
  • Loading branch information
agourlay committed Dec 9, 2023
1 parent 5f0d784 commit d114468
Showing 1 changed file with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,17 @@ case class HttpRequest[A: Show: Resolvable: Encoder](method: HttpMethod, url: St

def withBody[B: Show: Resolvable: Encoder](body: B) = copy(body = Some(body))

// compute once and cache
private lazy val bodyAsString: Option[String] = body.map(_.show)

def compactDescription: String = {
val builder = new StringBuilder()
builder.append(method.name)
builder.append(" ")
builder.append(url)
body.foreach { p =>
bodyAsString.foreach { p =>
builder.append(" with body\n")
builder.append(p.show)
builder.append(p)
}
if (params.nonEmpty) {
builder.append(" with query parameters ")
Expand Down Expand Up @@ -95,10 +98,10 @@ case class HttpRequest[A: Show: Resolvable: Encoder](method: HttpMethod, url: St
builder.append("\n")

// body
body match {
bodyAsString match {
case Some(b) =>
builder.append("with body\n")
builder.append(b.show)
builder.append(b)
case None => builder.append("without body")
}
builder.result()
Expand Down

0 comments on commit d114468

Please sign in to comment.