-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestType.swift
More file actions
47 lines (37 loc) · 1.21 KB
/
RequestType.swift
File metadata and controls
47 lines (37 loc) · 1.21 KB
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
import Foundation
private let privateKey = "d5aaf682f856e3668f0ac674484c3373fb790f9e"
private let publicKey = "a63d192034338073fa3c6f3d8ac2342f"
private let baseURL = "https://gateway.marvel.com/v1/public"
protocol RequestType {
associatedtype ResponseType: Codable
var endpoint: String { get }
var options: [String] { get }
}
extension RequestType {
var url: URL {
let finalEndpoint = options.reduce(endpoint) { acc, opt in
return acc + "&" + opt
}
return urlFromEndpoint(finalEndpoint)
}
private func urlFromEndpoint(_ endpoint: String) -> URL {
return URL(string: baseURL + endpoint + urlPrefix)!
}
private var urlPrefix: String {
let date = Date().string
return "&ts=\(date)&hash=\(hashFromDate(date))&apikey=\(publicKey)"
}
private func hashFromDate(_ date: String) -> String {
return (date + privateKey + publicKey).utf8.md5.rawValue
}
}
extension Date {
private static let formatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd-HH-mm-ss"
return formatter
}()
var string: String {
return Date.formatter.string(from: self)
}
}