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

Proposal: Add Context() method into Request and consider logging prerequesthook updates #206

Closed
clippit opened this issue Jan 3, 2019 · 4 comments
Assignees

Comments

@clippit
Copy link

clippit commented Jan 3, 2019

Hi,

Firstly thank you for creating such a great tool! 😄

I find it's inconvenient for passing user custom parameters when using user-defined middleware. For example, in our development process, the server may receive an X-Request-ID header and it should pass this header to any services it calls. That's a simple way for tracing RPC calling actually. Besides of this, we also have some requirements for generating signature per request.

For current version of resty, the only way to approach this is using SetPreRequestHook and carrying user values with req.RawRequest.Context(), e.g:

client.SetPreRequestHook(func(c *resty.Client, req *resty.Request) error {
	requestId, ok := req.RawRequest.Context().Value(requestIdKey).(string)
	if ok && requestId != "" {
		req.SetHeader("x-request-id", requestId)
	}

	authKey, ok := req.RawRequest.Context().Value(authKey).(string)
	if !ok {
		return errors.New("no auth key in request context")
	}

	// ... grab request info to generate signature...
	req.SetHeader("x-signature", "...")

	return nil
})

When using this client, we need a nested context to store these values:

ctx := context.WithValue(context.Background(), requestIdKey, "from_current_server_request_header")
ctx = context.WithValue(ctx, authKey, "may_returned_by_another_service")
client.R().SetContext(ctx).Get("/")

We can only use SetPreRequestHook rather than OnBeforeRequest because RawRequest is created after all OnBeforeRequest middlewares and the context of RawRequest if the only place that can carry these values. But there are some shortcoming:

  1. The headers added in SetPreRequestHook cannot be printed when debug is on since requestLogger happens before this hook
  2. It's not so good to create so many context

So here is my modest proposal. A GetContext() method can be added into Request so we can put these codes in OnBeforeRequest and make request logger prints completed headers. Furthermore, the Request may contain a map[string]interface{} registry to store any kind of user values to get more abilities.

@jeevatkm jeevatkm self-assigned this Jan 4, 2019
@jeevatkm jeevatkm added this to the v1.11.0 Milestone milestone Jan 4, 2019
@jeevatkm
Copy link
Member

jeevatkm commented Jan 4, 2019

@clippit Thanks for bringing up your concerns and your appreciations. I understand your suggestions.

I will bring following updates:

  • Expose context via Request.Context()
  • Move request logger after PreRequestHook call

@jeevatkm jeevatkm changed the title Proposal: Add GetContext() method or other ways for accessing user value in hooks Proposal: Add Context() method into Request and consider logging prerequesthook updates Jan 4, 2019
@jeevatkm
Copy link
Member

jeevatkm commented Jan 4, 2019

@clippit I have implemented in branch master. Could you try and let me know?

@clippit
Copy link
Author

clippit commented Jan 4, 2019

It's good to me:) and I have added some checks, please take a look at #207

@jeevatkm
Copy link
Member

jeevatkm commented Jan 4, 2019

@clippit Thanks for the confirmation 😄 I have responded to your PR please have a look.

@jeevatkm jeevatkm closed this as completed Jan 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants