Skip to content
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

Add support for dumping request and response in Go generated clients #4566

Merged
merged 3 commits into from
Nov 28, 2019

Conversation

dnaeon
Copy link
Contributor

@dnaeon dnaeon commented Nov 21, 2019

The following change adds a new configuration setting, which
controls whether clients want to dump the HTTP request and response.

Useful when debugging API calls and clients.

PR checklist

  • Read the contribution guidelines.
  • If contributing template-only or documentation-only changes which will change sample output, build the project before.
  • Run the shell script(s) under ./bin/ (or Windows batch scripts under.\bin\windows) to update Petstore samples related to your fix. This is important, as CI jobs will verify all generator outputs of your HEAD commit, and these must match the expectations made by your contribution. You only need to run ./bin/{LANG}-petstore.sh, ./bin/openapi3/{LANG}-petstore.sh if updating the code or mustache templates for a language ({LANG}) (e.g. php, ruby, python, etc).
  • File the PR against the correct branch: master, 4.3.x, 5.0.x. Default: master.
  • Copy the technical committee to review the pull request if your PR is targeting a particular programming language.

The following change adds a new configuration setting, which
controls whether clients want to dump the HTTP request and response.

Useful when debugging API calls and clients.
@dnaeon
Copy link
Contributor Author

dnaeon commented Nov 21, 2019

@wing328
Copy link
Member

wing328 commented Nov 21, 2019

@dnaeon thanks for the PR. What about using a logger (e.g. https://github.com/google/logger) instead?

@wing328 wing328 added this to the 4.2.2 milestone Nov 21, 2019
@dnaeon
Copy link
Contributor Author

dnaeon commented Nov 21, 2019

Hey @wing328,

I've tested it with a logger first, but the results weren't pretty, that's why I've left it using just fmt.Println.

@antihax
Copy link
Contributor

antihax commented Nov 21, 2019

This should really be done with middleware, not in the client directly. Is there a reason it cant be?

@wing328
Copy link
Member

wing328 commented Nov 21, 2019

I've tested it with a logger first, but the results weren't pretty, that's why I've left it using just fmt.Println.

Can you please elaborate?

@wing328
Copy link
Member

wing328 commented Nov 21, 2019

FYI. For other clients (e.g. Ruby), we also use a logger for debugging purposes.

@dnaeon
Copy link
Contributor Author

dnaeon commented Nov 21, 2019

I've tested it with a logger first, but the results weren't pretty, that's why I've left it using just fmt.Println.

Can you please elaborate?

@wing328 Here's one example.

https://play.golang.org/p/o5LE9R5iCpj

Basically the timestamp of the logger is attached to just the first line of the output, which makes it less pretty, IMO.

Also, I found fmt.Println to be less distractive for my use cases, but I can switch that over to a logger, if you think that's better.

This should really be done with middleware, not in the client directly. Is there a reason it cant be?

@antihax That should work I guess. Can you point me to an example middleware I can lookup?

Thanks!

@antihax
Copy link
Contributor

antihax commented Nov 21, 2019

Here is an example of two chained middleware for the API client.
Setup of the chain LimitedTransport and APICacheTransport.
https://github.com/antihax/evedata/blob/master/internal/apicache/apicache.go#L50

Here are the implementations:
https://github.com/antihax/evedata/blob/master/internal/apicache/limitedTransport.go
https://github.com/antihax/evedata/blob/master/internal/apicache/transport.go#L20

You would create another of these that would dump the request and response.

In the case here you would do something like this:

func (t *DumpTransport) RoundTrip(req *http.Request) (*http.Response, error) {
	reqb, _ := httputil.DumpRequest(req, true)
	res, err:= t.Transport.RoundTrip(req)
	resb, _ := httputil.DumpResponse(res, true)
        ... log the request and response ...
	return res, err
}

@wing328
Copy link
Member

wing328 commented Nov 22, 2019

Basically the timestamp of the logger is attached to just the first line of the output, which makes it less pretty, IMO.

What about putting a "\n" at the beginning of the log?

@dnaeon
Copy link
Contributor Author

dnaeon commented Nov 22, 2019

Here is an example of two chained middleware for the API client.
Setup of the chain LimitedTransport and APICacheTransport.
https://github.com/antihax/evedata/blob/master/internal/apicache/apicache.go#L50

Here are the implementations:
https://github.com/antihax/evedata/blob/master/internal/apicache/limitedTransport.go
https://github.com/antihax/evedata/blob/master/internal/apicache/transport.go#L20

You would create another of these that would dump the request and response.

In the case here you would do something like this:

func (t *DumpTransport) RoundTrip(req *http.Request) (*http.Response, error) {
	reqb, _ := httputil.DumpRequest(req, true)
	res, err:= t.Transport.RoundTrip(req)
	resb, _ := httputil.DumpResponse(res, true)
        ... log the request and response ...
	return res, err
}

@antihax Thanks, that would work.

Basically the timestamp of the logger is attached to just the first line of the output, which makes it less pretty, IMO.

What about putting a "\n" at the beginning of the log?

@antihax, @wing328 I'll leave it to you whether it's worth merging this PR considering that we can have the same thing done with either a middleware or custom roundtripper.

@wing328
Copy link
Member

wing328 commented Nov 23, 2019

@dnaeon the debug flag in other clients can be used not only for dumping request/response but also other troubleshooting (e.g. object serialization, etc).

so if you're using a logger with the debug flag, I'll be happy to merge this into master.

@dnaeon
Copy link
Contributor Author

dnaeon commented Nov 23, 2019

@dnaeon the debug flag in other clients can be used not only for dumping request/response but also other troubleshooting (e.g. object serialization, etc).

so if you're using a logger with the debug flag, I'll be happy to merge this into master.

@wing328, I can take care of updating the PR.

Which logger you'd prefer to have as part of the implementation?

https://github.com/google/logger or the builtin log.Logger?

@wing328
Copy link
Member

wing328 commented Nov 23, 2019 via email

@dnaeon
Copy link
Contributor Author

dnaeon commented Nov 23, 2019

@wing328 just pushed a new commit which uses log.Logger.

@wing328
Copy link
Member

wing328 commented Nov 28, 2019

@dnaeon thanks for the PR, which looks good to me.

@wing328 wing328 merged commit df682ab into OpenAPITools:master Nov 28, 2019
@wing328
Copy link
Member

wing328 commented Nov 28, 2019

@dnaeon when you've time, I wonder if you can file the same PR for go-experimental generator as well. Thank you.

@dnaeon
Copy link
Contributor Author

dnaeon commented Nov 29, 2019

@wing328 Sure, here it is #4649

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants