|
| 1 | +// |
| 2 | +// UtilViewModel.swift |
| 3 | +// testApp (iOS) |
| 4 | +// |
| 5 | +// Created by Anna Huller on 6/14/22. |
| 6 | +// |
| 7 | + |
| 8 | +import SwiftUI |
| 9 | +import NewRelic |
| 10 | + |
| 11 | +extension UtilityView { |
| 12 | + @MainActor class ViewModel: ObservableObject { |
| 13 | + |
| 14 | + let title = "Utility" |
| 15 | + @Published var numBreadcrumbs = 0 |
| 16 | + @Published var goodAttribute = false |
| 17 | + @Published var badAttribute = false |
| 18 | + @Published var attributes = "" |
| 19 | + @Published var events = 0 |
| 20 | + |
| 21 | + var uniqueInteractionTraceIdentifier: String? = nil |
| 22 | + |
| 23 | + let taskProcessor = TaskProcessor() |
| 24 | + |
| 25 | + func crash() { |
| 26 | + NewRelic.crashNow("New Relic intentionally crashed to test Utils") |
| 27 | + } |
| 28 | + func hugeCrashReport() { |
| 29 | + let crashOutputFilePath = String(format: "%@%@/%d.%@", NSTemporaryDirectory(), "nrcrashreports", 42, "nrcrashreport") |
| 30 | + |
| 31 | + let data = makeBigDictionary() |
| 32 | + do { |
| 33 | + try FileManager.default.createDirectory(atPath: String(format: "%@/%@", NSTemporaryDirectory(), "nrcrashreports"), withIntermediateDirectories: true) |
| 34 | + |
| 35 | + let success = FileManager.default.createFile(atPath: crashOutputFilePath, contents: data) |
| 36 | + |
| 37 | + } catch { |
| 38 | + print(error.localizedDescription) |
| 39 | + } |
| 40 | + } |
| 41 | + func removeAttributes() -> Bool{ |
| 42 | + return NewRelic.removeAllAttributes() |
| 43 | + } |
| 44 | + func setAttributes(){ |
| 45 | + attributes = "test1: " + String(NewRelic.setAttribute("test1", value: 1)) + " '': " + String(NewRelic.setAttribute("", value: 2)) |
| 46 | + } |
| 47 | + func makeError(){ |
| 48 | + do { |
| 49 | + try errorMethod() |
| 50 | + } catch { |
| 51 | + NewRelic.recordError(error) |
| 52 | + |
| 53 | + } |
| 54 | + } |
| 55 | + private func errorMethod() throws { |
| 56 | + throw CancellationError.init() |
| 57 | + } |
| 58 | + func makeBreadcrumb(name: String, attributes: Dictionary<String, Any>){ |
| 59 | + let madeBreadCrumb = NewRelic.recordBreadcrumb(name, |
| 60 | + attributes: attributes) |
| 61 | + if madeBreadCrumb == true { |
| 62 | + self.numBreadcrumbs += 1 |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + func makeEvent(){ |
| 67 | + let madeEvent = NewRelic.recordCustomEvent("ButtonPress") |
| 68 | + if madeEvent == true { |
| 69 | + events += 1 |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + func make100Events() { |
| 74 | + for i in 0...100 { |
| 75 | + NewRelic.recordCustomEvent("ButtonPress") |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + func sendRedirectRequest() { |
| 80 | + guard let url = URL(string: "https://easynvest.com.br") else { return } |
| 81 | + |
| 82 | + var request = URLRequest(url: url) |
| 83 | + request.httpMethod = "GET" |
| 84 | + |
| 85 | + let task = URLSession.shared.dataTask(with: request) { data, response, error in |
| 86 | + print("ok") |
| 87 | + } |
| 88 | + task.resume() |
| 89 | + } |
| 90 | + func stopInteractionTrace() { |
| 91 | + guard let identifier = uniqueInteractionTraceIdentifier else { |
| 92 | + print("no interaction to stop...") |
| 93 | + return |
| 94 | + } |
| 95 | + NewRelic.stopCurrentInteraction(identifier) |
| 96 | + |
| 97 | + uniqueInteractionTraceIdentifier = nil |
| 98 | + } |
| 99 | + |
| 100 | + func startInteractionTrace() { |
| 101 | + uniqueInteractionTraceIdentifier = NewRelic.startInteraction(withName: "myInteractionName") |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + func noticeFailedNWRequest() { |
| 107 | + NewRelic.noticeNetworkFailure(for: URL(string: "https://www.google.com"), httpMethod: "GET", |
| 108 | + with: NRTimer(), andFailureCode: NSURLErrorTimedOut) |
| 109 | + } |
| 110 | + |
| 111 | + func noticeNWRequest() { |
| 112 | + // NewRelic.noticeNetworkRequest(for: URL(string: "https://www.google.com"), httpMethod: "GET", with: NRTimer(), responseHeaders: [:], |
| 113 | + // statusCode: 200, bytesSent: 1000, bytesReceived: 1000, responseData: Data(), traceHeaders: nil, andParams: nil) |
| 114 | + |
| 115 | + NewRelic.noticeNetworkRequest(for: URL(string:"https://fakeurl.com"), |
| 116 | + httpMethod: "GET", |
| 117 | + startTime: Date().timeIntervalSince1970, |
| 118 | + endTime: Date().timeIntervalSince1970, |
| 119 | + responseHeaders: nil, |
| 120 | + statusCode: 400, |
| 121 | + bytesSent: 100, |
| 122 | + bytesReceived: 200, |
| 123 | + responseData: Data("example response body".utf8), |
| 124 | + traceHeaders: nil, |
| 125 | + andParams: nil) |
| 126 | + } |
| 127 | + |
| 128 | + func setBuild() { |
| 129 | + NewRelic.setApplicationBuild("42") |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | + func doDataTask() { |
| 134 | + let urlSession = URLSession(configuration: URLSession.shared.configuration, delegate: taskProcessor, delegateQueue: nil) |
| 135 | + guard let url = URL(string: "https://www.google.com") else { return } |
| 136 | + |
| 137 | + let request = URLRequest(url: url) |
| 138 | + |
| 139 | + let dataTask = urlSession.dataTask(with: request) |
| 140 | + |
| 141 | + dataTask.resume() |
| 142 | + } |
| 143 | + |
| 144 | + func doDataTaskWithCompletionHandler() { |
| 145 | + let urlSession = URLSession(configuration: URLSessionConfiguration.default) |
| 146 | + guard let url = URL(string: "https://www.google.com") else { return } |
| 147 | + |
| 148 | + let request = URLRequest(url: url) |
| 149 | + |
| 150 | + let dataTask = urlSession.dataTask(with: request) { data, response, error in |
| 151 | + //Handle |
| 152 | + if let httpResponse = response as? HTTPURLResponse { |
| 153 | + print("SUCCESS w/ dataTask w/ completionHandler") |
| 154 | + |
| 155 | + } |
| 156 | + else if let errorCode = error?._code { |
| 157 | + print(error?.localizedDescription) |
| 158 | + |
| 159 | + } |
| 160 | + else { |
| 161 | + |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + dataTask.resume() |
| 166 | + urlSession.finishTasksAndInvalidate() |
| 167 | + } |
| 168 | + |
| 169 | + func makeBigDictionary() -> Data { |
| 170 | + var dictionary = [String:String]() |
| 171 | + var data = Data() |
| 172 | + for i in 0...30000 { |
| 173 | + dictionary.updateValue("42", forKey: "The meaning of life #"+String(i)) |
| 174 | + } |
| 175 | + do { |
| 176 | + data = try JSONSerialization.data(withJSONObject: dictionary) |
| 177 | + } catch { |
| 178 | + print(error.localizedDescription) |
| 179 | + } |
| 180 | + return data |
| 181 | + } |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +class TaskProcessor: NSObject, URLSessionDelegate, URLSessionDataDelegate, URLSessionTaskDelegate { |
| 186 | + |
| 187 | + public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) { |
| 188 | + |
| 189 | + completionHandler(.allow) |
| 190 | + } |
| 191 | + |
| 192 | + public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) { |
| 193 | + print("DataTask rcv data.") |
| 194 | + } |
| 195 | + |
| 196 | + public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) { |
| 197 | + print("DataTask did complete.") |
| 198 | + } |
| 199 | + |
| 200 | + public func urlSession(_ session: URLSession, |
| 201 | + task: URLSessionTask, |
| 202 | + didReceive challenge: URLAuthenticationChallenge, |
| 203 | + completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) { |
| 204 | +// completionHandler(.performDefaultHandling, nil) |
| 205 | + |
| 206 | + //completionHandler(.cancelAuthenticationChallenge, nil) |
| 207 | + |
| 208 | + if let trust = challenge.protectionSpace.serverTrust, |
| 209 | + SecTrustGetCertificateCount(trust) > 0 { |
| 210 | + if let certificate = SecTrustGetCertificateAtIndex(trust, 0) { |
| 211 | + let data = SecCertificateCopyData(certificate) as Data |
| 212 | + |
| 213 | + completionHandler(.useCredential, URLCredential(trust: trust)) |
| 214 | + return |
| 215 | + } |
| 216 | + |
| 217 | + } |
| 218 | + completionHandler(.cancelAuthenticationChallenge, nil) |
| 219 | + |
| 220 | + |
| 221 | + } |
| 222 | +} |
0 commit comments