85 lines
3.4 KiB
Swift
85 lines
3.4 KiB
Swift
import Foundation
|
|
import RxSwift
|
|
|
|
class Api {
|
|
private static let baseUrl = "https://vps.aliencat.pro:8443/"
|
|
|
|
private static func genError(_ msg: String, suggestion: String, code: Int = 0) -> Error {
|
|
return NSError(domain: "", code: code, userInfo: [NSLocalizedDescriptionKey: msg, NSLocalizedRecoverySuggestionErrorKey: suggestion])
|
|
}
|
|
|
|
private static func createRequest<T>(api: String, method: String, body: [String: T]? = nil) -> URLRequest? where T: LosslessStringConvertible {
|
|
guard var urlComponents = URLComponents(string: baseUrl + api) else { return nil }
|
|
|
|
if let body = body, method.uppercased() == "GET" {
|
|
urlComponents.queryItems = body.map { URLQueryItem(name: $0, value: String($1)) }
|
|
}
|
|
|
|
var request = URLRequest(url: urlComponents.url!)
|
|
request.httpMethod = method
|
|
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
|
|
request.addValue("application/json", forHTTPHeaderField: "Accept")
|
|
request.addValue("Bearer " + Settings.shared.user.token, forHTTPHeaderField: "Authorization")
|
|
|
|
if let body = body, method.uppercased() != "GET" {
|
|
request.httpBody = try? JSONSerialization.data(withJSONObject: body, options: .prettyPrinted)
|
|
}
|
|
|
|
return request
|
|
}
|
|
|
|
private static func makeRequest<T, U>(api: String, method: String, body: [String: U]? = nil) -> Observable<T> where T: Decodable, U: LosslessStringConvertible {
|
|
guard let request = self.createRequest(api: api, method: method, body: body) else {
|
|
return Observable.error(self.genError("Error creating request", suggestion: ""))
|
|
}
|
|
|
|
return URLSession.shared.rx.data(request: request).map { data in
|
|
// let str = String(data: data, encoding: .utf8)
|
|
// print("================================")
|
|
// print(str)
|
|
// print("================================")
|
|
let resp = try JSONDecoder().decode(Response<T>.self, from: data)
|
|
if resp.success {
|
|
return resp.data!
|
|
} else {
|
|
throw self.genError(resp.error!, suggestion: "")
|
|
}
|
|
}
|
|
}
|
|
|
|
public static func login(username: String, password: String) -> Observable<User> {
|
|
let body = [
|
|
"login": username,
|
|
"password": password
|
|
]
|
|
return self.makeRequest(api: "user/login", method: "POST", body: body)
|
|
}
|
|
|
|
public static func signup(username: String, password: String) -> Observable<User> {
|
|
let body = [
|
|
"login": username,
|
|
"password": password
|
|
]
|
|
return self.makeRequest(api: "user/signup", method: "POST", body: body)
|
|
}
|
|
|
|
public static func getVehicles(with filter: Filter) -> Observable<[Vehicle]> {
|
|
let body = [
|
|
"limit": "0", // Unlimited
|
|
"query": filter.searchString
|
|
]
|
|
return self.makeRequest(api: "vehicles", method: "GET", body: body)
|
|
}
|
|
|
|
public static func checkVehicle(by number: String) -> Observable<Vehicle> {
|
|
if let token = Settings.shared.user.googleIdToken, let jwt = JWT(string: token) {
|
|
|
|
}
|
|
|
|
return self.makeRequest(api: "vehicles/check", method: "POST", body: ["number": number]).map { (vehicle: Vehicle) -> Vehicle in
|
|
vehicle.addedDate = Date().timeIntervalSince1970*1000
|
|
return vehicle
|
|
}
|
|
}
|
|
}
|