@@ -32,23 +32,32 @@ import (
3232type  postOptions  struct  {
3333	proxy              string 
3434	tlsConfig          * tls.Config 
35+ 	contentType        string 
36+ 	username           string 
37+ 	password           string 
3538	requestModifier    func (* retryablehttp.Request )
36- 	responseValidator  func (statusCode   int ,  body  [] byte ) error 
39+ 	responseValidator  func (* http. Response ) error 
3740}
3841
3942type  postOption  func (* postOptions )
4043
41- func  postMessage (ctx  context.Context , address  string , payload  interface {} , opts  ... postOption ) error  {
44+ func  postMessage (ctx  context.Context , address  string , payload  any , opts  ... postOption ) error  {
4245	options  :=  & postOptions {
4346		// Default validateResponse function verifies that the response status code is 200, 202 or 201. 
44- 		responseValidator : func (statusCode  int , body  []byte ) error  {
45- 			if  statusCode  ==  http .StatusOK  || 
46- 				statusCode  ==  http .StatusAccepted  || 
47- 				statusCode  ==  http .StatusCreated  {
47+ 		responseValidator : func (resp  * http.Response ) error  {
48+ 			s  :=  resp .StatusCode 
49+ 			if  200  <=  s  &&  s  <  300  {
4850				return  nil 
4951			}
5052
51- 			return  fmt .Errorf ("request failed with status code %d, %s" , statusCode , string (body ))
53+ 			err  :=  fmt .Errorf ("request failed with status code %d" , s )
54+ 
55+ 			b , bodyErr  :=  io .ReadAll (resp .Body )
56+ 			if  bodyErr  !=  nil  {
57+ 				return  fmt .Errorf ("%w: unable to read response body: %w" , err , bodyErr )
58+ 			}
59+ 
60+ 			return  fmt .Errorf ("%w: %s" , err , string (b ))
5261		},
5362	}
5463
@@ -61,17 +70,31 @@ func postMessage(ctx context.Context, address string, payload interface{}, opts
6170		return  err 
6271	}
6372
64- 	data , err  :=  json .Marshal (payload )
65- 	if  err  !=  nil  {
66- 		return  fmt .Errorf ("marshalling notification payload failed: %w" , err )
73+ 	contentType  :=  options .contentType 
74+ 	var  data  []byte 
75+ 	switch  contentType  {
76+ 	case  "" :
77+ 		contentType  =  "application/json" 
78+ 		var  err  error 
79+ 		data , err  =  json .Marshal (payload )
80+ 		if  err  !=  nil  {
81+ 			return  fmt .Errorf ("marshalling notification payload failed: %w" , err )
82+ 		}
83+ 	default :
84+ 		data  =  payload .([]byte )
6785	}
6886
6987	req , err  :=  retryablehttp .NewRequestWithContext (ctx , http .MethodPost , address , data )
7088	if  err  !=  nil  {
7189		return  fmt .Errorf ("failed to create a new request: %w" , err )
7290	}
7391
74- 	req .Header .Set ("Content-Type" , "application/json" )
92+ 	req .Header .Set ("Content-Type" , contentType )
93+ 
94+ 	if  options .username  !=  ""  ||  options .password  !=  ""  {
95+ 		req .SetBasicAuth (options .username , options .password )
96+ 	}
97+ 
7598	if  options .requestModifier  !=  nil  {
7699		options .requestModifier (req )
77100	}
@@ -82,12 +105,7 @@ func postMessage(ctx context.Context, address string, payload interface{}, opts
82105	}
83106	defer  resp .Body .Close ()
84107
85- 	body , err  :=  io .ReadAll (resp .Body )
86- 	if  err  !=  nil  {
87- 		return  fmt .Errorf ("failed to read response body: %w" , err )
88- 	}
89- 
90- 	if  err  :=  options .responseValidator (resp .StatusCode , body ); err  !=  nil  {
108+ 	if  err  :=  options .responseValidator (resp ); err  !=  nil  {
91109		return  fmt .Errorf ("request failed: %w" , err )
92110	}
93111
@@ -106,13 +124,26 @@ func withTLSConfig(tlsConfig *tls.Config) postOption {
106124	}
107125}
108126
127+ func  withContentType (contentType  string ) postOption  {
128+ 	return  func (opts  * postOptions ) {
129+ 		opts .contentType  =  contentType 
130+ 	}
131+ }
132+ 
133+ func  withBasicAuth (username , password  string ) postOption  {
134+ 	return  func (opts  * postOptions ) {
135+ 		opts .username  =  username 
136+ 		opts .password  =  password 
137+ 	}
138+ }
139+ 
109140func  withRequestModifier (reqModifier  func (* retryablehttp.Request )) postOption  {
110141	return  func (opts  * postOptions ) {
111142		opts .requestModifier  =  reqModifier 
112143	}
113144}
114145
115- func  withResponseValidator (respValidator  func (statusCode   int ,  body  [] byte ) error ) postOption  {
146+ func  withResponseValidator (respValidator  func (resp   * http. Response ) error ) postOption  {
116147	return  func (opts  * postOptions ) {
117148		opts .responseValidator  =  respValidator 
118149	}
0 commit comments