Skip to content

Commit

Permalink
add PreRequestHook to client options so clients can modify the reques…
Browse files Browse the repository at this point in the history
…t just prior to dispatch, for example to set/modify headers. (#63)
  • Loading branch information
bradlo authored Feb 24, 2023
1 parent 1d2862f commit 0329cce
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
20 changes: 14 additions & 6 deletions rai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ import (

const userAgent = "raictl/" + Version

type PreRequestHook func(*http.Request) *http.Request

type ClientOptions struct {
Config
HTTPClient *http.Client
AccessTokenHandler AccessTokenHandler
PreRequestHook PreRequestHook
}

func NewClientOptions(cfg *Config) *ClientOptions {
Expand All @@ -55,6 +58,7 @@ type Client struct {
Port string
HttpClient *http.Client
accessTokenHandler AccessTokenHandler
preRequestHook PreRequestHook
}

const DefaultHost = "azure.relationalai.com"
Expand Down Expand Up @@ -86,12 +90,13 @@ func NewClient(ctx context.Context, opts *ClientOptions) *Client {
opts.HTTPClient = &http.Client{}
}
client := &Client{
ctx: ctx,
Region: region,
Scheme: scheme,
Host: host,
Port: port,
HttpClient: opts.HTTPClient}
ctx: ctx,
Region: region,
Scheme: scheme,
Host: host,
Port: port,
preRequestHook: opts.PreRequestHook,
HttpClient: opts.HTTPClient}
if opts.AccessTokenHandler != nil {
client.accessTokenHandler = opts.AccessTokenHandler
} else if opts.Credentials == nil {
Expand Down Expand Up @@ -336,6 +341,9 @@ func isErrorStatus(rsp *http.Response) bool {
// Execute the given request and return the response or error.
func (c *Client) Do(req *http.Request) (*http.Response, error) {
req = req.WithContext(c.ctx)
if c.preRequestHook != nil {
req = c.preRequestHook(req)
}
rsp, err := c.HttpClient.Do(req)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion rai/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@

package rai

const Version = "0.4.0-alpha"
const Version = "0.4.1-alpha"

0 comments on commit 0329cce

Please sign in to comment.