-
Notifications
You must be signed in to change notification settings - Fork 1
/
wrapper.go
46 lines (40 loc) · 1.09 KB
/
wrapper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package openapi
import (
"fmt"
"github.com/easyops-cn/giraffe-micro"
"github.com/easyops-cn/giraffe-micro/plugins/restv2"
"net/http"
)
type wrapperMiddleware struct {
name string
restv2.Middleware
}
func (w *wrapperMiddleware) NewRequest(rule giraffe.HttpRule, in interface{}) (*http.Request, error) {
req, err := w.Middleware.NewRequest(rule, in)
if err == nil {
req.URL.Path = fmt.Sprintf("/%s%s", w.name, req.URL.Path)
}
return req, err
}
type wrapper struct {
*restv2.Client
}
func WrapClient(name string, client *Client) giraffe.Client {
return &wrapper{
Client: &restv2.Client{
Client: client.Client.Client,
Middleware: &wrapperMiddleware{name: name, Middleware: client.Client.Middleware},
NameService: client.Client.NameService,
},
}
}
func WrapClientWithScheme(name string, scheme string, client *Client) giraffe.Client {
return &wrapper{
Client: &restv2.Client{
Client: client.Client.Client,
Middleware: &wrapperMiddleware{name: name, Middleware: client.Client.Middleware},
NameService: client.Client.NameService,
Scheme: scheme,
},
}
}