-
Notifications
You must be signed in to change notification settings - Fork 5
/
BsuirApi.swift
118 lines (89 loc) · 2.93 KB
/
BsuirApi.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import Foundation
extension RequestsManager {
public static func bsuir(session: URLSession = .cached, logger: Logger? = nil) -> RequestsManager {
return RequestsManager(base: "https://journal.bsuir.by/api/v1", session: session, decoder: decoder, logger: logger)
}
}
extension URLSession {
public static let cached: URLSession = {
var configuration = URLSessionConfiguration.default
configuration.requestCachePolicy = .returnCacheDataElseLoad
return URLSession(configuration: configuration)
}()
}
public enum BsuirTargets {
public enum Agent {
case groupName(String)
case groupID(Int)
}
public struct Schedule: Target {
public typealias Value = Group.Schedule
public let path = "/studentGroup/schedule"
public let parameters: [String: String]
public init(agent: Agent) {
parameters = agent.parameters
}
}
public struct EmployeeSchedule: Target {
public typealias Value = Employee.Schedule
public let path = "/portal/employeeSchedule"
public let parameters: [String: String]
public init(id: Int) {
parameters = ["employeeId": String(id)]
}
}
public struct FindEmployee: Target {
public typealias Value = [Employee]
public let path = "/portal/employeeSchedule/employee"
public let parameters: [String: String]
init(query: String) {
parameters = ["employeeFio": query]
}
}
public struct LastUpdate: Target {
public struct Value: Decodable {
public let lastUpdateDate: Date
}
public let path = "/studentGroup/lastUpdateDate"
public let parameters: [String: String]
public init(agent: Agent) {
parameters = agent.parameters
}
}
public struct Groups: Target {
public typealias Value = [Group]
public let path = "/groups"
public init() {}
}
public struct Employees: Target {
public typealias Value = [Employee]
public let path = "/employees"
public init() {}
}
public struct Week: Target {
public typealias Value = Int
public let path = "/portal/schedule/week"
public init() {}
}
}
private extension RequestsManager {
private static let decoder: JSONDecoder = {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .formatted(dateFormatter)
return decoder
}()
private static let dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "dd.MM.yyyy"
formatter.timeZone = .minsk
return formatter
}()
}
private extension BsuirTargets.Agent {
var parameters: [String: String] {
switch self {
case .groupID(let id): return ["id": String(id)]
case .groupName(let name): return ["studentGroup": name]
}
}
}