-
Notifications
You must be signed in to change notification settings - Fork 418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added curl logger client aspect. #3285
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -492,4 +492,104 @@ object ZClientAspect { | |||||
}, | ||||||
) | ||||||
} | ||||||
|
||||||
/** | ||||||
* Client aspect that logs details of web request as curl command | ||||||
*/ | ||||||
final def curlLogger( | ||||||
verbose: Boolean = true, | ||||||
logEffect: String => UIO[Unit] = (m: String) => ZIO.log(m), | ||||||
)(implicit trace: Trace): ZClientAspect[Nothing, Any, Nothing, Body, Nothing, Any, Nothing, Response] = | ||||||
new ZClientAspect[Nothing, Any, Nothing, Body, Nothing, Any, Nothing, Response] { | ||||||
|
||||||
def formatCurlCommand( | ||||||
version: Version, | ||||||
method: Method, | ||||||
url: URL, | ||||||
headers: Headers, | ||||||
body: Body, | ||||||
proxy: Option[Proxy], | ||||||
): String = { | ||||||
val versionOpt = version match { | ||||||
case Version.Default => Chunk.empty | ||||||
case Version.Http_1_0 => Chunk("--http1.0") | ||||||
case Version.Http_1_1 => Chunk("--http1.1") | ||||||
} | ||||||
val verboseOpt = if (verbose) Chunk("--verbose") else Chunk.empty | ||||||
val requestOpt = method match { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be simplified to
Suggested change
|
||||||
case Method.GET => Chunk("--request GET") | ||||||
case Method.POST => Chunk("--request POST") | ||||||
case Method.PUT => Chunk("--request PUT") | ||||||
case Method.DELETE => Chunk("--request DELETE") | ||||||
case Method.PATCH => Chunk("--request PATCH") | ||||||
case Method.HEAD => Chunk("--request HEAD") | ||||||
case Method.OPTIONS => Chunk("--request OPTIONS") | ||||||
case Method.CONNECT => Chunk("--request CONNECT") | ||||||
case Method.TRACE => Chunk("--request TRACE") | ||||||
case Method.CUSTOM(name) => Chunk(s"--request $name") | ||||||
case Method.ANY => Chunk("--request GET") | ||||||
} | ||||||
val headerOpt = Chunk.fromIterable(headers.map(h => s"--header '${h.headerName}:${h.renderedValue}'")) | ||||||
val bodyOpt = body match { | ||||||
case Body.empty => Chunk.empty | ||||||
case body => { | ||||||
Chunk(s"--data '${body.asString.map(_.replace("'", "'\\''"))}'") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. body.asString returns an effect. Printing an effect is not what you want. |
||||||
} | ||||||
} | ||||||
val proxyOpt = proxy match { | ||||||
case Some(proxy) => | ||||||
Chunk( | ||||||
s"--proxy '${proxy.url}'" + | ||||||
proxy.credentials.map(c => s" --proxy-user '${c.uname}:${c.upassword}'") + | ||||||
proxy.headers.map(h => s" --proxy-header '${h.headerName}:${h.renderedValue}'").mkString(" "), | ||||||
) | ||||||
case None => Chunk.empty | ||||||
} | ||||||
( | ||||||
Chunk("curl") ++ | ||||||
verboseOpt ++ | ||||||
requestOpt ++ | ||||||
headerOpt ++ | ||||||
versionOpt ++ | ||||||
proxyOpt ++ | ||||||
bodyOpt ++ | ||||||
Chunk.single("'" + url.encode + "'") | ||||||
).mkString(" \\\n ") | ||||||
} | ||||||
|
||||||
override def apply[ | ||||||
ReqEnv, | ||||||
Env >: Nothing <: Any, | ||||||
In >: Nothing <: Body, | ||||||
Err >: Nothing <: Any, | ||||||
Out >: Nothing <: Response, | ||||||
]( | ||||||
client: ZClient[Env, ReqEnv, In, Err, Out], | ||||||
): ZClient[Env, ReqEnv, In, Err, Out] = { | ||||||
val oldDriver = client.driver | ||||||
|
||||||
val newDriver = new ZClient.Driver[Env, ReqEnv, Err] { | ||||||
override def request( | ||||||
version: Version, | ||||||
method: Method, | ||||||
url: URL, | ||||||
headers: Headers, | ||||||
body: Body, | ||||||
sslConfig: Option[ClientSSLConfig], | ||||||
proxy: Option[Proxy], | ||||||
)(implicit trace: Trace): ZIO[Env & ReqEnv, Err, Response] = | ||||||
logEffect(formatCurlCommand(version, method, url, headers, body, proxy)) *> | ||||||
oldDriver.request(version, method, url, headers, body, sslConfig, proxy) | ||||||
|
||||||
override def socket[Env1 <: Env](version: Version, url: URL, headers: Headers, app: WebSocketApp[Env1])( | ||||||
implicit | ||||||
trace: Trace, | ||||||
ev: ReqEnv =:= Scope, | ||||||
): ZIO[Env1 & ReqEnv, Err, Response] = | ||||||
client.driver.socket(version, url, headers, app) | ||||||
} | ||||||
|
||||||
client.transform(client.bodyEncoder, client.bodyDecoder, newDriver) | ||||||
} | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@landlockedsurfer Do you think we could put this capacity to transform a Request to a
curl
String as a method ofRequest
and use this method here?That'd allow people to "toString" their requests in a nice way if they need to (to log it at any moment, for example)