{"action":"created","issue":{"url":"https://api.github.com/repos/go-resty/resty/issues/206","repository_url":"https://api.github.com/repos/go-resty/resty","labels_url":"https://api.github.com/repos/go-resty/resty/issues/206/labels{/name}","comments_url":"https://api.github.com/repos/go-resty/resty/issues/206/comments","events_url":"https://api.github.com/repos/go-resty/resty/issues/206/events","html_url":"https://github.com/go-resty/resty/issues/206","id":395516282,"node_id":"MDU6SXNzdWUzOTU1MTYyODI=","number":206,"title":"Proposal: Add GetContext() method or other ways for accessing user value in hooks","user":{"login":"clippit","id":343973,"node_id":"MDQ6VXNlcjM0Mzk3Mw==","avatar_url":"https://avatars0.githubusercontent.com/u/343973?v=4","gravatar_id":"","url":"https://api.github.com/users/clippit","html_url":"https://github.com/clippit","followers_url":"https://api.github.com/users/clippit/followers","following_url":"https://api.github.com/users/clippit/following{/other_user}","gists_url":"https://api.github.com/users/clippit/gists{/gist_id}","starred_url":"https://api.github.com/users/clippit/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/clippit/subscriptions","organizations_url":"https://api.github.com/users/clippit/orgs","repos_url":"https://api.github.com/users/clippit/repos","events_url":"https://api.github.com/users/clippit/events{/privacy}","received_events_url":"https://api.github.com/users/clippit/received_events","type":"User","site_admin":false},"labels":[{"id":253847034,"node_id":"MDU6TGFiZWwyNTM4NDcwMzQ=","url":"https://api.github.com/repos/go-resty/resty/labels/enhancement","name":"enhancement","color":"84b6eb","default":true}],"state":"open","locked":false,"assignee":{"login":"jeevatkm","id":1175977,"node_id":"MDQ6VXNlcjExNzU5Nzc=","avatar_url":"https://avatars3.githubusercontent.com/u/1175977?v=4","gravatar_id":"","url":"https://api.github.com/users/jeevatkm","html_url":"https://github.com/jeevatkm","followers_url":"https://api.github.com/users/jeevatkm/followers","following_url":"https://api.github.com/users/jeevatkm/following{/other_user}","gists_url":"https://api.github.com/users/jeevatkm/gists{/gist_id}","starred_url":"https://api.github.com/users/jeevatkm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jeevatkm/subscriptions","organizations_url":"https://api.github.com/users/jeevatkm/orgs","repos_url":"https://api.github.com/users/jeevatkm/repos","events_url":"https://api.github.com/users/jeevatkm/events{/privacy}","received_events_url":"https://api.github.com/users/jeevatkm/received_events","type":"User","site_admin":false},"assignees":[{"login":"jeevatkm","id":1175977,"node_id":"MDQ6VXNlcjExNzU5Nzc=","avatar_url":"https://avatars3.githubusercontent.com/u/1175977?v=4","gravatar_id":"","url":"https://api.github.com/users/jeevatkm","html_url":"https://github.com/jeevatkm","followers_url":"https://api.github.com/users/jeevatkm/followers","following_url":"https://api.github.com/users/jeevatkm/following{/other_user}","gists_url":"https://api.github.com/users/jeevatkm/gists{/gist_id}","starred_url":"https://api.github.com/users/jeevatkm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jeevatkm/subscriptions","organizations_url":"https://api.github.com/users/jeevatkm/orgs","repos_url":"https://api.github.com/users/jeevatkm/repos","events_url":"https://api.github.com/users/jeevatkm/events{/privacy}","received_events_url":"https://api.github.com/users/jeevatkm/received_events","type":"User","site_admin":false}],"milestone":{"url":"https://api.github.com/repos/go-resty/resty/milestones/25","html_url":"https://github.com/go-resty/resty/milestone/25","labels_url":"https://api.github.com/repos/go-resty/resty/milestones/25/labels","id":3717917,"node_id":"MDk6TWlsZXN0b25lMzcxNzkxNw==","number":25,"title":"v1.11.0 Milestone","description":"","creator":{"login":"jeevatkm","id":1175977,"node_id":"MDQ6VXNlcjExNzU5Nzc=","avatar_url":"https://avatars3.githubusercontent.com/u/1175977?v=4","gravatar_id":"","url":"https://api.github.com/users/jeevatkm","html_url":"https://github.com/jeevatkm","followers_url":"https://api.github.com/users/jeevatkm/followers","following_url":"https://api.github.com/users/jeevatkm/following{/other_user}","gists_url":"https://api.github.com/users/jeevatkm/gists{/gist_id}","starred_url":"https://api.github.com/users/jeevatkm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jeevatkm/subscriptions","organizations_url":"https://api.github.com/users/jeevatkm/orgs","repos_url":"https://api.github.com/users/jeevatkm/repos","events_url":"https://api.github.com/users/jeevatkm/events{/privacy}","received_events_url":"https://api.github.com/users/jeevatkm/received_events","type":"User","site_admin":false},"open_issues":1,"closed_issues":1,"state":"open","created_at":"2018-10-07T18:40:22Z","updated_at":"2019-01-04T01:49:10Z","due_on":null,"closed_at":null},"comments":0,"created_at":"2019-01-03T10:08:25Z","updated_at":"2019-01-04T01:49:17Z","closed_at":null,"author_association":"NONE","body":"Hi,\r\n\r\nFirstly thank you for creating such a great tool! 😄 \r\n\r\nI 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.\r\n\r\nFor current version of resty, the only way to approach this is using `SetPreRequestHook` and carrying user values with `req.RawRequest.Context()`, e.g:\r\n\r\n```\r\nclient.SetPreRequestHook(func(c *resty.Client, req *resty.Request) error {\r\n\trequestId, ok := req.RawRequest.Context().Value(requestIdKey).(string)\r\n\tif ok && requestId != \"\" {\r\n\t\treq.SetHeader(\"x-request-id\", requestId)\r\n\t}\r\n\r\n\tauthKey, ok := req.RawRequest.Context().Value(authKey).(string)\r\n\tif !ok {\r\n\t\treturn errors.New(\"no auth key in request context\")\r\n\t}\r\n\r\n\t// ... grab request info to generate signature...\r\n\treq.SetHeader(\"x-signature\", \"...\")\r\n\r\n\treturn nil\r\n})\r\n```\r\n\r\nWhen using this client, we need a nested context to store these values:\r\n\r\n```\r\nctx := context.WithValue(context.Background(), requestIdKey, \"from_current_server_request_header\")\r\nctx = context.WithValue(ctx, authKey, \"may_returned_by_another_service\")\r\nclient.R().SetContext(ctx).Get(\"/\")\r\n```\r\n\r\nWe 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:\r\n\r\n1. The headers added in `SetPreRequestHook` cannot be printed when debug is on since `requestLogger` happens before this hook\r\n2. It's not so good to create so many context\r\n\r\nSo 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.\r\n"},"comment":{"url":"https://api.github.com/repos/go-resty/resty/issues/comments/451333622","html_url":"https://github.com/go-resty/resty/issues/206#issuecomment-451333622","issue_url":"https://api.github.com/repos/go-resty/resty/issues/206","id":451333622,"node_id":"MDEyOklzc3VlQ29tbWVudDQ1MTMzMzYyMg==","user":{"login":"jeevatkm","id":1175977,"node_id":"MDQ6VXNlcjExNzU5Nzc=","avatar_url":"https://avatars3.githubusercontent.com/u/1175977?v=4","gravatar_id":"","url":"https://api.github.com/users/jeevatkm","html_url":"https://github.com/jeevatkm","followers_url":"https://api.github.com/users/jeevatkm/followers","following_url":"https://api.github.com/users/jeevatkm/following{/other_user}","gists_url":"https://api.github.com/users/jeevatkm/gists{/gist_id}","starred_url":"https://api.github.com/users/jeevatkm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jeevatkm/subscriptions","organizations_url":"https://api.github.com/users/jeevatkm/orgs","repos_url":"https://api.github.com/users/jeevatkm/repos","events_url":"https://api.github.com/users/jeevatkm/events{/privacy}","received_events_url":"https://api.github.com/users/jeevatkm/received_events","type":"User","site_admin":false},"created_at":"2019-01-04T01:49:17Z","updated_at":"2019-01-04T01:49:17Z","author_association":"MEMBER","body":"@clippit Thanks for bringing up your concerns and your appreciations. I understand your suggestions.\r\n\r\nI will bring following updates:\r\n\r\n- [ ] Expose context via `Request.Context()`\r\n- [ ] Move request logger after `PreRequestHook` call"}}
{"actor":{"display_login":"jeevatkm"}}