@@ -69,6 +69,16 @@ type APIClient struct {
6969 Customer string
7070}
7171
72+ type HTTPRequestOpts func (req * http.Request )
73+
74+ func WithHttpHeaders (headers map [string ]string ) HTTPRequestOpts {
75+ return func (req * http.Request ) {
76+ for key , val := range headers {
77+ req .Header .Set (key , val )
78+ }
79+ }
80+ }
81+
7282func NewClient (baseURL , apiKey , customer string ) (Client , error ) {
7383 return & APIClient {
7484 HTTPClient : & http.Client {},
@@ -78,11 +88,15 @@ func NewClient(baseURL, apiKey, customer string) (Client, error) {
7888 }, nil
7989}
8090
81- func (c * APIClient ) do (req * http.Request ) ([]byte , error ) {
91+ func (c * APIClient ) do (req * http.Request , opts ... HTTPRequestOpts ) ([]byte , error ) {
8292 if req == nil {
8393 return nil , nil
8494 }
8595
96+ for _ , opt := range opts {
97+ opt (req )
98+ }
99+
86100 req .Header .Set ("Authorization" , "Bearer " + c .APIKey )
87101 res , err := c .HTTPClient .Do (req )
88102 if err != nil {
@@ -112,7 +126,7 @@ func (c *APIClient) get(ctx context.Context, URI string) ([]byte, error) {
112126 return c .do (req )
113127}
114128
115- func (c * APIClient ) update (ctx context.Context , URI string , payload any , method string ) ([]byte , error ) {
129+ func (c * APIClient ) update (ctx context.Context , URI string , payload any , method string , opts ... HTTPRequestOpts ) ([]byte , error ) {
116130 reqBody , err := json .Marshal (payload )
117131 if err != nil {
118132 return nil , fmt .Errorf ("failed to marshal config: %w" , err )
@@ -122,11 +136,11 @@ func (c *APIClient) update(ctx context.Context, URI string, payload any, method
122136 return nil , fmt .Errorf ("failed to create request: %w" , err )
123137 }
124138 httpReq .Header .Set ("Content-Type" , "application/json" )
125- return c .do (httpReq )
139+ return c .do (httpReq , opts ... )
126140}
127141
128- func (c * APIClient ) post (ctx context.Context , URI string , payload any ) ([]byte , error ) {
129- return c .update (ctx , URI , payload , "POST" )
142+ func (c * APIClient ) post (ctx context.Context , URI string , payload any , opts ... HTTPRequestOpts ) ([]byte , error ) {
143+ return c .update (ctx , URI , payload , "POST" , opts ... )
130144}
131145
132146func (c * APIClient ) put (ctx context.Context , URI string , payload any ) ([]byte , error ) {
0 commit comments