Skip to content

Commit e8b38bf

Browse files
committed
Only add body if non nil
1 parent 56fa2d7 commit e8b38bf

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

templates/swift/Sources/NotificationHandler.swift.twig

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,17 +219,28 @@ open class NotificationHandler {
219219
request.headers.add(name: key, value: value)
220220
}
221221

222-
let json = try JSONSerialization.data(withJSONObject: body as Any, options: [])
222+
if (body != nil) {
223+
guard let json = try? JSONSerialization.data(withJSONObject: body!, options: []) else {
224+
return nil
225+
}
223226

224-
request.body = .bytes(json)
227+
request.body = .bytes(json)
228+
}
225229

226-
let response = try await client!.http.execute(
230+
guard let response = try? await client!.http.execute(
227231
request,
228232
timeout: .seconds(30)
229-
)
233+
) else {
234+
return nil
235+
}
230236

231-
let data = try await response.body.collect(upTo: Int.max)
232-
let dict = try JSONSerialization.jsonObject(with: data) as? [String: Any]
237+
guard let data = try? await response.body.collect(upTo: Int.max) else {
238+
return nil
239+
}
240+
241+
guard let dict = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else {
242+
return nil
243+
}
233244

234245
return dict
235246
}

0 commit comments

Comments
 (0)