summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadREST/Relay/StoredRelays.swift
blob: cc28c00fd261fdd893c31620f989834554154e66 (plain)
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
//
//  StoredRelays.swift
//  MullvadVPNUITests
//
//  Created by Jon Petersson on 2024-09-09.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import Foundation

/// A struct that represents the relay cache on disk
public struct StoredRelays: Codable, Equatable {
    /// E-tag returned by server
    public let etag: String?

    /// The raw relay JSON data stored within the cache entry
    public let rawData: Data

    /// The date when this cache was last updated
    public let updatedAt: Date

    /// Relays parsed from the JSON data
    public let relays: REST.ServerRelaysResponse

    /// `CachedRelays` representation
    public var cachedRelays: CachedRelays {
        CachedRelays(etag: etag, relays: relays, updatedAt: updatedAt)
    }

    public init(etag: String? = nil, rawData: Data, updatedAt: Date) throws {
        self.etag = etag
        self.rawData = rawData
        self.updatedAt = updatedAt
        relays = try REST.Coding.makeJSONDecoder().decode(REST.ServerRelaysResponse.self, from: rawData)
    }

    public init(cachedRelays: CachedRelays) throws {
        etag = cachedRelays.etag
        rawData = try REST.Coding.makeJSONEncoder().encode(cachedRelays.relays)
        updatedAt = cachedRelays.updatedAt
        relays = cachedRelays.relays
    }
}