Revision control

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
import Foundation
6
import Shared
7
import SwiftyJSON
8
9
open class InfoCollections {
10
fileprivate let collections: [String: Timestamp]
11
12
init(collections: [String: Timestamp]) {
13
self.collections = collections
14
}
15
16
open class func fromJSON(_ json: JSON) -> InfoCollections? {
17
if let dict = json.dictionary {
18
var coll = [String: Timestamp]()
19
for (key, value) in dict {
20
if let value = value.double {
21
coll[key] = Timestamp(value * 1000)
22
} else {
23
return nil // Invalid, so bail out.
24
}
25
}
26
return InfoCollections(collections: coll)
27
}
28
return nil
29
}
30
31
open func collectionNames() -> [String] {
32
return Array(self.collections.keys)
33
}
34
35
open func modified(_ collection: String) -> Timestamp? {
36
return self.collections[collection]
37
}
38
39
// Two I/Cs are the same if they have the same modified times for a set of
40
// collections. If no collections are specified, they're considered the same
41
// if the other I/C has the same values for this I/C's collections, and
42
// they have the same collection array.
43
open func same(_ other: InfoCollections, collections: [String]?) -> Bool {
44
if let collections = collections {
45
return collections.every({ self.modified($0) == other.modified($0) })
46
}
47
48
// Same collections?
49
let ours = self.collectionNames()
50
let theirs = other.collectionNames()
51
return ours.sameElements(theirs) && same(other, collections: ours)
52
}
53
}
54
55
// Response object from https://<sync-endpoint-url>/info/configuration
56
public struct InfoConfiguration {
57
58
// Maximum size in bytes of the overall HTTP request body.
59
public let maxRequestBytes: Int
60
61
// Maximum number of records that can be uploaded to a collection in a single POST request.
62
public let maxPostRecords: Int
63
64
// Maximum combined size in bytes of the record payloads that can be uploaded to a collection in
65
// a single POST request.
66
public let maxPostBytes: Int
67
68
// Maximum total number of records that can be uploaded to a collection as part of a batched upload.
69
public let maxTotalRecords: Int
70
71
// Maximum total combined size in bytes of the record payloads that can be uploaded to a collection
72
// as part of a batched upload.
73
public let maxTotalBytes: Int
74
}
75