Skip to content

Commit

Permalink
Support authorization headers
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz committed Jul 22, 2022
1 parent 8f71b88 commit f994975
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/src/main/scala/playground/NodeEncoderVisitor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ object NodeEncoderVisitor extends SchemaVisitor[NodeEncoder] { self =>
case PBoolean => boolean
case PBigDecimal => bigdecimal
case PBlob => string.contramap(_.toString) // todo this only works for UTF-8 text
case PDouble => unsupported("double")
case PDouble => int.contramap(_.toInt) // todo: wraps
case PDocument => document
case PFloat => unsupported("float")
case PUnit =>
Expand Down
5 changes: 5 additions & 0 deletions vscode-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
"format": "uri",
"description": "The base URL of the HTTP server to use for running queries.",
"default": "http://localhost:8080"
},
"smithyql.http.authorizationHeader": {
"type": "string",
"description": "The value of the Authorization header to use for running queries. Empty/whitespace strings will result in no header.",
"default": ""
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions vscode-extension/src/main/scala/playground/client.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import fs2.io.net.tls.TLSContext
import fs2.io.net.tls.SecureContext
import org.http4s.client.middleware.Logger
import cats.effect.std
import cats.effect.kernel.Sync
import cats.implicits._
import org.http4s.headers.Authorization
import scala.concurrent.duration._

object client {

Expand Down Expand Up @@ -53,6 +57,7 @@ object client {
.flatMap { tls =>
EmberClientBuilder.default[F].withTLSContext(tls).build
}
.map(AuthorizationHeader[F])
.map(
Logger[F](
logHeaders = true,
Expand All @@ -61,4 +66,20 @@ object client {
)
)

private def AuthorizationHeader[F[_]: Async]: Client[F] => Client[F] =
client =>
Client[F] { request =>
val updatedRequest =
vscodeutil
.getConfigF[F, String]("smithyql.http.authorizationHeader")
.flatMap {
case v if v.trim.isEmpty() => request.pure[F]
case v => Authorization.parse(v).liftTo[F].map(request.putHeaders(_))
}
.toResource

updatedRequest
.flatMap(client.run(_))
}

}

0 comments on commit f994975

Please sign in to comment.