-
Notifications
You must be signed in to change notification settings - Fork 16
/
HttpRequestRunner.swift
86 lines (77 loc) · 3.23 KB
/
HttpRequestRunner.swift
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
/**
Exists to be able to mock http requests so we can test our HttpClient's response handling logic.
*/
public protocol HttpRequestRunner: AutoMockable {
func request(
params: HttpRequestParams,
session: URLSession,
onComplete: @escaping (Data?, HTTPURLResponse?, Error?) -> Void
)
func downloadFile(url: URL, fileType: DownloadFileType, session: URLSession, onComplete: @escaping (URL?) -> Void)
}
// sourcery: InjectRegister = "HttpRequestRunner"
// sourcery: InjectRegisterShared = "HttpRequestRunner"
public class UrlRequestHttpRequestRunner: HttpRequestRunner {
/**
Note: When mocking request, open JSON file, convert to `Data`.
*/
public func request(
params: HttpRequestParams,
session: URLSession,
onComplete: @escaping (Data?, HTTPURLResponse?, Error?) -> Void
) {
var request = URLRequest(url: params.url)
request.httpMethod = params.method
request.httpBody = params.body
params.headers?.forEach { key, value in
request.setValue(value, forHTTPHeaderField: key)
}
session.dataTask(with: request) { data, response, error in
/*
/// uncomment when running HTTP tests on local machine for debugging
print("----------------- HTTP logs start -----------------")
print("\(request.httpMethod) - \(request.url?.absoluteString)")
print("Request body: \(request.httpBody?.string)")
print(data?.string)
print(error?.localizedDescription)
print("----------------- HTTP logs end -----------------")
*/
onComplete(data, response as? HTTPURLResponse, error)
}.resume()
}
public func downloadFile(
url: URL,
fileType: DownloadFileType,
session: URLSession,
onComplete: @escaping (URL?) -> Void
) {
let directoryURL = fileType.directoryToSaveFiles(fileManager: FileManager.default)
session.downloadTask(with: url) { tempLocation, response, _ in
guard let tempLocation = tempLocation, let suggestedFileName = response?.suggestedFilename else {
return onComplete(nil)
}
// create a unique file name so when trying to move temp file to destination it doesn't give an exception
let uniqueFileName = UUID().uuidString + "_" + suggestedFileName
let destinationURL = directoryURL
.appendingPathComponent(uniqueFileName)
do {
// confirm that directories all created because we may have created a new sub-directory
try FileManager.default.createDirectory(
at: directoryURL,
withIntermediateDirectories: true,
attributes: nil
)
// Now attempt the move
try FileManager.default.moveItem(at: tempLocation, to: destinationURL)
} catch {
// XXX: log error when error handling for the customer enabled
return onComplete(nil)
}
onComplete(destinationURL)
}.resume()
}
}