Skip to content

Commit baa66a3

Browse files
committed
Initial commit
0 parents  commit baa66a3

File tree

985 files changed

+106659
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

985 files changed

+106659
-0
lines changed
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Pod::Spec.new do |s|
2+
s.name = 'AlamofireNetworkActivityLogger'
3+
s.version = '2.4.0'
4+
s.license = 'MIT'
5+
s.summary = 'Network request logger for Alamofire'
6+
s.homepage = 'https://github.com/konkab/AlamofireNetworkActivityLogger'
7+
s.social_media_url = 'https://www.linkedin.com/in/konstantinkabanov'
8+
s.authors = { 'Konstantin Kabanov' => '[email protected]' }
9+
10+
s.source = { :git => 'https://github.com/konkab/AlamofireNetworkActivityLogger.git', :tag => s.version }
11+
s.source_files = 'AlamofireNetworkActivityLogger/Source/*.swift'
12+
13+
s.ios.deployment_target = '9.0'
14+
s.osx.deployment_target = '10.11'
15+
s.tvos.deployment_target = '9.0'
16+
s.watchos.deployment_target = '2.0'
17+
18+
s.dependency 'Alamofire', '~> 4.8'
19+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
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+
import Alamofire
28+
import Foundation
29+
30+
public enum NetworkActivityLoggerLevel {
31+
32+
case off
33+
34+
case debug
35+
36+
case info
37+
38+
case warn
39+
40+
case error
41+
42+
case fatal
43+
}
44+
45+
public class NetworkActivityLogger {
46+
47+
48+
public static let shared = NetworkActivityLogger()
49+
50+
public var level: NetworkActivityLoggerLevel
51+
52+
public var filterPredicate: NSPredicate?
53+
54+
private var startDates: [URLSessionTask: Date]
55+
56+
57+
init() {
58+
level = .info
59+
startDates = [URLSessionTask: Date]()
60+
}
61+
62+
deinit {
63+
stopLogging()
64+
}
65+
66+
67+
public func startLogging() {
68+
stopLogging()
69+
70+
let notificationCenter = NotificationCenter.default
71+
72+
notificationCenter.addObserver(
73+
self,
74+
selector: #selector(NetworkActivityLogger.networkRequestDidStart(notification:)),
75+
name: Notification.Name.Task.DidResume,
76+
object: nil
77+
)
78+
79+
notificationCenter.addObserver(
80+
self,
81+
selector: #selector(NetworkActivityLogger.networkRequestDidComplete(notification:)),
82+
name: Notification.Name.Task.DidComplete,
83+
object: nil
84+
)
85+
}
86+
87+
public func stopLogging() {
88+
NotificationCenter.default.removeObserver(self)
89+
}
90+
91+
92+
@objc private func networkRequestDidStart(notification: Notification) {
93+
guard let userInfo = notification.userInfo,
94+
let task = userInfo[Notification.Key.Task] as? URLSessionTask,
95+
let request = task.originalRequest,
96+
let httpMethod = request.httpMethod,
97+
let requestURL = request.url
98+
else {
99+
return
100+
}
101+
102+
if let filterPredicate = filterPredicate, filterPredicate.evaluate(with: request) {
103+
return
104+
}
105+
106+
if startDates[task] != nil {
107+
startDates[task] = Date()
108+
}
109+
110+
switch level {
111+
case .debug:
112+
logDivider()
113+
114+
print("\(httpMethod) '\(requestURL.absoluteString)':")
115+
116+
if let httpHeadersFields = request.allHTTPHeaderFields {
117+
logHeaders(headers: httpHeadersFields)
118+
}
119+
120+
if let httpBody = request.httpBody, let httpBodyString = String(data: httpBody, encoding: .utf8) {
121+
print(httpBodyString)
122+
}
123+
case .info:
124+
logDivider()
125+
126+
print("\(httpMethod) '\(requestURL.absoluteString)'")
127+
default:
128+
break
129+
}
130+
}
131+
132+
@objc private func networkRequestDidComplete(notification: Notification) {
133+
guard let sessionDelegate = notification.object as? SessionDelegate,
134+
let userInfo = notification.userInfo,
135+
let task = userInfo[Notification.Key.Task] as? URLSessionTask,
136+
let request = task.originalRequest,
137+
let httpMethod = request.httpMethod,
138+
let requestURL = request.url
139+
else {
140+
return
141+
}
142+
143+
if let filterPredicate = filterPredicate, filterPredicate.evaluate(with: request) {
144+
return
145+
}
146+
147+
var elapsedTime: TimeInterval = 0.0
148+
149+
if let startDate = startDates[task] {
150+
elapsedTime = Date().timeIntervalSince(startDate)
151+
if startDates[task] != nil {
152+
startDates[task] = nil
153+
}
154+
}
155+
156+
if let error = task.error {
157+
switch level {
158+
case .debug, .info, .warn, .error:
159+
logDivider()
160+
161+
print("[Error] \(httpMethod) '\(requestURL.absoluteString)' [\(String(format: "%.04f", elapsedTime)) s]:")
162+
print(error)
163+
default:
164+
break
165+
}
166+
} else {
167+
guard let response = task.response as? HTTPURLResponse else {
168+
return
169+
}
170+
171+
switch level {
172+
case .debug:
173+
logDivider()
174+
175+
print("\(String(response.statusCode)) '\(requestURL.absoluteString)' [\(String(format: "%.04f", elapsedTime)) s]:")
176+
177+
logHeaders(headers: response.allHeaderFields)
178+
179+
guard let data = sessionDelegate[task]?.delegate.data else { break }
180+
181+
do {
182+
let jsonObject = try JSONSerialization.jsonObject(with: data, options: .mutableContainers)
183+
let prettyData = try JSONSerialization.data(withJSONObject: jsonObject, options: .prettyPrinted)
184+
185+
if let prettyString = String(data: prettyData, encoding: .utf8) {
186+
print(prettyString)
187+
}
188+
} catch {
189+
if let string = NSString(data: data, encoding: String.Encoding.utf8.rawValue) {
190+
print(string)
191+
}
192+
}
193+
case .info:
194+
logDivider()
195+
196+
print("\(String(response.statusCode)) '\(requestURL.absoluteString)' [\(String(format: "%.04f", elapsedTime)) s]")
197+
default:
198+
break
199+
}
200+
}
201+
}
202+
}
203+
204+
private extension NetworkActivityLogger {
205+
func logDivider() {
206+
print("---------------------")
207+
}
208+
209+
func logHeaders(headers: [AnyHashable : Any]) {
210+
print("Headers: [")
211+
for (key, value) in headers {
212+
print(" \(key) : \(value)")
213+
}
214+
print("]")
215+
}
216+
}

3rd/InputBarAccessoryView.podspec

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
Pod::Spec.new do |s|
3+
4+
# 1 - Specs
5+
s.platform = :ios
6+
s.ios.deployment_target = '12.0'
7+
s.name = 'InputBarAccessoryView'
8+
s.summary = "Make powerful and flexible InputAccessoryView's with ease"
9+
s.description = "Featuring reactive changes, autocomplete, image paste support and so much more."
10+
s.requires_arc = true
11+
s.swift_versions = '5.5'
12+
13+
# 2 - Version
14+
s.version = "6.3.0"
15+
16+
# 3 - License
17+
s.license = { :type => "MIT", :file => "LICENSE" }
18+
19+
# 4 - Author
20+
s.author = { "Nathan Tannar" => "[email protected]" }
21+
22+
# 5 - Homepage
23+
s.homepage = "https://github.com/nathantannar4/InputBarAccessoryView"
24+
25+
# 6 - Source
26+
s.source = { :git => "https://github.com/nathantannar4/InputBarAccessoryView.git", :tag => "#{s.version}"}
27+
28+
# 7 - Dependencies
29+
s.framework = "UIKit"
30+
31+
# 8 - Sources
32+
s.default_subspec = 'Core'
33+
34+
s.subspec 'Core' do |ss|
35+
ss.source_files = "InputBarAccessoryView/Sources/**/*.{swift}"
36+
end
37+
end

3rd/InputBarAccessoryView/LICENSE

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
The MIT License (MIT)
3+
4+
Copyright (c) Nathan Tannar <[email protected]> 2017-2019
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.

0 commit comments

Comments
 (0)