-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMockFormbricksService.swift
More file actions
43 lines (36 loc) · 1.42 KB
/
MockFormbricksService.swift
File metadata and controls
43 lines (36 loc) · 1.42 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
import UIKit
@testable import FormbricksSDK
/// This can be extended later when needed
enum MockResponse: String {
case environment = "Environment"
case user = "User"
}
class MockFormbricksService: FormbricksService {
var isErrorResponseNeeded = false
override func getEnvironmentState(completion: @escaping (ResultType<GetEnvironmentRequest.Response>) -> Void) {
if isErrorResponseNeeded {
completion(.failure(RuntimeError(message: "")))
} else {
execute(.environment, completion: completion)
}
}
override func postUser(id: String, attributes: [String : String]?, completion: @escaping (ResultType<PostUserRequest.Response>) -> Void) {
if isErrorResponseNeeded {
completion(.failure(RuntimeError(message: "")))
} else {
execute(.user, completion: completion)
}
}
func execute<T: Decodable>(_ response: MockResponse, completion: @escaping (ResultType<T>) -> Void) {
guard let url = Bundle.module.url(forResource: response.rawValue, withExtension: "json"), let data = try? Data(contentsOf: url) else {
completion(.failure(RuntimeError(message: "Unable to parse response")))
return
}
do {
let body = try JSONDecoder.iso8601Full.decode(T.self, from: data)
completion(.success(body))
} catch {
completion(.failure(error))
}
}
}