Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Code Quality #121

Merged
merged 5 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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