SessionCoordinator.swift 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2022 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. protocol SessionCoordinatorProtocol {
  16. func attemptLoggingSessionStart(event: SessionStartEvent,
  17. callback: @escaping (Result<Void, FirebaseSessionsError>) -> Void)
  18. }
  19. ///
  20. /// SessionCoordinator is responsible for coordinating the systems in this SDK
  21. /// involved with sending a Session Start event.
  22. ///
  23. class SessionCoordinator: SessionCoordinatorProtocol {
  24. let installations: InstallationsProtocol
  25. let fireLogger: EventGDTLoggerProtocol
  26. init(installations: InstallationsProtocol,
  27. fireLogger: EventGDTLoggerProtocol) {
  28. self.installations = installations
  29. self.fireLogger = fireLogger
  30. }
  31. /// Begins the process of logging a SessionStartEvent to FireLog after
  32. /// it has been approved for sending
  33. func attemptLoggingSessionStart(event: SessionStartEvent,
  34. callback: @escaping (Result<Void, FirebaseSessionsError>)
  35. -> Void) {
  36. /// Order of execution
  37. /// 1. Fetch the installations Id. If successful, move to 3. Else, drop sending the event.
  38. /// 2. Log the event. If successful, all is good. Else, log the message with error.
  39. installations.installationID { result in
  40. switch result {
  41. case let .success(fiid):
  42. event.setInstallationID(installationId: fiid)
  43. self.fireLogger.logEvent(event: event) { logResult in
  44. switch logResult {
  45. case .success():
  46. Logger.logDebug("Successfully logged Session Start event to GoogleDataTransport")
  47. callback(.success(()))
  48. case let .failure(error):
  49. callback(.failure(FirebaseSessionsError.DataTransportError(error)))
  50. }
  51. }
  52. case let .failure(error):
  53. callback(.failure(FirebaseSessionsError.SessionInstallationsError(error)))
  54. }
  55. }
  56. }
  57. }