Skip to content

Commit

Permalink
feat: add query param on http RequestBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-alfers committed Jan 16, 2025
1 parent 88d25a6 commit 54e5b2a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public interface RequestBuilder<R> {

RequestBuilder<R> withTimeout(Duration timeout);

RequestBuilder<R> addQueryParameter(String key, String value);

/**
* Transform the request before sending it. This method allows for extra request configuration.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,10 @@ private[akka] final case class RequestBuilderImpl[R](
timeout,
request,
(res: HttpResponse, bytes: ByteString) => new StrictResponse[T](res, parse.apply(bytes.toArray)))

override def addQueryParameter(key: String, value: String): RequestBuilder[R] = {
val query = request.getUri.query().withParam(key, value)
val uriWithQuery = request.getUri.query(query)
withRequest(request.withUri(uriWithQuery))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2021-2024 Lightbend Inc. <https://www.lightbend.com>
*/

package akka.javasdk.impl.http

import akka.actor.typed.ActorSystem
import akka.actor.typed.scaladsl.Behaviors
import akka.http.javadsl.model.HttpHeader
import akka.javasdk.http.RequestBuilder
import akka.util.ByteString
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

class HttpClientImplSpec extends AnyWordSpec with Matchers {
val system: ActorSystem[Nothing] = ActorSystem[Nothing](Behaviors.empty[Nothing], "httpClient")

"RequestBuilderImpl" should {
"add query parameter" in new HttpClientImplSuite {
val builder: RequestBuilderImpl[ByteString] = get { builder =>
builder
.addQueryParameter("key", "some value")
.addQueryParameter("another", "name")
}
builder.request.getUri.toString shouldBe "http://test.com/test?key=some+value&another=name"
}
}

}

trait HttpClientImplSuite {
implicit private val system: ActorSystem[Nothing] =
ActorSystem[Nothing](Behaviors.empty[Nothing], "RequestBuilderImplSpec")

val baseUrl = "http://test.com"
val path = "/test"
val headers: Seq[HttpHeader] = Seq.empty

private def client = new HttpClientImpl(system, baseUrl, headers)

private def get(url: String)(
builder: RequestBuilder[ByteString] => RequestBuilder[ByteString]): RequestBuilderImpl[ByteString] = {
builder(client.GET(url))
.asInstanceOf[RequestBuilderImpl[ByteString]]
}
def get(builder: RequestBuilder[ByteString] => RequestBuilder[ByteString]): RequestBuilderImpl[ByteString] =
get(path)(builder)
}

0 comments on commit 54e5b2a

Please sign in to comment.