AMIVApiSession.swift 1.53 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
//
//  AMIVApiSession.swift
//  Amiv
//
//  Created by Domenic Wüthrich on 08.10.18.
//  Copyright © 2018 Amiv an der ETH. All rights reserved.
//

import Foundation

public enum AMIVApiSession: EndPointType {
    
    case authenticate(username: String, password: String)
domenicw's avatar
domenicw committed
14
    case logout
15
16
17
18
19
20
    
}

extension AMIVApiSession {
    
    public var path: String {
domenicw's avatar
domenicw committed
21
22
23
24
25
        switch self {
        case .authenticate:
            return "/sessions"
        case .logout:
            let keychain = KeychainSwift()
26
            guard let id = keychain.get(KeychainKey.userID.rawValue) else {
domenicw's avatar
domenicw committed
27
28
29
30
                return "/sessions"
            }
            return "/sessions/\(id)"
        }
31
32
33
34
35
36
    }
    
    public var httpMethod: HTTPMethod {
        switch self {
        case .authenticate:
            return .post
domenicw's avatar
domenicw committed
37
38
        case .logout:
            return .delete
39
40
41
42
43
44
45
46
        }
    }
    
    public var task: HTTPTask {
        switch self {
        case let .authenticate(username: username, password: password):
            let parameters = ["username": username, "password": password]
            return .requestParameters(bodyParameters: parameters, urlParameter: nil)
domenicw's avatar
domenicw committed
47
48
        case .logout:
            return .request
49
50
51
52
53
        }
    }
    
    public var headers: HTTPHeaders? {
        switch self {
domenicw's avatar
domenicw committed
54
        case .authenticate, .logout:
55
56
57
58
59
60
61
62
            return nil
        }
    }
    
    public var isAuthenticationRequired: Bool {
        switch self {
        case .authenticate:
            return false
domenicw's avatar
domenicw committed
63
64
        case .logout:
            return true
65
66
67
68
        }
    }
    
}