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 header host #4

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ type client struct {
portalAddress string
token string
options ClientOptions
host string
header map[string]string
}

func NewClient(portalAddress, token string, opts ...ClientOption) OpenAPI {
func NewClient(portalAddress, token string, host string, header map[string]string, opts ...ClientOption) OpenAPI {
var options ClientOptions
for _, opt := range opts {
opt(&options)
Expand All @@ -69,7 +71,9 @@ func NewClient(portalAddress, token string, opts ...ClientOption) OpenAPI {
return &client{
portalAddress: normalizeURL(portalAddress),
token: token,
host: host,
options: options,
header: header,
}
}

Expand All @@ -81,6 +85,12 @@ func (c *client) newRequest(method, url string, body io.Reader) (*http.Request,

req.Header.Set("Authorization", c.token)
req.Header.Set("Content-Type", "application/json;charset=UTF-8")
for k, v := range c.header {
req.Header.Set(k, v)
}
if c.host != "" {
req.Host = c.host
}
return req, nil
}

Expand All @@ -100,8 +110,8 @@ func (c *client) do(method, url string, request, response interface{}) error {
status int
)
defer func(reqBody, respBody *[]byte) {
c.debug("Method: %s, URL: %s, \n Request body: %s,\n Response body: %s",
method, url, *reqBody, *respBody)
c.debug("Method: %s, URL: %s, header:%v \n Request body: %s,\n Response body: %s",
method, url, req.Header, *reqBody, *respBody)
}(&reqBody, &respBody)

if request != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/shima-park/apollo-openapi
module github.com/Xuwudong/apollo-openapi

go 1.12