forked from mironal/TwitterAPIKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwitterRateLimit.swift
More file actions
44 lines (37 loc) · 1.17 KB
/
TwitterRateLimit.swift
File metadata and controls
44 lines (37 loc) · 1.17 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
import Foundation
/// https://developer.twitter.com/en/docs/rate-limits
/// https://developer.twitter.com/en/docs/twitter-api/v1/rate-limits
/// https://developer.twitter.com/en/docs/twitter-api/rate-limits
public struct TwitterRateLimit {
public let limit: Int
public let remaining: Int
/// UTC epoch seconds
public let reset: TimeInterval
public init?(header: [AnyHashable: Any]) {
guard let limit = parse(header, key: "x-rate-limit-limit"),
let remaining = parse(header, key: "x-rate-limit-remaining"),
let reset = parse(header, key: "x-rate-limit-reset")
else {
return nil
}
self.limit = limit
self.remaining = remaining
self.reset = TimeInterval(reset)
}
}
extension TwitterRateLimit {
public var resetDate: Date {
return Date(timeIntervalSince1970: reset)
}
}
private func parse(_ header: [AnyHashable: Any], key: String) -> Int? {
// Normally, the header is a String.
if let str = header[key] as? String, let value = Int(str) {
return value
}
// just in case.
if let int = header[key] as? Int {
return int
}
return nil
}