Created
November 6, 2020 12:38
-
-
Save GrafBlutwurst/2bc68aff07c742902188f82699ac4f2c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private def memLoadResponse( | |
response: Response[RIO[Clock, *]] | |
): RIO[Clock, (Response[RIO[Clock, *]], Response[RIO[Clock, *]])] = | |
response.body.compile | |
.to(fs2.Chunk) | |
.map(bodyBytes => | |
( | |
response.copy( | |
body = fs2.Stream.chunk(bodyBytes) | |
), | |
response.copy( | |
body = fs2.Stream.chunk(bodyBytes) | |
) | |
) | |
) | |
private def memoLoadRequest( | |
request: Request[RIO[Clock, *]] | |
) = | |
request.body.compile | |
.to(fs2.Chunk) | |
.map(bodyBytes => | |
( | |
Request[RIO[Clock, *]]( | |
request.method, | |
request.uri, | |
request.httpVersion, | |
request.headers, | |
fs2.Stream.chunk(bodyBytes), | |
request.attributes | |
), | |
Request[RIO[Clock, *]]( | |
request.method, | |
request.uri, | |
request.httpVersion, | |
request.headers, | |
fs2.Stream.chunk(bodyBytes), | |
request.attributes | |
) | |
) | |
) | |
private def generateLoggingClient( | |
client: Client[RIO[Clock, *]], | |
logger: Logger[RIO[Clock, *]], | |
tracingIdGenerator: RIO[Clock, ju.UUID] | |
): Client[RIO[Clock, ?]] = | |
Client { req => | |
Resource.liftF(memoLoadRequest(req)).flatMap { | |
case (req1, req2) => | |
client.run(req1).flatMap { resp => | |
for { | |
tracingId <- Resource.liftF(tracingIdGenerator) | |
respTpl <- Resource.liftF(memLoadResponse(resp)) | |
(resp1, resp2) = respTpl | |
_ <- Resource.liftF( | |
org.http4s.client.middleware.Logger | |
.logMessage[RIO[Clock, *], Request[RIO[Clock, *]]](req2)( | |
true, | |
true, | |
_ => false | |
)(msg => | |
for { | |
now <- UTCLocal.DateTime.now[RIO[Clock, *]]() | |
_ <- logger.info( | |
tech.rivero.edgeconnector.domain.cloud.LogMessage | |
.DANGERHttpLog( | |
now, | |
tracingId, | |
msg | |
) | |
) | |
} yield () | |
) | |
) | |
_ <- Resource.liftF( | |
org.http4s.client.middleware.Logger | |
.logMessage[RIO[Clock, *], Response[RIO[Clock, *]]](resp2)( | |
true, | |
true, | |
_ => false | |
)(msg => | |
for { | |
now <- UTCLocal.DateTime.now[RIO[Clock, *]]() | |
_ <- logger.info( | |
tech.rivero.edgeconnector.domain.cloud.LogMessage | |
.DANGERHttpLog( | |
now, | |
tracingId, | |
msg | |
) | |
) | |
} yield () | |
) | |
) | |
} yield resp1 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment