Skip to content

Commit

Permalink
Add analytics block
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlocke committed Oct 1, 2024
1 parent c83bbc7 commit 9dd46f0
Show file tree
Hide file tree
Showing 13 changed files with 466 additions and 216 deletions.
30 changes: 30 additions & 0 deletions Source/SwiftyDropbox/Shared/Handwritten/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,23 @@ public class Request<RSerial: JSONSerializer, ESerial: JSONSerializer> {
}
}

public typealias AnalyticsBlock = (_ success: Bool, _ resultData: Data?, _ response: HTTPURLResponse?, _ error: Error?) -> Void

/// An "rpc-style" request
public class RpcRequest<RSerial: JSONSerializer, ESerial: JSONSerializer>: Request<RSerial, ESerial> {
@discardableResult
public func response(
queue: DispatchQueue? = nil,
completionHandler: @escaping (RSerial.ValueType?, CallError<ESerial.ValueType>?) -> Void
) -> Self {
response(queue: queue, analyticsBlock: nil, completionHandler: completionHandler)
}

@discardableResult
public func response(
queue: DispatchQueue? = nil,
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (RSerial.ValueType?, CallError<ESerial.ValueType>?) -> Void
) -> Self {
request.setCompletionHandlerProvider(queue: queue, completionHandlerProvider: .dataCompletionHandlerProvider({ [weak self] response in
guard let strongSelf = self else {
Expand All @@ -114,11 +125,30 @@ public class RpcRequest<RSerial: JSONSerializer, ESerial: JSONSerializer>: Reque
}

return {
strongSelf.callAnalyticsBlockIfNeeded(response: response, analyticsBlock)
completionHandler(result, error)
}
}))
return self
}

private func callAnalyticsBlockIfNeeded(response: NetworkDataTaskResult, _ analyticsBlock: AnalyticsBlock?) {
guard let analyticsBlock = analyticsBlock else {
return
}

switch response {
case .success((let data, let response)):
analyticsBlock(true, data, response, nil)
case .failure(let failure):
switch failure {
case .badStatusCode(let data, _, let response):
analyticsBlock(false, data, response, nil)
case .failedWithError(let error):
analyticsBlock(false, nil, nil, error)
}
}
}
}

/// An "upload-style" request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class DBXAccountSetProfilePhotoRpcRequest: NSObject, DBXRequest {

