summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadRESTTests/Mocks/TimeServerProxy.swift
blob: 5614b0427378a5d1f9c6977a17d8b476da1cc591 (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
//
//  TimeServerProxy.swift
//  MullvadRESTTests
//
//  Created by pronebird on 25/08/2023.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import Foundation

@testable import MullvadREST

/// Simple API proxy used for testing purposes.
final class TimeServerProxy: REST.Proxy<REST.ProxyConfiguration>, @unchecked Sendable {
    init(configuration: REST.ProxyConfiguration) {
        super.init(
            name: "TimeServerProxy",
            configuration: configuration,
            requestFactory: REST.RequestFactory.withDefaultAPICredentials(
                pathPrefix: "",
                bodyEncoder: REST.Coding.makeJSONEncoder()
            ),
            responseDecoder: REST.Coding.makeJSONDecoder()
        )
    }

    func getDateTime() -> any RESTRequestExecutor<TimeResponse> {
        let requestHandler = REST.AnyRequestHandler { endpoint in
            return try self.requestFactory.createRequest(endpoint: endpoint, method: .get, pathTemplate: "date-time")
        }
        let responseHandler = REST.defaultResponseHandler(decoding: TimeResponse.self, with: responseDecoder)

        return makeRequestExecutor(
            name: "get-date-time",
            requestHandler: requestHandler,
            responseHandler: responseHandler
        )
    }
}

struct TimeResponse: Codable {
    var dateTime: Date
}