Support for logging OpenAPI responses (#75)
- Commit
2265449730a4c5ddc708b50f67b2611f7975b914- Author
- Kabir Oberai <[email protected]>
- When
- 2025-05-22 16:33 UTC (1 year ago)
- Parents
3a4290c
Allow setting `XTL_DEV_LOG` to a regex (e.g. `export XTL_DEV_LOG='profiles_.*'`) to log the responses of all matching operations Also add `BundleIdPlatform.services` as seen [here](https://github.com/AvdLee/appstoreconnect-swift-sdk/blob/60bbc198c8d9f9992807bfebef86f45d6173a1f1/Makefile#L9)
Diff
Sources/DeveloperAPI/Generated/Types.swift
diff --git a/Sources/DeveloperAPI/Generated/Types.swift b/Sources/DeveloperAPI/Generated/Types.swiftindex 84bf00f..0609e31 100644--- a/Sources/DeveloperAPI/Generated/Types.swift+++ b/Sources/DeveloperAPI/Generated/Types.swift@@ -5021,6 +5021,7 @@ public enum Components { case ios = "IOS" case macOs = "MAC_OS" case universal = "UNIVERSAL"+ case services = "SERVICES" } /// - Remark: Generated from `#/components/schemas/CapabilityOption`. public struct CapabilityOption: Codable, Hashable, Sendable {
Sources/DeveloperAPI/openapi-overlay.yaml
diff --git a/Sources/DeveloperAPI/openapi-overlay.yaml b/Sources/DeveloperAPI/openapi-overlay.yamlindex ea68eea..ea5fcbe 100644--- a/Sources/DeveloperAPI/openapi-overlay.yaml+++ b/Sources/DeveloperAPI/openapi-overlay.yaml@@ -23,3 +23,6 @@ actions: - target: '$.components.schemas.Device.properties.attributes.properties.deviceClass' update: enum: ["APPLE_VISION_PRO"]+ - target: '$.components.schemas.BundleIdPlatform'+ update:+ enum: ["SERVICES"]
Sources/XKit/DeveloperServices/DeveloperServices+OpenAPI.swift
diff --git a/Sources/XKit/DeveloperServices/DeveloperServices+OpenAPI.swift b/Sources/XKit/DeveloperServices/DeveloperServices+OpenAPI.swiftindex ad720ef..3ba555e 100644--- a/Sources/XKit/DeveloperServices/DeveloperServices+OpenAPI.swift+++ b/Sources/XKit/DeveloperServices/DeveloperServices+OpenAPI.swift@@ -17,7 +17,8 @@ extension DeveloperAPIClient { ), transport: httpClient.asOpenAPITransport, middlewares: [- auth.middleware+ auth.middleware,+ LoggingMiddleware(), ] ) }@@ -219,3 +220,39 @@ public struct DeveloperAPIASCAuthMiddleware: ClientMiddleware { return try await next(request, body, baseURL) } }++struct LoggingMiddleware: ClientMiddleware {+ static let regex: NSRegularExpression? = {+ guard let pat = ProcessInfo.processInfo.environment["XTL_DEV_LOG"] else { return nil }+ return try? NSRegularExpression(pattern: pat)+ }()++ func intercept(+ _ request: HTTPRequest,+ body: HTTPBody?,+ baseURL: URL,+ operationID: String,+ next: (HTTPRequest, HTTPBody?, URL) async throws -> (HTTPResponse, HTTPBody?)+ ) async throws -> (HTTPResponse, HTTPBody?) {+ var (response, body) = try await next(request, body, baseURL)++ guard Self.regex?.firstMatch(+ in: operationID,+ range: NSRange(operationID.startIndex..., in: operationID)+ ) != nil+ else { return (response, body) }++ print("\n\(operationID) response status -> \(response.status)")++ if let unwrapped = body {+ let data = try await Data(collecting: unwrapped, upTo: .max)+ // body may only be consumable once, replace it with the collected data+ body = .init(data)++ let text = String(decoding: data, as: UTF8.self)+ print("\(operationID) response body -> \(text)")+ }++ return (response, body)+ }+}