-
Notifications
You must be signed in to change notification settings - Fork 723
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
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
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. |
@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 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 I think I get the point. However, I need to think about it and will take care of it in Resty v3. |
This PR concept is added in the upcoming Resty v3 on PR #910 |
I want the Scopes api, which could be used to add parameters dynamically. It will be more flexible.