113 lines
2.7 KiB
Swift
113 lines
2.7 KiB
Swift
//
|
|
// APIService.swift
|
|
// APIService
|
|
//
|
|
// Created by Javier Cicchelli on 04/12/2022.
|
|
// Copyright © 2022 Röck+Cöde. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
public actor APIService {
|
|
|
|
// MARK: Properties
|
|
|
|
private let client: Client
|
|
|
|
// MARK: Initialisers
|
|
|
|
public init(configuration: URLSessionConfiguration = .default) {
|
|
self.client = APIClient(configuration: configuration)
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - Service
|
|
|
|
extension APIService: Service {
|
|
public func getUser(
|
|
credentials: Credentials
|
|
) async throws -> Me {
|
|
try await client.request(
|
|
endpoint: GetMeEndpoint(
|
|
username: credentials.username,
|
|
password: credentials.password
|
|
),
|
|
model: Me.self
|
|
)
|
|
}
|
|
|
|
public func getItems(
|
|
id: String,
|
|
credentials: Credentials
|
|
) async throws -> [Item] {
|
|
try await client.request(
|
|
endpoint: GetItemsEndpoint(
|
|
itemId: id,
|
|
username: credentials.username,
|
|
password: credentials.password
|
|
),
|
|
model: [Item].self
|
|
)
|
|
}
|
|
|
|
public func getData(
|
|
id: String,
|
|
credentials: Credentials
|
|
) async throws -> Data {
|
|
try await client.request(
|
|
endpoint: GetDataEndpoint(
|
|
itemId: id,
|
|
username: credentials.username,
|
|
password: credentials.password
|
|
)
|
|
)
|
|
}
|
|
|
|
public func createFolder(
|
|
id: String,
|
|
name: String,
|
|
credentials: Credentials
|
|
) async throws -> Item {
|
|
try await client.request(
|
|
endpoint: CreateFolderEndpoint(
|
|
itemId: id,
|
|
folderName: name,
|
|
username: credentials.username,
|
|
password: credentials.password
|
|
),
|
|
model: Item.self
|
|
)
|
|
}
|
|
|
|
public func uploadFile(
|
|
id: String,
|
|
file: File,
|
|
credentials: Credentials
|
|
) async throws -> Item {
|
|
try await client.request(
|
|
endpoint: UploadFileEndpoint(
|
|
itemId: id,
|
|
fileName: file.name,
|
|
fileData: file.data,
|
|
username: credentials.username,
|
|
password: credentials.password
|
|
),
|
|
model: Item.self
|
|
)
|
|
}
|
|
|
|
public func deleteItem(
|
|
id: String,
|
|
credentials: Credentials
|
|
) async throws {
|
|
try await client.request(
|
|
endpoint: DeleteItemEndpoint(
|
|
itemId: id,
|
|
username: credentials.username,
|
|
password: credentials.password
|
|
)
|
|
)
|
|
}
|
|
}
|