Skip to content

Commit

Permalink
Improve Code Quality (#121)
Browse files Browse the repository at this point in the history
* Disable force_cast warning for Caches Directory File URL

* Fix line length warning in systemVersion

* Remove an unnecessary force try

* Fix line length warnings in SignalManager

* Fix trailing whitespace issue
  • Loading branch information
winsmith authored Sep 8, 2023
1 parent 99a00be commit a47879b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
9 changes: 7 additions & 2 deletions Sources/TelemetryClient/Signal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ extension DefaultSignalPayload {

/// The operating system and its version
static var systemVersion: String {
return "\(platform) \(ProcessInfo.processInfo.operatingSystemVersion.majorVersion).\(ProcessInfo.processInfo.operatingSystemVersion.minorVersion).\(ProcessInfo.processInfo.operatingSystemVersion.patchVersion)"
let majorVersion = ProcessInfo.processInfo.operatingSystemVersion.majorVersion
let minorVersion = ProcessInfo.processInfo.operatingSystemVersion.minorVersion
let patchVersion = ProcessInfo.processInfo.operatingSystemVersion.patchVersion
return "\(platform) \(majorVersion).\(minorVersion).\(patchVersion)"
}

/// The major system version, i.e. iOS 15
Expand All @@ -123,7 +126,9 @@ extension DefaultSignalPayload {

/// The major system version, i.e. iOS 15
static var majorMinorSystemVersion: String {
return "\(platform) \(ProcessInfo.processInfo.operatingSystemVersion.majorVersion).\(ProcessInfo.processInfo.operatingSystemVersion.minorVersion)"
let majorVersion = ProcessInfo.processInfo.operatingSystemVersion.majorVersion
let minorVersion = ProcessInfo.processInfo.operatingSystemVersion.minorVersion
return "\(platform) \(majorVersion).\(minorVersion)"
}

/// The Bundle Short Version String, as described in Info.plist
Expand Down
2 changes: 2 additions & 0 deletions Sources/TelemetryClient/SignalCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@ internal class SignalCache<T> where T: Codable {
}

private func fileURL() -> URL {
// swiftlint:disable force_try
let cacheFolderURL = try! FileManager.default.url(
for: .cachesDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: false
)
// swiftlint:enable force_try

return cacheFolderURL.appendingPathComponent("telemetrysignalcache")
}
Expand Down
18 changes: 15 additions & 3 deletions Sources/TelemetryClient/SignalManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ internal class SignalManager: SignalManageable {
}

/// Adds a signal to the process queue
func processSignal(_ signalType: TelemetrySignalType, for clientUser: String? = nil, floatValue: Double? = nil, with additionalPayload: [String: String] = [:], configuration: TelemetryManagerConfiguration) {
func processSignal(
_ signalType: TelemetrySignalType,
for clientUser: String? = nil,
floatValue: Double? = nil,
with additionalPayload: [String: String] = [:],
configuration: TelemetryManagerConfiguration
) {
DispatchQueue.global(qos: .utility).async {
let enrichedMetadata: [String: String] = configuration.metadataEnrichers
.map { $0.enrich(signalType: signalType, for: clientUser, floatValue: floatValue) }
Expand Down Expand Up @@ -179,7 +185,11 @@ private extension SignalManager {
urlRequest.httpMethod = "POST"
urlRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")

urlRequest.httpBody = try! JSONEncoder.telemetryEncoder.encode(signalPostBodies)
guard let body = try? JSONEncoder.telemetryEncoder.encode(signalPostBodies) else {
return
}

urlRequest.httpBody = body
self.configuration.logHandler?.log(.debug, message: String(data: urlRequest.httpBody!, encoding: .utf8)!)

let task = URLSession.shared.dataTask(with: urlRequest, completionHandler: completionHandler)
Expand Down Expand Up @@ -221,7 +231,9 @@ private extension SignalManager {
}
#else
#if DEBUG
configuration.logHandler?.log(message: "[Telemetry] On this platform, Telemetry can't generate a unique user identifier. It is recommended you supply one yourself. More info: https://telemetrydeck.com/pages/signal-reference.html")
let line1 = "[Telemetry] On this platform, Telemetry can't generate a unique user identifier."
let line2 = "It is recommended you supply one yourself. More info: https://telemetrydeck.com/pages/signal-reference.html"
configuration.logHandler?.log(message: "\(line1) \(line2)")
#endif
return "unknown user \(DefaultSignalPayload.platform) \(DefaultSignalPayload.systemVersion) \(DefaultSignalPayload.buildNumber)"
#endif
Expand Down

0 comments on commit a47879b

Please sign in to comment.