GoogleDataTransport+GoogleDataTransportProtocol.swift 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //
  2. // Copyright 2022 Google LLC
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. import Foundation
  16. @preconcurrency internal import GoogleDataTransport
  17. enum GoogleDataTransportProtocolErrors: Error {
  18. case writeFailure
  19. }
  20. protocol GoogleDataTransportProtocol: Sendable {
  21. func logGDTEvent(event: GDTCOREvent,
  22. completion: @escaping @Sendable (Result<Void, Error>) -> Void)
  23. func eventForTransport() -> GDTCOREvent
  24. }
  25. /// Workaround in combo with preconcurrency import of GDT. When GDT's
  26. /// `GDTCORTransport`type conforms to Sendable within the GDT module,
  27. /// this can be removed.
  28. final class GoogleDataTransporter: GoogleDataTransportProtocol {
  29. private let transporter: GDTCORTransport
  30. init(mappingID: String,
  31. transformers: [any GDTCOREventTransformer]?,
  32. target: GDTCORTarget) {
  33. transporter = GDTCORTransport(mappingID: mappingID, transformers: transformers, target: target)!
  34. }
  35. func logGDTEvent(event: GDTCOREvent,
  36. completion: @escaping @Sendable (Result<Void, any Error>) -> Void) {
  37. transporter.sendDataEvent(event) { wasWritten, error in
  38. if let error {
  39. completion(.failure(error))
  40. } else if !wasWritten {
  41. completion(.failure(GoogleDataTransportProtocolErrors.writeFailure))
  42. } else {
  43. completion(.success(()))
  44. }
  45. }
  46. }
  47. func eventForTransport() -> GDTCOREvent {
  48. transporter.eventForTransport()
  49. }
  50. }