Replace lazy AppStorage calls with AccountManager

This commit is contained in:
2025-04-25 14:26:05 +02:00
parent d3143915ea
commit 351f08e43e
16 changed files with 392 additions and 270 deletions
+79
View File
@@ -0,0 +1,79 @@
//
// MeteClient.swift
// meterios
//
// (c) 2025 Martin "maride" Dessauer
//
import Foundation
enum MeteClientError: Error {
case StatusCodeLooksBad(explanation: String)
}
/// **MeteClient** is the client-side implementation of the Space Market API. Pure Swift, no platform-specific frameworks used.
class MeteClient {
private let address: String
private let userID: Int
convenience init(account: Account) {
self.init(address: account.GetAddress(), userID: account.GetUserID())
}
init(address: String, userID: Int) {
self.address = address
self.userID = userID
}
// GetDrinks returns the list of drinks available at the backend
func GetDrinks() async throws -> Array<Drink> {
// Send HTTP request to the drinks endpoint
let url = URL(string: "https://\(address)/api/v1/drinks")
let (data, _) = try await URLSession.shared.data(from: url!)
// Decode JSON to array of drinks
let decoder = JSONDecoder()
let drinks = try decoder.decode([Drink].self, from: data)
return drinks
}
// GetUser returns the user object for the given ID
func GetUser() async throws -> User {
// Send HTTP request to the user endpoint
let url = URL(string: "https://\(address)/api/v1/users/\(userID).json")
let (data, _) = try await URLSession.shared.data(from: url!)
// Decode JSON to array of drinks
let decoder = JSONDecoder()
let user = try decoder.decode(User.self, from: data)
return user
}
// GetUsers returns all users registered at the backend
func GetUsers() async throws -> Array<User> {
// Send HTTP request to the user endpoint
let url = URL(string: "https://\(address)/api/v1/users.json")
let (data, _) = try await URLSession.shared.data(from: url!)
// Decode JSON to array of drinks
let decoder = JSONDecoder()
let users = try decoder.decode([User].self, from: data)
return users
}
// BookDrink subtracts the recommended price off the balance from the user given by ID
func BookDrink(_ drinkID: Int) async throws {
// Send HTTP request to the user endpoint
let url = URL(string: "https://\(address)/api/v1/users/\(userID)/buy.json?drink=\(drinkID)")
let (_, response) = try await URLSession.shared.data(from: url!)
// Check status code
let statusCode = (response as? HTTPURLResponse)?.statusCode
if statusCode != 204 {
throw MeteClientError.StatusCodeLooksBad(explanation: "Failed to book drink \(drinkID) for user \(userID)@\(address): HTTP status code \(statusCode) received for \(url)")
}
}
}