@@ -128,16 +128,16 @@ final class HTTPServer: ObservableObject {
128128 return
129129 }
130130
131- if let index = parseWorkoutSubpath ( path: path, suffix : " metrics " ) {
132- await handleGetMetrics ( connection: connection, index: index)
131+ if let index = parseWorkoutIndex ( path: path) {
132+ await handleGetWorkout ( connection: connection, index: index)
133133 return
134134 }
135135
136136 sendResponse ( connection: connection, status: 404 , body: " { \" error \" : \" Not found \" } " )
137137 }
138138
139- private func parseWorkoutSubpath ( path: String , suffix : String ) -> Int ? {
140- let pattern = " ^/workouts/( \\ d+)/ \( suffix ) $ "
139+ private func parseWorkoutIndex ( path: String ) -> Int ? {
140+ let pattern = " ^/workouts/( \\ d+)$ "
141141 guard let regex = try ? NSRegularExpression ( pattern: pattern) ,
142142 let match = regex. firstMatch (
143143 in: path, range: NSRange ( path. startIndex... , in: path) ) ,
@@ -178,7 +178,7 @@ final class HTTPServer: ObservableObject {
178178 }
179179
180180 @MainActor
181- private func handleGetMetrics ( connection: NWConnection , index: Int ) async {
181+ private func handleGetWorkout ( connection: NWConnection , index: Int ) async {
182182 guard let manager = healthKitManager else {
183183 sendResponse (
184184 connection: connection, status: 500 ,
@@ -198,13 +198,13 @@ final class HTTPServer: ObservableObject {
198198 return
199199 }
200200
201- let metrics = try await manager. fetchAllMetrics ( for: workouts [ index] )
202- let routeCount = ( metrics [ " route " ] as? [ [ String : Any ] ] ) ? . count ?? 0
203- let metricCount = metrics . values. compactMap { ( $0 as? [ [ String : Any ] ] ) ? . count } . reduce ( 0 , + ) - routeCount
204- log ( " /workouts/ \( index) /metrics " , status: 200 , detail: " \( metricCount) metrics, \( routeCount) GPS " )
205- sendJSONResponse ( connection: connection, object: metrics )
201+ let data = try await manager. fetchAllMetrics ( for: workouts [ index] )
202+ let routeCount = ( data [ " route " ] as? [ [ String : Any ] ] ) ? . count ?? 0
203+ let metricCount = data . values. compactMap { ( $0 as? [ [ String : Any ] ] ) ? . count } . reduce ( 0 , + ) - routeCount
204+ log ( " /workouts/ \( index) " , status: 200 , detail: " \( metricCount) metrics, \( routeCount) GPS " )
205+ sendJSONResponse ( connection: connection, object: data )
206206 } catch {
207- log ( " /workouts/ \( index) /metrics " , status: 500 , detail: error. localizedDescription)
207+ log ( " /workouts/ \( index) " , status: 500 , detail: error. localizedDescription)
208208 sendResponse (
209209 connection: connection, status: 500 ,
210210 body: " { \" error \" : \" \( error. localizedDescription) \" } " )
0 commit comments