@@ -81,6 +81,12 @@ private HttpClient GenerateProviderHttpClient()
8181
8282 var properties = _smsConfiguration . Properties ;
8383
84+ return SetRequestHeaders ( client , provider , properties ) ;
85+ }
86+
87+ private HttpClient SetRequestHeaders ( HttpClient client , SmsProviders provider ,
88+ Dictionary < string , string > properties )
89+ {
8490 switch ( provider )
8591 {
8692 case SmsProviders . Dexatel :
@@ -111,7 +117,7 @@ private async Task<List<GeneralSmsResponse>> SendSmsAsync(SmsMessage smsMessage,
111117 return await SendSmsViaTwilioAsync ( smsMessage , cancellationToken ) ;
112118
113119 default :
114- throw new ArgumentOutOfRangeException ( ) ;
120+ throw new InvalidOperationException ( ) ;
115121 }
116122 }
117123
@@ -129,18 +135,7 @@ private async Task<List<GeneralSmsResponse>> SendSmsViaDexatelAsync(SmsMessage s
129135 }
130136 } ;
131137
132- var response = await _httpClient . PostAsync (
133- $ "{ SmsProviderIntegrations . BaseUrls [ _smsConfiguration . Provider ] } /v1/messages",
134- new StringContent (
135- JsonConvert . SerializeObject ( request , new JsonSerializerSettings
136- {
137- ContractResolver = new DefaultContractResolver
138- {
139- NamingStrategy = new SnakeCaseNamingStrategy ( )
140- }
141- } ) ,
142- Encoding . UTF8 ,
143- "application/json" ) , cancellationToken ) ;
138+ var response = await PostAsyncViaDexatelHttpClient ( request , cancellationToken ) ;
144139
145140 var responseContent = await response . Content . ReadAsStringAsync ( cancellationToken ) ;
146141
@@ -165,23 +160,33 @@ private async Task<List<GeneralSmsResponse>> SendSmsViaDexatelAsync(SmsMessage s
165160 Body = x . Text
166161 } ) . ToList ( ) ?? [ ] ;
167162 }
168-
163+
164+ private async Task < HttpResponseMessage > PostAsyncViaDexatelHttpClient ( DexatelSmsSendRequest request ,
165+ CancellationToken cancellationToken )
166+ {
167+ return await _httpClient . PostAsync (
168+ $ "{ SmsProviderIntegrations . BaseUrls [ _smsConfiguration . Provider ] } /v1/messages",
169+ new StringContent (
170+ JsonConvert . SerializeObject ( request , new JsonSerializerSettings
171+ {
172+ ContractResolver = new DefaultContractResolver
173+ {
174+ NamingStrategy = new SnakeCaseNamingStrategy ( )
175+ }
176+ } ) ,
177+ Encoding . UTF8 ,
178+ "application/json" ) , cancellationToken ) ;
179+ }
180+
169181 private async Task < List < GeneralSmsResponse > > SendSmsViaTwilioAsync ( SmsMessage smsMessage ,
170182 CancellationToken cancellationToken )
171183 {
172184 var result = new List < TwilioSmsSendResponse > ( ) ;
173185
174186 foreach ( var phone in smsMessage . Recipients . MakeDistinct ( ) )
175187 {
176- var response = await _httpClient . PostAsync (
177- $ "{ SmsProviderIntegrations . BaseUrls [ _smsConfiguration . Provider ] } /2010-04-01/Accounts/{ _smsConfiguration . Properties [ "SID" ] } /Messages.json",
178- new FormUrlEncodedContent ( new [ ]
179- {
180- new KeyValuePair < string , string > ( "To" , phone ) ,
181- new KeyValuePair < string , string > ( "From" , _smsConfiguration . From ) ,
182- new KeyValuePair < string , string > ( "Body" , smsMessage . Message )
183- } ) , cancellationToken ) ;
184-
188+ var response = await PostAsyncViaTwilioHttpClient ( phone , smsMessage . Message , cancellationToken ) ;
189+
185190 var responseContent = await response . Content . ReadAsStringAsync ( cancellationToken ) ;
186191
187192 var responseObject = JsonConvert . DeserializeObject < TwilioSmsSendResponse > ( responseContent ,
@@ -208,4 +213,17 @@ private async Task<List<GeneralSmsResponse>> SendSmsViaTwilioAsync(SmsMessage sm
208213 Body = x . Body
209214 } ) . ToList ( ) ;
210215 }
216+
217+ private async Task < HttpResponseMessage > PostAsyncViaTwilioHttpClient ( string phone , string smsMessage ,
218+ CancellationToken cancellationToken )
219+ {
220+ return await _httpClient . PostAsync (
221+ $ "{ SmsProviderIntegrations . BaseUrls [ _smsConfiguration . Provider ] } /2010-04-01/Accounts/{ _smsConfiguration . Properties [ "SID" ] } /Messages.json",
222+ new FormUrlEncodedContent ( new [ ]
223+ {
224+ new KeyValuePair < string , string > ( "To" , phone ) ,
225+ new KeyValuePair < string , string > ( "From" , _smsConfiguration . From ) ,
226+ new KeyValuePair < string , string > ( "Body" , smsMessage )
227+ } ) , cancellationToken ) ;
228+ }
211229}
0 commit comments