Skip to content

Commit

Permalink
Merge pull request #136 from buildo/api-135-add_prefix_to_client
Browse files Browse the repository at this point in the history
#135: Add prefix to client requests (closes #135)
  • Loading branch information
danielegallingani authored Sep 19, 2018
2 parents 011d3f5 + dee6521 commit 8f386aa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
3 changes: 2 additions & 1 deletion wiro/clientAkkaHttp/src/main/scala/RPCClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ trait RPCClientContext[T] extends MetaDataMacro with PathMacro {

class RPCClient(
config: Config,
prefix: Option[String] = None,
ctx: RPCClientContext[_]
)(implicit
system: ActorSystem,
materializer: ActorMaterializer,
executionContext: ExecutionContext
) extends autowire.Client[Json, WiroDecoder, Encoder] {
private[wiro] val requestBuilder = new RequestBuilder(config, ctx)
private[wiro] val requestBuilder = new RequestBuilder(config, prefix, ctx)

def write[Result: Encoder](r: Result): Json = r.asJson

Expand Down
19 changes: 14 additions & 5 deletions wiro/clientAkkaHttp/src/main/scala/RequestBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import cats.syntax.either._

class RequestBuilder(
config: Config,
prefix: Option[String],
ctx: RPCClientContext[_]
) {
def build(path : Seq[String], args: Map[String, Json]): HttpRequest = {
Expand All @@ -28,7 +29,7 @@ class RequestBuilder(
val (headersArgs, remainingArgs) = splitHeaderArgs(nonTokenArgs)
val tokenHeader = handleAuth(tokenArgs.values.toList)
val headers = handleHeaders(headersArgs.values.toList) ++ tokenHeader
val uri = buildUri(operationName)
val uri = buildUri(operationName, prefix)
val httpRequest = methodMetaData.operationType match {
case _: OperationType.Command => commandHttpRequest(remainingArgs, uri)
case _: OperationType.Query => queryHttpRequest(remainingArgs, uri)
Expand All @@ -37,10 +38,18 @@ class RequestBuilder(
httpRequest.withHeaders(headers)
}

private[this] def buildUri(operationName: String) = Uri(
scheme = "http", path = Path / ctx.path / operationName,
authority = Authority(host = Host(config.host), port = config.port)
)
private[this] def buildUri(operationName: String, prefix: Option[String]) = {
val path = prefix match {
case None => Path / ctx.path / operationName
case Some(prefix) => Path / prefix /ctx.path / operationName
}

Uri(
scheme = "http",
path = path,
authority = Authority(host = Host(config.host), port = config.port)
)
}

private[this] def splitTokenArgs(args: Map[String, Json]): (Map[String, Json], Map[String, Json]) =
args.partition { case (_, value) => value.as[wiro.Auth].isRight }
Expand Down
10 changes: 6 additions & 4 deletions wiro/clientAkkaHttp/src/test/scala/RPCTestClient.scala
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package wiro

import akka.http.scaladsl.model.HttpRequest
import akka.http.scaladsl.server.Route
import akka.http.scaladsl.testkit.{ RouteTest, TestFrameworkInterface }
import akka.http.scaladsl.server._
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.testkit._

import cats.implicits._
import client.akkaHttp.{ RPCClient, RPCClientContext }

import scala.concurrent.Future

trait RPCRouteTest extends RouteTest { this: TestFrameworkInterface =>
val prefix = Some("test")
class RPCClientTest(ctx: RPCClientContext[_], route: Route)
extends RPCClient(config = Config("localhost", 80), ctx = ctx){
extends RPCClient(config = Config("localhost", 80), prefix = prefix, ctx = ctx){
override private[wiro] def doHttpRequest(request: HttpRequest) =
(request ~> route).response.pure[Future]
(request ~> pathPrefix(prefix.get)(route)).response.pure[Future]
}
}

0 comments on commit 8f386aa

Please sign in to comment.