2022-04-14 11:54:53 +02:00
|
|
|
//
|
|
|
|
// ApiClient.swift
|
|
|
|
// App
|
|
|
|
//
|
|
|
|
// Created by Rasmus Krämer on 13.04.22.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Alamofire
|
|
|
|
|
|
|
|
class ApiClient {
|
2022-05-03 12:55:13 +02:00
|
|
|
public static func getData(from url: URL, completion: @escaping (UIImage?) -> Void) {
|
|
|
|
URLSession.shared.dataTask(with: url, completionHandler: {(data, response, error) in
|
|
|
|
if let data = data {
|
|
|
|
completion(UIImage(data:data))
|
|
|
|
}
|
|
|
|
}).resume()
|
|
|
|
}
|
|
|
|
|
2022-04-15 12:21:46 +02:00
|
|
|
public static func postResource<T: Decodable>(endpoint: String, parameters: [String: String], decodable: T.Type = T.self, callback: ((_ param: T) -> Void)?) {
|
2022-04-28 18:05:33 -05:00
|
|
|
if (Store.serverConfig == nil) {
|
|
|
|
NSLog("Server config not set")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:54:53 +02:00
|
|
|
let headers: HTTPHeaders = [
|
2022-04-15 12:21:46 +02:00
|
|
|
"Authorization": "Bearer \(Store.serverConfig!.token)"
|
2022-04-14 11:54:53 +02:00
|
|
|
]
|
|
|
|
|
2022-04-15 12:21:46 +02:00
|
|
|
AF.request("\(Store.serverConfig!.address)/\(endpoint)", method: .post, parameters: parameters, encoder: JSONParameterEncoder.default, headers: headers).responseDecodable(of: decodable) { response in
|
|
|
|
switch response.result {
|
|
|
|
case .success(let obj):
|
|
|
|
callback?(obj)
|
|
|
|
case .failure(let error):
|
|
|
|
NSLog("api request to \(endpoint) failed")
|
|
|
|
print(error)
|
|
|
|
}
|
2022-04-14 11:54:53 +02:00
|
|
|
}
|
|
|
|
}
|
2022-04-15 12:21:46 +02:00
|
|
|
public static func postResource(endpoint: String, parameters: [String: String], callback: ((_ success: Bool) -> Void)?) {
|
2022-04-28 18:05:33 -05:00
|
|
|
if (Store.serverConfig == nil) {
|
|
|
|
NSLog("Server config not set")
|
|
|
|
callback?(false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:54:53 +02:00
|
|
|
let headers: HTTPHeaders = [
|
2022-04-15 10:16:11 +02:00
|
|
|
"Authorization": "Bearer \(Store.serverConfig!.token)"
|
2022-04-14 11:54:53 +02:00
|
|
|
]
|
|
|
|
|
2022-04-15 12:21:46 +02:00
|
|
|
AF.request("\(Store.serverConfig!.address)/\(endpoint)", method: .post, parameters: parameters, encoder: JSONParameterEncoder.default, headers: headers).response { response in
|
2022-04-14 11:54:53 +02:00
|
|
|
switch response.result {
|
2022-04-15 12:21:46 +02:00
|
|
|
case .success(let _):
|
|
|
|
callback?(true)
|
2022-04-14 11:54:53 +02:00
|
|
|
case .failure(let error):
|
|
|
|
NSLog("api request to \(endpoint) failed")
|
|
|
|
print(error)
|
2022-04-15 12:21:46 +02:00
|
|
|
|
|
|
|
callback?(false)
|
2022-04-14 11:54:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-06 18:17:45 -05:00
|
|
|
public static func startPlaybackSession(libraryItemId: String, episodeId: String?, forceTranscode:Bool, callback: @escaping (_ param: PlaybackSession) -> Void) {
|
2022-04-14 11:54:53 +02:00
|
|
|
var endpoint = "api/items/\(libraryItemId)/play"
|
|
|
|
if episodeId != nil {
|
|
|
|
endpoint += "/\(episodeId!)"
|
|
|
|
}
|
|
|
|
|
|
|
|
ApiClient.postResource(endpoint: endpoint, parameters: [
|
2022-05-06 18:17:45 -05:00
|
|
|
"forceDirectPlay": !forceTranscode ? "1" : "",
|
|
|
|
"forceTranscode": forceTranscode ? "1" : "",
|
2022-04-14 11:54:53 +02:00
|
|
|
"mediaPlayer": "AVPlayer",
|
2022-04-14 14:39:09 +02:00
|
|
|
], decodable: PlaybackSession.self) { obj in
|
|
|
|
var session = obj
|
|
|
|
|
2022-04-15 10:16:11 +02:00
|
|
|
session.serverConnectionConfigId = Store.serverConfig!.id
|
|
|
|
session.serverAddress = Store.serverConfig!.address
|
2022-04-14 14:39:09 +02:00
|
|
|
|
|
|
|
callback(session)
|
|
|
|
}
|
2022-04-14 11:54:53 +02:00
|
|
|
}
|
2022-04-15 12:21:46 +02:00
|
|
|
public static func reportPlaybackProgress(report: PlaybackReport, sessionId: String) {
|
|
|
|
try? postResource(endpoint: "api/session/\(sessionId)/sync", parameters: report.asDictionary().mapValues({ value in "\(value)" }), callback: nil)
|
|
|
|
}
|
2022-04-14 11:54:53 +02:00
|
|
|
}
|