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 request Scopes api, user can add parameters dynamically. #462

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

thinkgos
Copy link

I want the Scopes api, which could be used to add parameters dynamically. It will be more flexible.

func TransferContentType(r *resty.Request) *resty.Request {
	return r.SetHeader("Content-Type", "application/json").
		SetHeader("Accept", "application/json")
}

func PageParam(page, size int) func(r *resty.Request) *resty.Request {
	return func(r *resty.Request) *resty.Request {
		return r.SetQueryParam("page", strconv.FormatInt(int64(page), 10)).
			SetQueryParam("size", strconv.FormatInt(int64(size), 10))
	}
}

func main() {
	client := resty.New()

	client.Scopes(TransferContentType, PageParam(1, 100)).
		Get("https://localhost:8080/bar")
}

@codecov
Copy link

codecov bot commented Sep 17, 2021

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.68%. Comparing base (1792d62) to head (ed87974).
Report is 92 commits behind head on main.

❗ Current head ed87974 differs from pull request most recent head ed1f2ea. Consider uploading reports for the commit ed1f2ea to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #462      +/-   ##
==========================================
+ Coverage   96.67%   96.68%   +0.01%     
==========================================
  Files          10       10              
  Lines        1324     1328       +4     
==========================================
+ Hits         1280     1284       +4     
  Misses         26       26              
  Partials       18       18              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@thinkgos thinkgos changed the title Add request Scopes api, the user can add parameters dynamically. Add request Scopes api, user can add parameters dynamically. Sep 17, 2021
@jeevatkm
Copy link
Member

@thinkgos From my understanding, the mentioned use cases in the description could be achieved (& more) with resty request middleware. Can you help me understand, what are the differentiation factors?

@thinkgos
Copy link
Author

thinkgos commented Sep 20, 2021

@jeevatkm resty request middleware meet my needs, but it seems not flexible. middleware may used to manipulate every request object. Scopes API can reduce the same code and add parameters dynamically.

func TransferXmlContentType(r *resty.Request) *resty.Request {
	return r.SetHeader("Content-Type", "application/xml; charset=utf-8").
		SetHeader("Accept", "application/xml; charset=utf-8")
}

func TransferJSONContentType(r *resty.Request) *resty.Request {
	return r.SetHeader("Content-Type", "application/json").
		SetHeader("Accept", "application/json")
}

func PageParam(page, size int) func(r *resty.Request) *resty.Request {
	return func(r *resty.Request) *resty.Request {
		return r.SetQueryParam("page", strconv.FormatInt(int64(page), 10)).
			SetQueryParam("size", strconv.FormatInt(int64(size), 10))
	}
}

func main() {
	// Create a Resty Client
	client := resty.New()
	client.OnBeforeRequest(func(client *resty.Client, r *resty.Request) error {
		r.SetHeader("token", "my-token")
		r.SetHeader("nonce", strconv.FormatInt(rand.Int63(), 10))
		return nil
	})
	// request json and pagination
	go func() {
		client.R().
			Scopes(TransferJSONContentType, PageParam(1, 100)). //
			Get("https://localhost:8080/bar")
	}()

	// request xml
	go func() {
		for i := 1; i < 10; i++ {
			client.R().
				Scopes(TransferJSONContentType, PageParam(i, 20)). //
				Get("https://localhost:8080/bar")
		}
	}()
	time.Sleep(time.Second * 5)
}

@jeevatkm
Copy link
Member

@thinkgos I think I get the point. However, I need to think about it and will take care of it in Resty v3.
Maybe the name of the feature may not be called Scopes.

@jeevatkm jeevatkm added the v3 For resty v3 label Oct 24, 2021
@jeevatkm jeevatkm added this to the v3.0.0 Milestone milestone Oct 24, 2021
@jeevatkm jeevatkm added v3-consideration and removed v3 For resty v3 labels Mar 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

None yet

2 participants