Modernize networking layer and fix category navigation and recipe list bugs
Network layer: - Replace static CookbookApi protocol with instance-based CookbookApiProtocol using async/throws instead of tuple returns - Refactor ApiRequest to use URLComponents for proper URL encoding, replace print statements with OSLog, and return typed NetworkError cases - Add structured NetworkError variants (httpError, connectionError, etc.) - Remove global cookbookApi constant in favor of injected dependency on AppState - Delete unused RecipeEditViewModel, RecipeScraper, and Scraper playground Data & model fixes: - Add custom Decodable for RecipeDetail with safe fallbacks for malformed JSON - Make Category Hashable/Equatable use only `name` so NavigationSplitView selection survives category refreshes with updated recipe_count - Return server-assigned ID from uploadRecipe so new recipes get their ID before the post-upload refresh block executes View updates: - Refresh both old and new category recipe lists after upload when category changes, mapping empty recipeCategory to "*" for uncategorized recipes - Raise deployment target to iOS 18, adopt new SwiftUI API conventions - Clean up alerts, onboarding views, and settings Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -14,7 +14,7 @@ struct ApiRequest {
|
||||
let authString: String?
|
||||
let headerFields: [HeaderField]
|
||||
let body: Data?
|
||||
|
||||
|
||||
init(
|
||||
path: String,
|
||||
method: RequestMethod,
|
||||
@@ -28,21 +28,21 @@ struct ApiRequest {
|
||||
self.authString = authString
|
||||
self.body = body
|
||||
}
|
||||
|
||||
|
||||
func send(pathCompletion: Bool = true) async -> (Data?, NetworkError?) {
|
||||
Logger.network.debug("\(method.rawValue) \(path) sending ...")
|
||||
|
||||
|
||||
// Prepare URL
|
||||
let urlString = pathCompletion ? UserSettings.shared.serverProtocol + UserSettings.shared.serverAddress + path : path
|
||||
print("Full path: \(urlString)")
|
||||
//Logger.network.debug("Full path: \(urlString)")
|
||||
guard let urlStringSanitized = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { return (nil, .unknownError) }
|
||||
guard let url = URL(string: urlStringSanitized) else { return (nil, .unknownError) }
|
||||
|
||||
guard var components = URLComponents(string: urlString) else { return (nil, .missingUrl) }
|
||||
// Ensure path percent encoding is applied correctly
|
||||
components.percentEncodedPath = components.path.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? components.path
|
||||
guard let url = components.url else { return (nil, .missingUrl) }
|
||||
|
||||
// Create URL request
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = method.rawValue
|
||||
|
||||
|
||||
// Set authentication string, if needed
|
||||
if let authString = authString {
|
||||
request.setValue(
|
||||
@@ -50,7 +50,7 @@ struct ApiRequest {
|
||||
forHTTPHeaderField: "Authorization"
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
// Set other header fields
|
||||
for headerField in headerFields {
|
||||
request.setValue(
|
||||
@@ -58,46 +58,38 @@ struct ApiRequest {
|
||||
forHTTPHeaderField: headerField.getField()
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
// Set http body
|
||||
if let body = body {
|
||||
request.httpBody = body
|
||||
}
|
||||
|
||||
|
||||
// Wait for and return data and (decoded) response
|
||||
var data: Data? = nil
|
||||
var response: URLResponse? = nil
|
||||
do {
|
||||
(data, response) = try await URLSession.shared.data(for: request)
|
||||
Logger.network.debug("\(method.rawValue) \(path) SUCCESS!")
|
||||
if let error = decodeURLResponse(response: response as? HTTPURLResponse) {
|
||||
print("\(method.rawValue) \(path) FAILURE: \(error.localizedDescription)")
|
||||
let (data, response) = try await URLSession.shared.data(for: request)
|
||||
if let error = decodeURLResponse(response: response as? HTTPURLResponse, data: data) {
|
||||
Logger.network.debug("\(method.rawValue) \(path) FAILURE: \(error.localizedDescription)")
|
||||
return (nil, error)
|
||||
}
|
||||
if let data = data {
|
||||
print(data, String(data: data, encoding: .utf8) as Any)
|
||||
return (data, nil)
|
||||
}
|
||||
return (nil, .unknownError)
|
||||
Logger.network.debug("\(method.rawValue) \(path) SUCCESS!")
|
||||
return (data, nil)
|
||||
} catch {
|
||||
let error = decodeURLResponse(response: response as? HTTPURLResponse)
|
||||
Logger.network.debug("\(method.rawValue) \(path) FAILURE: \(error.debugDescription)")
|
||||
return (nil, error)
|
||||
Logger.network.debug("\(method.rawValue) \(path) FAILURE: \(error.localizedDescription)")
|
||||
return (nil, .connectionError(underlying: error))
|
||||
}
|
||||
}
|
||||
|
||||
private func decodeURLResponse(response: HTTPURLResponse?) -> NetworkError? {
|
||||
|
||||
private func decodeURLResponse(response: HTTPURLResponse?, data: Data?) -> NetworkError? {
|
||||
guard let response = response else {
|
||||
return NetworkError.unknownError
|
||||
return .unknownError(detail: "No HTTP response")
|
||||
}
|
||||
print("Status code: ", response.statusCode)
|
||||
switch response.statusCode {
|
||||
case 200...299: return (nil)
|
||||
case 300...399: return (NetworkError.redirectionError)
|
||||
case 400...499: return (NetworkError.clientError)
|
||||
case 500...599: return (NetworkError.serverError)
|
||||
case 600: return (NetworkError.invalidRequest)
|
||||
default: return (NetworkError.unknownError)
|
||||
let statusCode = response.statusCode
|
||||
switch statusCode {
|
||||
case 200...299:
|
||||
return nil
|
||||
default:
|
||||
let body = data.flatMap { String(data: $0, encoding: .utf8) }
|
||||
return .httpError(statusCode: statusCode, body: body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user