Skip to content

Commit

Permalink
Move BaseUri middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz committed Jan 22, 2025
1 parent be9f9b9 commit 5ac4920
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 39 deletions.
35 changes: 1 addition & 34 deletions modules/core/src/main/scala/playground/OperationRunner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ import cats.data.NonEmptyList
import cats.effect.Async
import cats.effect.MonadCancelThrow
import cats.effect.Resource
import cats.effect.implicits.*
import cats.effect.std
import cats.syntax.all.*
import fs2.compression.Compression
import org.http4s.Uri
import org.http4s.client.Client
import playground.plugins.PlaygroundPlugin
import playground.plugins.SimpleHttpBuilder
Expand Down Expand Up @@ -117,39 +115,15 @@ object OperationRunner {

}

// https://github.com/kubukoz/smithy-playground/issues/158
def dynamicBaseUri[F[_]: MonadCancelThrow](
getUri: F[Uri]
): Client[F] => Client[F] =
client =>
Client[F] { req =>
getUri.toResource.flatMap { uri =>
client.run(
req.withUri(
req
.uri
.copy(
scheme = uri.scheme,
authority = uri.authority,
// prefixing with uri.path
path = uri.path.addSegments(req.uri.path.segments),
)
)
)
}
}

def forSchemaIndex[F[_]: StdlibRuntime: Async: Compression: std.Console](
dsi: DynamicSchemaIndex,
client: Client[F],
baseUri: F[Uri],
awsEnv: Resource[F, AwsEnvironment[F]],
plugins: List[PlaygroundPlugin],
): Map[QualifiedIdentifier, Resolver[F]] = forServices(
services = dsi.allServices.toList,
getSchema = dsi.getSchema,
client = client,
baseUri = baseUri,
awsEnv = awsEnv,
plugins = plugins,
)
Expand All @@ -158,7 +132,6 @@ object OperationRunner {
services: List[DynamicSchemaIndex.ServiceWrapper],
getSchema: ShapeId => Option[Schema[?]],
client: Client[F],
baseUri: F[Uri],
awsEnv: Resource[F, AwsEnvironment[F]],
plugins: List[PlaygroundPlugin],
): Map[QualifiedIdentifier, Resolver[F]] =
Expand All @@ -167,7 +140,6 @@ object OperationRunner {
OperationRunner.forService[svc.Alg, F](
svc.service,
client,
baseUri,
awsEnv,
getSchema,
plugins,
Expand Down Expand Up @@ -201,7 +173,6 @@ object OperationRunner {
def forService[Alg[_[_, _, _, _, _]], F[_]: StdlibRuntime: Async: Compression: std.Console](
service: Service[Alg],
client: Client[F],
baseUri: F[Uri],
awsEnv: Resource[F, AwsEnvironment[F]],
schemaIndex: ShapeId => Option[Schema[?]],
plugins: List[PlaygroundPlugin],
Expand All @@ -224,11 +195,7 @@ object OperationRunner {
builder
.client(
service,
dynamicBaseUri[F](
baseUri.flatTap { uri =>
std.Console[F].println(s"Using base URI: $uri")
}
).apply(client),
client,
)
.leftMap(e => Issue.InvalidProtocol(e.protocolTag.id, serviceProtocols))
.map(service.toPolyFunction(_))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ object DiagnosticProviderTests extends SimpleIOSuite {
services = services,
getSchema = _ => None,
client = client,
baseUri = IO.stub,
awsEnv = Resource.eval(IO.stub: IO[AwsEnvironment[IO]]),
plugins = Nil,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import cats.effect.kernel.Async
import cats.syntax.all.*
import cats.tagless.Derive
import cats.tagless.FunctorK
import cats.tagless.catsTaglessApplyKForIdK
import cats.tagless.implicits.*
import cats.~>
import com.google.gson.JsonElement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import cats.parse.LocationMap
import cats.syntax.all.*
import cats.tagless.Derive
import cats.tagless.FunctorK
import cats.tagless.catsTaglessApplyKForIdK
import cats.tagless.implicits.*
import cats.~>
import com.google.gson.JsonElement
Expand Down
24 changes: 22 additions & 2 deletions modules/lsp/src/main/scala/playground/lsp/ServerBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package playground.lsp

import cats.effect.implicits.*
import cats.effect.kernel.Async
import cats.effect.kernel.MonadCancelThrow
import cats.effect.kernel.Resource
import cats.effect.std
import cats.syntax.all.*
Expand Down Expand Up @@ -45,6 +46,7 @@ object ServerBuilder {
val makeClient = EmberClientBuilder
.default[F]
.build
.map(middleware.BaseUri[F])
.map(middleware.AuthorizationHeader[F])
.map(
Logger[F](
Expand Down Expand Up @@ -79,7 +81,6 @@ object ServerBuilder {
.forSchemaIndex[F](
dsi = dsi,
client = client,
baseUri = LanguageClient[F].configuration(ConfigurationValue.baseUri),
awsEnv = awsEnv,
plugins = plugins,
)
Expand All @@ -98,7 +99,26 @@ object ServerBuilder {

private object middleware {

def AuthorizationHeader[F[_]: Async: LanguageClient]: Client[F] => Client[F] =
def BaseUri[F[_]: LanguageClient: MonadCancelThrow]: Client[F] => Client[F] =
client =>
Client[F] { req =>
LanguageClient[F].configuration(ConfigurationValue.baseUri).toResource.flatMap { uri =>
client.run(
req.withUri(
req
.uri
.copy(
scheme = uri.scheme,
authority = uri.authority,
// prefixing with uri.path
path = uri.path.addSegments(req.uri.path.segments),
)
)
)
}
}

def AuthorizationHeader[F[_]: LanguageClient: MonadCancelThrow]: Client[F] => Client[F] =
client =>
Client[F] { request =>
val updatedRequest =
Expand Down

0 comments on commit 5ac4920

Please sign in to comment.