init commit,

This commit is contained in:
louiscklaw
2025-05-28 09:55:51 +08:00
commit efe70ceb69
8042 changed files with 951668 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
//
// APIManager.swift
// watchapp Watch App
//
// Created by Alexander Harding on 8/2/23.
//
import Foundation
class APIManager {
static let shared = APIManager()
private var instanceHostname: String?
private init() {}
func setup(instanceHostname: String) {
self.instanceHostname = instanceHostname
}
func savePost(withID postId: Int, authToken: String, completion: @escaping (Result<Void, Error>) -> Void) {
guard let foundInstanceHostname = instanceHostname,
let url = URL(string: "https://\(foundInstanceHostname)/api/v3/post/save")
else {
completion(.failure(NSError(domain: "Invalid URL", code: 0, userInfo: nil)))
return
}
var request = URLRequest(url: url)
request.httpMethod = "PUT"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("Bearer \(authToken)", forHTTPHeaderField: "Authorization") // lemmy >= 0.19
let payload = SavePost(post_id: postId, save: true, auth: authToken)
do {
let jsonData = try JSONEncoder().encode(payload)
request.httpBody = jsonData
} catch {
completion(.failure(error))
return
}
URLSession.shared.dataTask(with: request) { data, response, error in
if let error = error {
completion(.failure(error))
return
}
if let response = response as? HTTPURLResponse, response.statusCode == 200 {
completion(.success(()))
} else {
completion(.failure(NSError(domain: "Unknown error", code: 0, userInfo: nil)))
}
}.resume()
}
}