resty icon indicating copy to clipboard operation
resty copied to clipboard

Add request Scopes api, user can add parameters dynamically.

Open thinkgos opened this issue 4 years ago • 4 comments

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")
}

thinkgos avatar Sep 17 '21 06:09 thinkgos

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

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

:exclamation: 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              

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Sep 17 '21 06:09 codecov[bot]

@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?

jeevatkm avatar Sep 19 '21 18:09 jeevatkm

@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)
}

thinkgos avatar Sep 20 '21 01:09 thinkgos

@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 avatar Oct 24 '21 23:10 jeevatkm