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

Setting context for all request? #722

Open
renom opened this issue Oct 5, 2023 · 6 comments
Open

Setting context for all request? #722

renom opened this issue Oct 5, 2023 · 6 comments

Comments

@renom
Copy link

renom commented Oct 5, 2023

Add client.SetContext similar to client.SetHeader (Client method)

@jeevatkm
Copy link
Member

jeevatkm commented Oct 8, 2023

@renom Could you explain your request in detail along with some samples, please?
I need more confidence about using the same instance of Context in all the requests.

@jeevatkm
Copy link
Member

@renom Any feedback?

@renom
Copy link
Author

renom commented Oct 16, 2023

@jeevatkm thanks for the response.
I thought about initializing the client once when multiple requests are sent further in the same code block.
Or setting the context when the client is passed to another service as a func argument, to avoid passing the context separately.
If you still need code examples, I can make them a little bit later.

@renom
Copy link
Author

renom commented Oct 16, 2023

Case 1 (now):

func DoSomething(ctx context.Context, url1, url2, url3 string) (err error) {
	client := resty.New()
	_, err = client.R().SetContext(ctx).Get(url1)
	if err != nil {
		return err
	}
	_, err = client.R().SetContext(ctx).Get(url2)
	if err != nil {
		return err
	}
	_, err = client.R().SetContext(ctx).Get(url3)
	if err != nil {
		return err
	}
	return nil
}

Case 1 (updated):

func DoSomething(ctx context.Context, url1, url2, url3 string) (err error) {
	client := resty.New().SetContext(ctx)
	_, err = client.R().Get(url1)
	if err != nil {
		return err
	}
	_, err = client.R().Get(url2)
	if err != nil {
		return err
	}
	_, err = client.R().Get(url3)
	if err != nil {
		return err
	}
	return nil
}

@renom
Copy link
Author

renom commented Oct 16, 2023

Case 2 (now):

func DoSomething(client *resty.Client, ctx context.Context, url1, url2, url3 string) (err error) {
	_, err = client.R().SetContext(ctx).Get(url1)
	if err != nil {
		return err
	}
	_, err = client.R().SetContext(ctx).Get(url2)
	if err != nil {
		return err
	}
	_, err = client.R().SetContext(ctx).Get(url3)
	if err != nil {
		return err
	}
	return nil
}

Case 2 (updated):

func DoSomething(client *resty.Client, url1, url2, url3 string) (err error) {
	_, err = client.R().Get(url1)
	if err != nil {
		return err
	}
	_, err = client.R().Get(url2)
	if err != nil {
		return err
	}
	_, err = client.R().Get(url3)
	if err != nil {
		return err
	}
	return nil
}

We can still pass the context:

err := DoSomething(client.SetContext(ctx), url1, url2, url3)

@jeevatkm
Copy link
Member

jeevatkm commented Mar 2, 2024

@renom Thank you for the code snippets to explain the use case flow.

@jeevatkm jeevatkm added this to the v3.0.0 Milestone milestone Mar 2, 2024
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