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

Custom headers #61

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add: custom header processing
  • Loading branch information
shiro8613 authored and QuintenQVD0 committed Jan 17, 2025
commit 1baf955221ca4faef890003255efcc73c90eb0b3
12 changes: 12 additions & 0 deletions remote/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type client struct {
tokenId string
token string
maxAttempts int
customHeaders map[string]string
}

// New returns a new HTTP request client that is used for making authenticated
Expand Down Expand Up @@ -69,6 +70,13 @@ func WithCredentials(id, token string) ClientOption {
}
}

// WithCustomHeader sets custom headers to be used when making remote requests.
func WithCustomHeader(headers map[string]string) ClientOption {
return func(c *client) {
c.customHeaders = headers
}
}

// WithHttpClient sets the underlying HTTP client instance to use when making
// requests to the Panel API.
func WithHttpClient(httpClient *http.Client) ClientOption {
Expand Down Expand Up @@ -110,6 +118,10 @@ func (c *client) requestOnce(ctx context.Context, method, path string, body io.R
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s.%s", c.tokenId, c.token))

for cHeaderKey, cHeaderValue := range c.customHeaders {
req.Header.Set(cHeaderKey, cHeaderValue)
}

// Call all opts functions to allow modifying the request
for _, o := range opts {
Expand Down