2020-02-18 21:43:30 +00:00
|
|
|
//
|
|
|
|
// UserApi.swift
|
2022-08-07 13:31:15 +01:00
|
|
|
// Mixonomer
|
2020-02-18 21:43:30 +00:00
|
|
|
//
|
|
|
|
// Created by Andy Pack on 18/02/2020.
|
|
|
|
// Copyright © 2020 Sarsoo. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Alamofire
|
|
|
|
import SwiftyJSON
|
|
|
|
|
|
|
|
public enum UserApi {
|
|
|
|
case getUser
|
2022-08-11 20:53:09 +01:00
|
|
|
case deleteUser
|
2020-02-18 21:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extension UserApi: ApiRequest {
|
|
|
|
var domain: String {
|
|
|
|
return ApiRequestDefaults.domain
|
|
|
|
}
|
|
|
|
|
|
|
|
var path: String {
|
|
|
|
switch self {
|
|
|
|
case .getUser:
|
|
|
|
return "api/user"
|
2022-08-11 20:53:09 +01:00
|
|
|
case .deleteUser:
|
|
|
|
return "api/user"
|
2020-02-18 21:43:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var httpMethod: Alamofire.HTTPMethod {
|
|
|
|
switch self {
|
|
|
|
case .getUser:
|
|
|
|
return .get
|
2022-08-11 20:53:09 +01:00
|
|
|
case .deleteUser:
|
|
|
|
return .delete
|
2020-02-18 21:43:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var parameters: JSON? {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var parameterType: ParameterEncoder? {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var headers: HTTPHeaders? {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var authMethod: AuthMethod? {
|
|
|
|
return ApiRequestDefaults.authMethod
|
|
|
|
}
|
|
|
|
|
2022-08-11 20:53:09 +01:00
|
|
|
static func fromJSON(user: Data) -> User? {
|
|
|
|
|
|
|
|
let decoder = JSONDecoder()
|
|
|
|
do {
|
|
|
|
let user = try decoder.decode(User.self, from: user)
|
|
|
|
return user
|
|
|
|
} catch {
|
|
|
|
print(error)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
static func fromJSON(user: JSON) -> User? {
|
|
|
|
|
|
|
|
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 {
|
|
|
|
print(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-02-18 21:43:30 +00: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)
|
|
|
|
if let returned = _iter {
|
|
|
|
_users.append(returned)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _users
|
|
|
|
}
|
2020-02-18 21:43:30 +00:00
|
|
|
}
|