Better file caching and update management

This commit is contained in:
Vicnet
2023-12-14 14:11:56 +01:00
parent 899dc20e55
commit a3fc891d0a
23 changed files with 592 additions and 483 deletions

View File

@@ -1,80 +0,0 @@
//
// APIController.swift
// Nextcloud Cookbook iOS Client
//
// Created by Vincent Meilinger on 20.09.23.
//
import Foundation
class APIController {
var userSettings: UserSettings
var apiPath: String
var authString: String
let apiVersion = "1"
init(userSettings: UserSettings) {
print("Initializing APIController.")
self.userSettings = userSettings
self.apiPath = "https://\(userSettings.serverAddress)/index.php/apps/cookbook/api/v\(apiVersion)/"
let loginString = "\(userSettings.username):\(userSettings.token)"
let loginData = loginString.data(using: String.Encoding.utf8)!
self.authString = loginData.base64EncodedString()
}
}
extension APIController {
func imageDataFromServer(recipeId: Int, thumb: Bool) async -> Data? {
do {
let request = RequestWrapper.imageRequest(path: .IMAGE(recipeId: recipeId, thumb: thumb))
let (data, _): (Data?, Error?) = try await NetworkHandler.sendHTTPRequest(
request,
hostPath: apiPath,
authString: authString
)
guard let data = data else {
print("Error receiving or decoding data.")
return nil
}
return data
} catch {
print("Could not load image from server.")
}
return nil
}
func sendDataRequest<D: Decodable>(_ request: RequestWrapper) async -> (D?, Error?) {
do {
let (data, error) = try await NetworkHandler.sendHTTPRequest(
request,
hostPath: apiPath,
authString: authString
)
if let data = data {
return (JSONDecoder.safeDecode(data), error)
}
return (nil, error)
} catch {
print("An unknown network error occured.")
}
return (nil, NetworkError.unknownError)
}
func sendRequest(_ request: RequestWrapper) async -> Error? {
do {
return try await NetworkHandler.sendHTTPRequest(
request,
hostPath: apiPath,
authString: authString
).1
} catch {
print("An unknown network error occured.")
}
return NetworkError.unknownError
}
}

View File

@@ -77,10 +77,14 @@ struct ApiRequest {
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)")
return (nil, error)
}
if let data = data {
print(data, String(data: data, encoding: .utf8))
}
return (data, nil)
return (data!, nil)
} catch {
let error = decodeURLResponse(response: response as? HTTPURLResponse)
Logger.network.debug("\(method.rawValue) \(path) FAILURE: \(error.debugDescription)")
@@ -92,6 +96,7 @@ struct ApiRequest {
guard let response = response else {
return NetworkError.unknownError
}
print("Status code: ", response.statusCode)
switch response.statusCode {
case 200...299: return (nil)
case 300...399: return (NetworkError.redirectionError)

View File

@@ -132,7 +132,7 @@ protocol CookbookApi {
static func getTags(
from serverAdress: String,
auth: String
) async -> ([String]?, NetworkError?)
) async -> ([RecipeKeyword]?, NetworkError?)
/// Get all recipes tagged with the specified keyword.
/// - Parameters:

View File

@@ -53,7 +53,7 @@ class CookbookApiV1: CookbookApi {
path: "/api/v1/recipes",
method: .POST,
authString: auth,
headerFields: [HeaderField.ocsRequest(value: true), HeaderField.accept(value: .JSON)],
headerFields: [HeaderField.ocsRequest(value: true), HeaderField.accept(value: .JSON), HeaderField.contentType(value: .JSON)],
body: recipeData
)
@@ -95,7 +95,7 @@ class CookbookApiV1: CookbookApi {
path: "/api/v1/recipes/\(recipe.id)",
method: .PUT,
authString: auth,
headerFields: [HeaderField.ocsRequest(value: true), HeaderField.accept(value: .JSON)],
headerFields: [HeaderField.ocsRequest(value: true), HeaderField.accept(value: .JSON), HeaderField.contentType(value: .JSON)],
body: recipeData
)
@@ -170,7 +170,7 @@ class CookbookApiV1: CookbookApi {
return nil
}
static func getTags(from serverAdress: String, auth: String) async -> ([String]?, NetworkError?) {
static func getTags(from serverAdress: String, auth: String) async -> ([RecipeKeyword]?, NetworkError?) {
let request = ApiRequest(
serverAdress: serverAdress,
path: "/api/v1/keywords",