@@ -53,7 +53,7 @@ public async Task<SendCorrespondenceResponse> Send(
5353 try
5454 {
5555 if (
56- payload . CorrespondenceRequest . Content . Attachments . Count > 0
56+ payload . CorrespondenceRequest . Content . Attachments ? . Count > 0
5757 && payload . CorrespondenceRequest . Content . Attachments . All ( a => a is CorrespondenceStreamedAttachment )
5858 )
5959 {
@@ -81,7 +81,17 @@ CorrespondenceStreamedAttachment attachment in payload.CorrespondenceRequest.Con
8181 initializeAttachmentRequest ,
8282 cancellationToken
8383 ) ;
84- var attachmentId = await initializeAttachmentRequest . Content . ReadAsStringAsync ( cancellationToken ) ;
84+ var initializeAttachmentContent = initializeAttachmentRequest . Content ;
85+ if ( initializeAttachmentContent is null )
86+ {
87+ throw new CorrespondenceRequestException (
88+ "Attachment initialization request did not return content." ,
89+ null ,
90+ HttpStatusCode . InternalServerError ,
91+ "No content returned from attachment initialization"
92+ ) ;
93+ }
94+ var attachmentId = await initializeAttachmentContent . ReadAsStringAsync ( cancellationToken ) ;
8595 attachmentId = attachmentId . Trim ( '"' ) ;
8696 var attachmentDataContent = new StreamContent ( attachment . Data ) ;
8797 attachmentDataContent . Headers . ContentType = new MediaTypeHeaderValue ( "application/octet-stream" ) ;
@@ -155,39 +165,31 @@ CancellationToken cancellationToken
155165 {
156166 await Task . Delay ( TimeSpan . FromSeconds ( delaySeconds ) , cancellationToken ) ;
157167
158- try
159- {
160- var statusResponse = await AuthenticatedHttpRequestFactory (
161- method : HttpMethod . Get ,
162- uri : GetUri ( $ "attachment/{ attachmentId } ") ,
163- content : null ,
164- payload : payload
165- ) ;
168+ var statusResponse = await AuthenticatedHttpRequestFactory (
169+ method : HttpMethod . Get ,
170+ uri : GetUri ( $ "attachment/{ attachmentId } ") ,
171+ content : null ,
172+ payload : payload
173+ ) ;
174+ var pollContent = statusResponse . Content ;
166175
167- var statusContent = await statusResponse . Content . ReadFromJsonAsync < AttachmentOverview > (
168- cancellationToken
169- ) ;
170- if ( statusContent . Status == "Published" )
171- {
172- break ;
173- }
176+ if ( pollContent is null )
177+ {
178+ break ;
174179 }
175- catch ( Exception ex )
180+
181+ var statusContent = await pollContent . ReadFromJsonAsync < AttachmentOverview > ( cancellationToken ) ;
182+ if ( statusContent ? . Status == "Published" )
176183 {
177- // Log the exception
178- if ( attempt == maxAttempts )
179- {
180- // Handle or rethrow the exception after all attempts failed
181- throw ;
182- }
184+ return ;
183185 }
184- throw new CorrespondenceRequestException (
185- $ "Failure when uploading attachment. Attachment was not published in time. ",
186- null ,
187- HttpStatusCode . InternalServerError ,
188- "Polling failed"
189- ) ;
190186 }
187+ throw new CorrespondenceRequestException (
188+ $ "Failure when uploading attachment. Attachment was not published in time.",
189+ null ,
190+ HttpStatusCode . InternalServerError ,
191+ "Polling failed"
192+ ) ;
191193 }
192194
193195 /// <inheritdoc/>
0 commit comments