Skip to content

Commit

Permalink
add token to request
Browse files Browse the repository at this point in the history
  • Loading branch information
ValdemarGr committed Sep 26, 2023
1 parent 072cbe6 commit 76d812c
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions client/src/main/scala/spice4s/client/SpiceClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ trait SpiceClient[F[_]] { self =>
}

object SpiceClient {
val AUTHORIZATION = "authorization"
val METADATA_KEY = Metadata.Key.of(AUTHORIZATION, Metadata.ASCII_STRING_MARSHALLER)

def fromChannel[F[_]: Async](channel: Channel, token: String): Resource[F, SpiceClient[F]] =
PermissionsServiceFs2Grpc
.clientResource[F, Unit](
channel,
{ _ =>
val m = new Metadata()
m.put(METADATA_KEY, s"Bearer ${token}")
m
}
)
.map(fromClient[F])

def fromClient[F[_]: MonadThrow](client: PermissionsServiceFs2Grpc[F, Unit]): SpiceClient[F] = {
def decodeWith[A, B](f: A => Decoded[B]): A => F[B] = a => raiseIn[F, B](f(a))

Expand All @@ -91,9 +106,4 @@ object SpiceClient {
client.lookupSubjects(x.encode, ()).evalMap(decodeWith(LookupSubjectsResponse.decode))
}
}

def fromChannel[F[_]: Async](channel: Channel): Resource[F, SpiceClient[F]] =
PermissionsServiceFs2Grpc
.clientResource[F, Unit](channel, _ => new Metadata())
.map(fromClient[F])
}

0 comments on commit 76d812c

Please sign in to comment.