GoogleDataTransport+GoogleDataTransportProtocol.swift 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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, completion: @escaping (Result<Void, Error>) -> Void)
  22. func eventForTransport() -> GDTCOREvent
  23. }
  24. /// Workaround in combo with preconcurrency import of GDT. When GDT's
  25. /// `GDTCORTransport`type conforms to Sendable within the GDT module,
  26. /// this can be removed.
  27. final class GoogleDataTransporter: GoogleDataTransportProtocol {
  28. private let transporter: GDTCORTransport
  29. init(mappingID: String,
  30. transformers: [any GDTCOREventTransformer]?,
  31. target: GDTCORTarget) {
  32. transporter = GDTCORTransport(mappingID: mappingID, transformers: transformers, target: target)!
  33. }
  34. func logGDTEvent(event: GDTCOREvent, completion: @escaping (Result<Void, any Error>) -> Void) {
  35. transporter.sendDataEvent(event) { wasWritten, error in
  36. if let error {
  37. completion(.failure(error))
  38. } else if !wasWritten {
  39. completion(.failure(GoogleDataTransportProtocolErrors.writeFailure))
  40. } else {
  41. completion(.success(()))
  42. }
  43. }
  44. }
  45. func eventForTransport() -> GDTCOREvent {
  46. transporter.eventForTransport()
  47. }
  48. }