caicloud / nirvana

Golang Restful API Framework for Productivity
https://caicloud.github.io/nirvana/
Apache License 2.0
520 stars 105 forks source link

feat(hook): add hook function for http.request #433

Closed whalecold closed 3 years ago

whalecold commented 3 years ago

What this PR does / why we need it:

feat(hook): add hook function for http.request

Which issue(s) this PR is related to (optional, link to 3rd issue(s)):

Fixes #

Reference to #

Special notes for your reviewer:

/cc @iawia002

Release note:

NONE
caicloud-bot commented 3 years ago

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: To fully approve this pull request, please assign additional approvers. We suggest the following additional approver: supereagle

Assign the PR to them by writing /assign @supereagle in a comment when ready.

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files: - **[OWNERS](https://github.com/caicloud/nirvana/blob/master/OWNERS)** Approvers can indicate their approval by writing `/approve` in a comment Approvers can cancel approval by writing `/approve cancel` in a comment
iawia002 commented 3 years ago

这个现在就是支持的,rest config 里面有一个 Executor 配置项,这个就能 hack request,参考之前给 QA 写的设置认证的方式:

type requestExecutorWithAuth struct {
    c        *http.Client
    username string
    password string
}

// Do uses the http.Client to send an HTTP request and set basic authentication before sending.
func (r *requestExecutorWithAuth) Do(req *http.Request) (*http.Response, error) {
    req.SetBasicAuth(r.username, r.password)
    return r.c.Do(req)
}

// NewRequestExecutorWithAuth implements a custom nirvana rest.RequestExecutor to set authentication information on each request.
// Usage:
//   xxx.NewClient(&rest.Config{
//       Scheme:   "...",
//       Host:     "...",
//       Executor: NewRequestExecutorWithAuth("admin", "Pwd123456"),
//   })
func NewRequestExecutorWithAuth(username, password string) rest.RequestExecutor {
    return &requestExecutorWithAuth{
        c:        &http.Client{},
        username: username,
        password: password,
    }
}

rest.NewClient(&rest.Config{
    Scheme:   "...",
    Host:     "...",
    Executor: NewRequestExecutorWithAuth("admin", "xxx"),
})
whalecold commented 3 years ago

/close