Mixonomer-iOS/Mixonomer/Network/UserApi.swift

137 lines
3.6 KiB
Swift
Raw Normal View History

//
// UserApi.swift
2022-08-07 13:31:15 +01:00
// Mixonomer
//
// Created by Andy Pack on 18/02/2020.
// Copyright © 2020 Sarsoo. All rights reserved.
//
import Foundation
import Alamofire
import SwiftyJSON
2022-11-23 23:39:58 +00:00
import OSLog
public enum UserApi {
case getUser
2022-08-16 18:01:17 +01:00
case updateUser(updates: JSON)
2022-08-11 20:53:09 +01:00
case deleteUser
2022-12-09 08:51:42 +00:00
case passAPNSToken(updates: String)
case updateNotify(state: Bool)
case updateNotifyPlaylist(state: String)
case updateNotifyTag(state: String)
case updateNotifyAdmin(state: String)
}
extension UserApi: ApiRequest {
var domain: String {
return ApiRequestDefaults.domain
}
var path: String {
switch self {
case .getUser:
return "api/user"
2022-08-16 18:01:17 +01:00
case .updateUser:
return "api/user"
2022-08-11 20:53:09 +01:00
case .deleteUser:
return "api/user"
2022-12-09 08:51:42 +00:00
case .passAPNSToken:
return "api/user"
case .updateNotify, .updateNotifyPlaylist, .updateNotifyTag, .updateNotifyAdmin:
return "api/user"
}
}
var httpMethod: Alamofire.HTTPMethod {
switch self {
case .getUser:
return .get
2022-08-16 18:01:17 +01:00
case .updateUser:
return .post
2022-08-11 20:53:09 +01:00
case .deleteUser:
return .delete
2022-12-09 08:51:42 +00:00
case .passAPNSToken:
return .post
case .updateNotify, .updateNotifyPlaylist, .updateNotifyTag, .updateNotifyAdmin:
return .post
}
}
var parameters: JSON? {
2022-08-16 18:01:17 +01:00
switch self {
case .getUser, .deleteUser:
return nil
case .updateUser(let updates):
return updates
2022-12-09 08:51:42 +00:00
case .passAPNSToken(let token):
return JSON(["apns_token": token])
case .updateNotify(let state):
return JSON(["notify": state])
case .updateNotifyPlaylist(let state):
return JSON(["notify_playlist_updates": state])
case .updateNotifyTag(let state):
return JSON(["notify_tag_updates": state])
case .updateNotifyAdmin(let state):
return JSON(["notify_admins": state])
2022-08-16 18:01:17 +01:00
}
}
var parameterType: ParameterEncoder? {
2022-08-16 18:01:17 +01:00
switch self {
case .getUser, .deleteUser:
return nil
2022-12-09 08:51:42 +00:00
case .updateUser, .passAPNSToken:
return JSONParameterEncoder.default
case .updateNotify, .updateNotifyPlaylist, .updateNotifyTag, .updateNotifyAdmin:
2022-08-16 18:01:17 +01:00
return JSONParameterEncoder.default
}
}
var headers: HTTPHeaders? {
return nil
}
var authMethod: AuthMethod? {
return ApiRequestDefaults.authMethod
}
2022-12-09 08:51:42 +00:00
static func fromJSON(user: Data) -> User {
2022-08-11 20:53:09 +01:00
let decoder = JSONDecoder()
do {
let user = try decoder.decode(User.self, from: user)
return user
} catch {
2022-11-23 23:39:58 +00:00
Logger.parse.error("error parsing user from json: \(error)")
2022-12-09 08:51:42 +00:00
return User.get_null_user()
2022-08-11 20:53:09 +01:00
}
}
2022-12-09 08:51:42 +00:00
static func fromJSON(user: JSON) -> User {
2022-08-11 20:53:09 +01:00
let _json = user.rawString()?.data(using: .utf8)
if let data = _json {
let decoder = JSONDecoder()
do {
let user = try decoder.decode(User.self, from: data)
return user
} catch {
2022-11-23 23:39:58 +00:00
Logger.parse.error("error parsing user from json: \(error)")
2022-08-11 20:53:09 +01:00
}
}
2022-12-09 08:51:42 +00:00
return User.get_null_user()
2022-08-11 20:53:09 +01:00
}
2022-08-11 20:53:09 +01:00
static func fromJSON(user: [JSON]) -> [User] {
var _users: [User] = []
for dict in user {
let _iter = self.fromJSON(user: dict)
2022-12-09 08:51:42 +00:00
_users.append(_iter)
2022-08-11 20:53:09 +01:00
}
return _users
}
}