-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayolaNetworkLogger.swift
More file actions
76 lines (72 loc) · 2.54 KB
/
Copy pathPlayolaNetworkLogger.swift
File metadata and controls
76 lines (72 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//
// PlayolaNetworkLogger.swift
// PlayolaPlayer
//
// Created by Brian D Keane on 6/4/26.
//
import Foundation
import os
/// A single JSON API request/response observed by the library.
///
/// The library emits these raw — it does **not** redact, persist, or format
/// anything. A consuming app is responsible for redacting sensitive values
/// (e.g. `Authorization` headers) before storing or displaying them.
public struct PlayolaNetworkLogEvent: Sendable {
/// The time the request started.
public let timestamp: Date
/// The HTTP method (e.g. `GET`, `POST`).
public let method: String
/// The request URL, if available.
public let url: URL?
/// All HTTP header fields sent with the request, unredacted.
public let requestHeaders: [String: String]
/// The raw request body, if any.
public let requestBody: Data?
/// The HTTP status code of the response, or `nil` if the request threw.
public let statusCode: Int?
/// The raw response body, or `nil` if the request threw.
public let responseBody: Data?
/// The wall-clock duration of the request in milliseconds.
public let durationMS: Int?
/// A description of the thrown error, or `nil` on success.
public let errorDescription: String?
public init(
timestamp: Date,
method: String,
url: URL?,
requestHeaders: [String: String],
requestBody: Data?,
statusCode: Int?,
responseBody: Data?,
durationMS: Int?,
errorDescription: String?
) {
self.timestamp = timestamp
self.method = method
self.url = url
self.requestHeaders = requestHeaders
self.requestBody = requestBody
self.statusCode = statusCode
self.responseBody = responseBody
self.durationMS = durationMS
self.errorDescription = errorDescription
}
}
/// A logging seam for the library's JSON API traffic.
///
/// A consuming app sets ``handler`` at startup to observe the library's JSON
/// API calls (binary audio downloads are intentionally excluded). When
/// ``handler`` is `nil` (the default) no logging occurs and behavior and
/// performance are unchanged.
public enum PlayolaNetworkLogger {
private static let _handler =
OSAllocatedUnfairLock<(@Sendable (PlayolaNetworkLogEvent) -> Void)?>(initialState: nil)
/// The handler invoked after each JSON API request completes.
///
/// Set by the consuming app at startup. `nil` (the default) disables logging.
/// Access is thread-safe.
public static var handler: (@Sendable (PlayolaNetworkLogEvent) -> Void)? {
get { _handler.withLock { $0 } }
set { _handler.withLock { $0 = newValue } }
}
}