Skip to content

Commit

Permalink
RUMM-1234 URLSessionDelegateProviding introduced
Browse files Browse the repository at this point in the history
  • Loading branch information
buranmert committed Apr 9, 2021
1 parent af85391 commit 5549b7e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,28 @@

import Foundation

/// In case that you are not able to use `DDURLSessionDelegate` or its subclasses in your `URLSession` instance,
/// you can use `DDURLSessionDelegateProviding` protocol.
/// Implement this protocol in your `URLSessionDelegate` class and forward required API calls to `DDURLSessionDelegate`.
@objc
public protocol URLSessionDelegateProviding: URLSessionDelegate {
/// Datadog delegate object.
/// The class implementing `DDURLSessionDelegateProviding` must ensure that following method calls are forwarded to `ddURLSessionDelegate`:
// - `func urlSession(_:task:didFinishCollecting:)`
// - `func urlSession(_:task: didCompleteWithError:)`
var ddURLSessionDelegate: DDURLSessionDelegate { get }
}

/// The `URLSession` delegate object which enables network requests instrumentation. **It must be
/// used together with** `Datadog.Configuration.trackURLSession(firstPartyHosts:)`.
///
/// All requests made with the `URLSession` instrumented with this delegate will be intercepted by the SDK.
@objc
open class DDURLSessionDelegate: NSObject, URLSessionTaskDelegate {
open class DDURLSessionDelegate: NSObject, URLSessionTaskDelegate, URLSessionDelegateProviding {
public var ddURLSessionDelegate: DDURLSessionDelegate {
return self
}

var interceptor: URLSessionInterceptorType?
let firstPartyURLsFilter: FirstPartyURLsFilter?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ internal class URLSessionSwizzler {
typealias Signature = @convention(block) (URLSession, URLRequest, CompletionHandler?) -> URLSessionDataTask
swizzle(method) { previousImplementation -> Signature in
return { session, urlRequest, completionHandler -> URLSessionDataTask in
guard let interceptor = (session.delegate as? DDURLSessionDelegate)?.interceptor else {
guard let interceptor = (session.delegate as? URLSessionDelegateProviding)?.ddURLSessionDelegate.interceptor else {
return previousImplementation(session, Self.selector, urlRequest, completionHandler)
}
let task: URLSessionDataTask
Expand Down Expand Up @@ -129,7 +129,7 @@ internal class URLSessionSwizzler {
typealias Signature = @convention(block) (URLSession, URL, CompletionHandler?) -> URLSessionDataTask
swizzle(method) { previousImplementation -> Signature in
return { session, url, completionHandler -> URLSessionDataTask in
guard let interceptor = (session.delegate as? DDURLSessionDelegate)?.interceptor else {
guard let interceptor = (session.delegate as? URLSessionDelegateProviding)?.ddURLSessionDelegate.interceptor else {
return previousImplementation(session, Self.selector, url, completionHandler)
}
let task: URLSessionDataTask
Expand Down Expand Up @@ -183,7 +183,7 @@ internal class URLSessionSwizzler {
typealias Signature = @convention(block) (URLSession, URLRequest) -> URLSessionDataTask
swizzle(method) { previousImplementation -> Signature in
return { session, urlRequest -> URLSessionDataTask in
guard let interceptor = (session.delegate as? DDURLSessionDelegate)?.interceptor else {
guard let interceptor = (session.delegate as? URLSessionDelegateProviding)?.ddURLSessionDelegate.interceptor else {
return previousImplementation(session, Self.selector, urlRequest)
}
let newRequest = interceptor.modify(request: urlRequest, session: session)
Expand Down Expand Up @@ -227,7 +227,7 @@ internal class URLSessionSwizzler {
typealias Signature = @convention(block) (URLSession, URL) -> URLSessionDataTask
swizzle(method) { previousImplementation -> Signature in
return { session, url -> URLSessionDataTask in
guard let interceptor = (session.delegate as? DDURLSessionDelegate)?.interceptor else {
guard let interceptor = (session.delegate as? URLSessionDelegateProviding)?.ddURLSessionDelegate.interceptor else {
return previousImplementation(session, Self.selector, url)
}
let task = previousImplementation(session, Self.selector, url)
Expand Down
25 changes: 24 additions & 1 deletion Sources/DatadogObjc/DDURLSessionDelegate+objc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,27 @@ import Foundation
import Datadog

@objc
public class DDNSURLSessionDelegate: DDURLSessionDelegate {}
open class DDNSURLSessionDelegate: NSObject, URLSessionTaskDelegate, URLSessionDelegateProviding {
let swiftDelegate: DDURLSessionDelegate
public var ddURLSessionDelegate: DDURLSessionDelegate {
return swiftDelegate
}

@objc
override public init() {
swiftDelegate = DDURLSessionDelegate()
}

@objc
public init(additionalFirstPartyHosts: Set<String>) {
swiftDelegate = DDURLSessionDelegate(additionalFirstPartyHosts: additionalFirstPartyHosts)
}

open func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
swiftDelegate.urlSession(session, task: task, didCompleteWithError: error)
}

open func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) {
swiftDelegate.urlSession(session, task: task, didFinishCollecting: metrics)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@

import XCTest
import Datadog
import DatadogObjc

@objc
public class SubDDNSURLSessionDelegate: DDNSURLSessionDelegate {
override public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
super.urlSession(session, task: task, didCompleteWithError: error)
}

override public func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) {
super.urlSession(session, task: task, didFinishCollecting: metrics)
}
}

internal final class SubDDURLSessionDelegate: DDURLSessionDelegate {
let property: String
Expand Down

0 comments on commit 5549b7e

Please sign in to comment.