Simpler api endpoints are now integrated into the MainViewModel
This commit is contained in:
@@ -40,8 +40,9 @@ struct ApiRequest {
|
||||
Logger.network.debug("\(method.rawValue) \(path) sending ...")
|
||||
|
||||
// Prepare URL
|
||||
let urlString = serverAddress + cookbookPath + path
|
||||
Logger.network.debug("Full path: \(urlString)")
|
||||
let urlString = "https://" + serverAddress + cookbookPath + 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) }
|
||||
|
||||
@@ -76,6 +77,9 @@ struct ApiRequest {
|
||||
do {
|
||||
(data, response) = try await URLSession.shared.data(for: request)
|
||||
Logger.network.debug("\(method.rawValue) \(path) SUCCESS!")
|
||||
if let data = data {
|
||||
print(data, String(data: data, encoding: .utf8))
|
||||
}
|
||||
return (data, nil)
|
||||
} catch {
|
||||
let error = decodeURLResponse(response: response as? HTTPURLResponse)
|
||||
|
||||
@@ -49,7 +49,8 @@ protocol CookbookApi {
|
||||
/// - Returns: A NetworkError if the request fails. Nil otherwise.
|
||||
static func createRecipe(
|
||||
from serverAdress: String,
|
||||
auth: String
|
||||
auth: String,
|
||||
recipe: RecipeDetail
|
||||
) async -> (NetworkError?)
|
||||
|
||||
/// Get the recipe with the specified id.
|
||||
@@ -94,7 +95,7 @@ protocol CookbookApi {
|
||||
static func getCategories(
|
||||
from serverAdress: String,
|
||||
auth: String
|
||||
) async -> ([String]?, NetworkError?)
|
||||
) async -> ([Category]?, NetworkError?)
|
||||
|
||||
/// Get all recipes of a specified category.
|
||||
/// - Parameters:
|
||||
@@ -185,3 +186,6 @@ protocol CookbookApi {
|
||||
) async -> (NetworkError?)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -43,13 +43,18 @@ class CookbookApiV1: CookbookApi {
|
||||
return (JSONDecoder.safeDecode(data), nil)
|
||||
}
|
||||
|
||||
static func createRecipe(from serverAdress: String, auth: String) async -> (NetworkError?) {
|
||||
static func createRecipe(from serverAdress: String, auth: String, recipe: RecipeDetail) async -> (NetworkError?) {
|
||||
guard let recipeData = JSONEncoder.safeEncode(recipe) else {
|
||||
return .dataError
|
||||
}
|
||||
|
||||
let request = ApiRequest(
|
||||
serverAdress: serverAdress,
|
||||
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)],
|
||||
body: recipeData
|
||||
)
|
||||
|
||||
let (data, error) = await request.send()
|
||||
@@ -119,7 +124,7 @@ class CookbookApiV1: CookbookApi {
|
||||
return nil
|
||||
}
|
||||
|
||||
static func getCategories(from serverAdress: String, auth: String) async -> ([String]?, NetworkError?) {
|
||||
static func getCategories(from serverAdress: String, auth: String) async -> ([Category]?, NetworkError?) {
|
||||
let request = ApiRequest(
|
||||
serverAdress: serverAdress,
|
||||
path: "/api/v1/categories",
|
||||
|
||||
Reference in New Issue
Block a user