@objc
@discardableResult public func response(
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXAccountSetProfilePhotoResult?, DBXAccountSetProfilePhotoError?, DBXCallError?) -> Void
) -> Self {
response(queue: nil, completionHandler: completionHandler)
Expand All @@ -52,9 +53,10 @@ public class DBXAccountSetProfilePhotoRpcRequest: NSObject, DBXRequest {
@objc
@discardableResult public func response(
queue: DispatchQueue?,
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXAccountSetProfilePhotoResult?, DBXAccountSetProfilePhotoError?, DBXCallError?) -> Void
) -> Self {
swift.response(queue: queue) { result, error in
swift.response(queue: queue, analyticsBlock: analyticsBlock) { result, error in
var routeError: DBXAccountSetProfilePhotoError?
var callError: DBXCallError?
switch error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class DBXAuthTokenRevokeRpcRequest: NSObject, DBXRequest {

@objc
@discardableResult public func response(
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXCallError?) -> Void
) -> Self {
response(queue: nil, completionHandler: completionHandler)
Expand All @@ -50,9 +51,10 @@ public class DBXAuthTokenRevokeRpcRequest: NSObject, DBXRequest {
@objc
@discardableResult public func response(
queue: DispatchQueue?,
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXCallError?) -> Void
) -> Self {
swift.response(queue: queue) { _, error in
swift.response(queue: queue, analyticsBlock: analyticsBlock) { _, error in
completionHandler(error?.objc)
}
return self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class DBXCheckUserRpcRequest: NSObject, DBXRequest {

@objc
@discardableResult public func response(
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXCheckEchoResult?, DBXCallError?) -> Void
) -> Self {
response(queue: nil, completionHandler: completionHandler)
Expand All @@ -70,9 +71,10 @@ public class DBXCheckUserRpcRequest: NSObject, DBXRequest {
@objc
@discardableResult public func response(
queue: DispatchQueue?,
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXCheckEchoResult?, DBXCallError?) -> Void
) -> Self {
swift.response(queue: queue) { result, error in
swift.response(queue: queue, analyticsBlock: analyticsBlock) { result, error in
var objc: DBXCheckEchoResult?
if let swift = result {
objc = DBXCheckEchoResult(swift: swift)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class DBXContactsDeleteManualContactsRpcRequest: NSObject, DBXRequest {

@objc
@discardableResult public func response(
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXCallError?) -> Void
) -> Self {
response(queue: nil, completionHandler: completionHandler)
Expand All @@ -66,9 +67,10 @@ public class DBXContactsDeleteManualContactsRpcRequest: NSObject, DBXRequest {
@objc
@discardableResult public func response(
queue: DispatchQueue?,
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXCallError?) -> Void
) -> Self {
swift.response(queue: queue) { _, error in
swift.response(queue: queue, analyticsBlock: analyticsBlock) { _, error in
completionHandler(error?.objc)
}
return self
Expand Down Expand Up @@ -110,6 +112,7 @@ public class DBXContactsDeleteManualContactsBatchRpcRequest: NSObject, DBXReques

@objc
@discardableResult public func response(
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXContactsDeleteManualContactsError?, DBXCallError?) -> Void
) -> Self {
response(queue: nil, completionHandler: completionHandler)
Expand All @@ -118,9 +121,10 @@ public class DBXContactsDeleteManualContactsBatchRpcRequest: NSObject, DBXReques
@objc
@discardableResult public func response(
queue: DispatchQueue?,
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXContactsDeleteManualContactsError?, DBXCallError?) -> Void
) -> Self {
swift.response(queue: queue) { _, error in
swift.response(queue: queue, analyticsBlock: analyticsBlock) { _, error in
var routeError: DBXContactsDeleteManualContactsError?
var callError: DBXCallError?
switch error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ public class DBXFilePropertiesPropertiesAddRpcRequest: NSObject, DBXRequest {

@objc
@discardableResult public func response(
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXFilePropertiesAddPropertiesError?, DBXCallError?) -> Void
) -> Self {
response(queue: nil, completionHandler: completionHandler)
Expand All @@ -356,9 +357,10 @@ public class DBXFilePropertiesPropertiesAddRpcRequest: NSObject, DBXRequest {
@objc
@discardableResult public func response(
queue: DispatchQueue?,
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXFilePropertiesAddPropertiesError?, DBXCallError?) -> Void
) -> Self {
swift.response(queue: queue) { _, error in
swift.response(queue: queue, analyticsBlock: analyticsBlock) { _, error in
var routeError: DBXFilePropertiesAddPropertiesError?
var callError: DBXCallError?
switch error {
Expand Down Expand Up @@ -411,6 +413,7 @@ public class DBXFilePropertiesPropertiesOverwriteRpcRequest: NSObject, DBXReques

@objc
@discardableResult public func response(
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXFilePropertiesInvalidPropertyGroupError?, DBXCallError?) -> Void
) -> Self {
response(queue: nil, completionHandler: completionHandler)
Expand All @@ -419,9 +422,10 @@ public class DBXFilePropertiesPropertiesOverwriteRpcRequest: NSObject, DBXReques
@objc
@discardableResult public func response(
queue: DispatchQueue?,
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXFilePropertiesInvalidPropertyGroupError?, DBXCallError?) -> Void
) -> Self {
swift.response(queue: queue) { _, error in
swift.response(queue: queue, analyticsBlock: analyticsBlock) { _, error in
var routeError: DBXFilePropertiesInvalidPropertyGroupError?
var callError: DBXCallError?
switch error {
Expand Down Expand Up @@ -474,6 +478,7 @@ public class DBXFilePropertiesPropertiesRemoveRpcRequest: NSObject, DBXRequest {

@objc
@discardableResult public func response(
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXFilePropertiesRemovePropertiesError?, DBXCallError?) -> Void
) -> Self {
response(queue: nil, completionHandler: completionHandler)
Expand All @@ -482,9 +487,10 @@ public class DBXFilePropertiesPropertiesRemoveRpcRequest: NSObject, DBXRequest {
@objc
@discardableResult public func response(
queue: DispatchQueue?,
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXFilePropertiesRemovePropertiesError?, DBXCallError?) -> Void
) -> Self {
swift.response(queue: queue) { _, error in
swift.response(queue: queue, analyticsBlock: analyticsBlock) { _, error in
var routeError: DBXFilePropertiesRemovePropertiesError?
var callError: DBXCallError?
switch error {
Expand Down Expand Up @@ -537,6 +543,7 @@ public class DBXFilePropertiesPropertiesSearchRpcRequest: NSObject, DBXRequest {

@objc
@discardableResult public func response(
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXFilePropertiesPropertiesSearchResult?, DBXFilePropertiesPropertiesSearchError?, DBXCallError?) -> Void
) -> Self {
response(queue: nil, completionHandler: completionHandler)
Expand All @@ -545,9 +552,10 @@ public class DBXFilePropertiesPropertiesSearchRpcRequest: NSObject, DBXRequest {
@objc
@discardableResult public func response(
queue: DispatchQueue?,
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXFilePropertiesPropertiesSearchResult?, DBXFilePropertiesPropertiesSearchError?, DBXCallError?) -> Void
) -> Self {
swift.response(queue: queue) { result, error in
swift.response(queue: queue, analyticsBlock: analyticsBlock) { result, error in
var routeError: DBXFilePropertiesPropertiesSearchError?
var callError: DBXCallError?
switch error {
Expand Down Expand Up @@ -604,6 +612,7 @@ public class DBXFilePropertiesPropertiesSearchContinueRpcRequest: NSObject, DBXR

@objc
@discardableResult public func response(
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXFilePropertiesPropertiesSearchResult?, DBXFilePropertiesPropertiesSearchContinueError?, DBXCallError?) -> Void
) -> Self {
response(queue: nil, completionHandler: completionHandler)
Expand All @@ -612,9 +621,10 @@ public class DBXFilePropertiesPropertiesSearchContinueRpcRequest: NSObject, DBXR
@objc
@discardableResult public func response(
queue: DispatchQueue?,
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXFilePropertiesPropertiesSearchResult?, DBXFilePropertiesPropertiesSearchContinueError?, DBXCallError?) -> Void
) -> Self {
swift.response(queue: queue) { result, error in
swift.response(queue: queue, analyticsBlock: analyticsBlock) { result, error in
var routeError: DBXFilePropertiesPropertiesSearchContinueError?
var callError: DBXCallError?
switch error {
Expand Down Expand Up @@ -671,6 +681,7 @@ public class DBXFilePropertiesPropertiesUpdateRpcRequest: NSObject, DBXRequest {

@objc
@discardableResult public func response(
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXFilePropertiesUpdatePropertiesError?, DBXCallError?) -> Void
) -> Self {
response(queue: nil, completionHandler: completionHandler)
Expand All @@ -679,9 +690,10 @@ public class DBXFilePropertiesPropertiesUpdateRpcRequest: NSObject, DBXRequest {
@objc
@discardableResult public func response(
queue: DispatchQueue?,
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXFilePropertiesUpdatePropertiesError?, DBXCallError?) -> Void
) -> Self {
swift.response(queue: queue) { _, error in
swift.response(queue: queue, analyticsBlock: analyticsBlock) { _, error in
var routeError: DBXFilePropertiesUpdatePropertiesError?
var callError: DBXCallError?
switch error {
Expand Down Expand Up @@ -801,6 +813,7 @@ public class DBXFilePropertiesTemplatesAddForUserRpcRequest: NSObject, DBXReques

@objc
@discardableResult public func response(
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXFilePropertiesAddTemplateResult?, DBXFilePropertiesModifyTemplateError?, DBXCallError?) -> Void
) -> Self {
response(queue: nil, completionHandler: completionHandler)
Expand All @@ -809,9 +822,10 @@ public class DBXFilePropertiesTemplatesAddForUserRpcRequest: NSObject, DBXReques
@objc
@discardableResult public func response(
queue: DispatchQueue?,
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXFilePropertiesAddTemplateResult?, DBXFilePropertiesModifyTemplateError?, DBXCallError?) -> Void
) -> Self {
swift.response(queue: queue) { result, error in
swift.response(queue: queue, analyticsBlock: analyticsBlock) { result, error in
var routeError: DBXFilePropertiesModifyTemplateError?
var callError: DBXCallError?
switch error {
Expand Down Expand Up @@ -935,6 +949,7 @@ public class DBXFilePropertiesTemplatesGetForUserRpcRequest: NSObject, DBXReques

@objc
@discardableResult public func response(
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXFilePropertiesGetTemplateResult?, DBXFilePropertiesTemplateError?, DBXCallError?) -> Void
) -> Self {
response(queue: nil, completionHandler: completionHandler)
Expand All @@ -943,9 +958,10 @@ public class DBXFilePropertiesTemplatesGetForUserRpcRequest: NSObject, DBXReques
@objc
@discardableResult public func response(
queue: DispatchQueue?,
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXFilePropertiesGetTemplateResult?, DBXFilePropertiesTemplateError?, DBXCallError?) -> Void
) -> Self {
swift.response(queue: queue) { result, error in
swift.response(queue: queue, analyticsBlock: analyticsBlock) { result, error in
var routeError: DBXFilePropertiesTemplateError?
var callError: DBXCallError?
switch error {
Expand Down Expand Up @@ -1069,6 +1085,7 @@ public class DBXFilePropertiesTemplatesListForUserRpcRequest: NSObject, DBXReque

@objc
@discardableResult public func response(
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXFilePropertiesListTemplateResult?, DBXFilePropertiesTemplateError?, DBXCallError?) -> Void
) -> Self {
response(queue: nil, completionHandler: completionHandler)
Expand All @@ -1077,9 +1094,10 @@ public class DBXFilePropertiesTemplatesListForUserRpcRequest: NSObject, DBXReque
@objc
@discardableResult public func response(
queue: DispatchQueue?,
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXFilePropertiesListTemplateResult?, DBXFilePropertiesTemplateError?, DBXCallError?) -> Void
) -> Self {
swift.response(queue: queue) { result, error in
swift.response(queue: queue, analyticsBlock: analyticsBlock) { result, error in
var routeError: DBXFilePropertiesTemplateError?
var callError: DBXCallError?
switch error {
Expand Down Expand Up @@ -1199,6 +1217,7 @@ public class DBXFilePropertiesTemplatesRemoveForUserRpcRequest: NSObject, DBXReq

@objc
@discardableResult public func response(
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXFilePropertiesTemplateError?, DBXCallError?) -> Void
) -> Self {
response(queue: nil, completionHandler: completionHandler)
Expand All @@ -1207,9 +1226,10 @@ public class DBXFilePropertiesTemplatesRemoveForUserRpcRequest: NSObject, DBXReq
@objc
@discardableResult public func response(
queue: DispatchQueue?,
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXFilePropertiesTemplateError?, DBXCallError?) -> Void
) -> Self {
swift.response(queue: queue) { _, error in
swift.response(queue: queue, analyticsBlock: analyticsBlock) { _, error in
var routeError: DBXFilePropertiesTemplateError?
var callError: DBXCallError?
switch error {
Expand Down Expand Up @@ -1329,6 +1349,7 @@ public class DBXFilePropertiesTemplatesUpdateForUserRpcRequest: NSObject, DBXReq

@objc
@discardableResult public func response(
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXFilePropertiesUpdateTemplateResult?, DBXFilePropertiesModifyTemplateError?, DBXCallError?) -> Void
) -> Self {
response(queue: nil, completionHandler: completionHandler)
Expand All @@ -1337,9 +1358,10 @@ public class DBXFilePropertiesTemplatesUpdateForUserRpcRequest: NSObject, DBXReq
@objc
@discardableResult public func response(
queue: DispatchQueue?,
analyticsBlock: AnalyticsBlock? = nil,
completionHandler: @escaping (DBXFilePropertiesUpdateTemplateResult?, DBXFilePropertiesModifyTemplateError?, DBXCallError?) -> Void
) -> Self {
swift.response(queue: queue) { result, error in
swift.response(queue: queue, analyticsBlock: analyticsBlock) { result, error in
var routeError: DBXFilePropertiesModifyTemplateError?
var callError: DBXCallError?
switch error {
Expand Down
Loading

0 comments on commit 9dd46f0

Please sign in to comment.