Swiftpack.co - DataDome/datadome-alamofire-package as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by DataDome.
DataDome/datadome-alamofire-package 3.4.0
This repository hosts the DataDome Alamofire Swift Package Manager releases.
⭐️ 3
🕓 18 weeks ago
iOS
.package(url: "https://github.com/DataDome/datadome-alamofire-package.git", from: "3.4.0")

DataDome Alamofire Integration

Version License Platform

Installation

Swift package manager

The DataDomeAlamofire SDK is available on Swift Package Manager. To get the SDK integrated to your project:

  1. In Xcode > File > Swift Packages > Add Package Dependency, select your target in which to integrate DataDomeAlamofire.
  2. Paste the following git url in the search bar https://github.com/DataDome/datadome-alamofire-package
  3. Select DataDomeAlamofire and press Add.

Cocoapods

Alternatively, DataDomeAlamofire is available on CocoaPods. To get the SDK integrated to your project, simply add the following line to your Podfile:

pod "DataDomeAlamofire"

Run pod install to download and integrate the framework to your project.

Getting started

  1. Run your application. It is going to crash with the following log
Fatal error: [DataDome] Missing DataDomeKey (Your client side key) in your Info.plist
  1. In your Info.plist, add a new entry with String type, use DataDomeKey as key and your actual client side key as value.
  2. In your Info.plist, add a new entry with Boolean type, use DataDomeProxyEnabled as key and NO as value. This will disable method swizzling in the framework.
  3. You can run now the app, it won't crash. You should see a log confirming the SDK is running
[DataDome] Version x.y.z

Congrats, the DataDome and DataDomeAlamofire frameworks are well integrated

Logging

If you need to see the logs produced by the framework, you can set the log level to control the detail of logs you get

import DataDome
DataDome.setLogLevel(level: .verbose)

By default, the framework is completely silent.

The following table contains different logging levels that you may consider using

Level Description
verbose Everything is logged
info Info messages, warnings and errors are shown
warning Only warning and errors messages are printed
error Only errors are printed
none Silent mode (default)

Force a captcha display

You can simulate a captcha display using the framework by providing a user agent with the value BLOCKUA

To do so:

  1. Edit your app scheme
  2. Under Run (Debug) > Arguments > Environment Variables, create a new variable
  3. Set the name to DATADOME_USER_AGENT and the value to BLOCKUA

The DataDome framework will inject the specified user agent in the requests the app will be sending. Using the BLOCKUA user agent value will hint our remote protection module installed on your servers to treat this request as if it is coming from a bot. Which will block it with a captcha response.

Since the DataDome framework retains the cookies after resolving the captcha, this test can be done only the first time you used the BLOCKUA user agent. To reproduce the test case, you can use the following code snippet to manually clear the cookies stored in your app

for cookie in HTTPCookieStorage.shared.cookies ?? [] {
	HTTPCookieStorage.shared.deleteCookie(cookie)
}

Create and use your Alamofire Session

Create your Alamofire Session Manager as shown in the example below:

import DataDomeAlamofire

let configuration = URLSessionConfiguration.default
configuration.headers = .default
configuration.httpCookieStorage = HTTPCookieStorage.shared
        
let dataDome = AlamofireInterceptor()
let interceptor = Interceptor(adapter: dataDome.sessionAdapter, 
                              retrier: dataDome.sessionRetrier)
 
let alamofireSessionManager = Session(configuration: configuration, 
                                      interceptor: interceptor)

The rest of your app won't change. Only make sure to use the created alamofireSessionManager to perform your requests.

Alternatively, you can conform to the CaptchaDelegate protocol and handle manually the navigation of the Captcha View Controller

let dataDome = AlamofireInterceptor(captchaDelegate: self)

Implement the CaptchaDelegate protocol


import DataDomeSDK

extension AlamofireViewController: CaptchaDelegate {
    func present(captchaController controller: UIViewController) {
        present(controller, animated: true) {
            print("Captcha displayed")
        }
    }
    
    func dismiss(captchaController controller: UIViewController) {
        controller.dismiss(animated: true) {
            print("Captcha dismissed")
        }
    } 
}

####Important When using Alamofire, make sure you call .validate() for each request to make sure Alamofire does call the retry function and hands the execution to the DataDome SDK in case of a 403 response with a Captcha Challenge.

self.alamofireSessionManager
        .request(endpoint)
        //validate here is mandatory
        .validate()
        .responseData { response in
}

When using Moya, make sure you explicitly implement `validationType in your service implementation

extension MyService: TargetType {
    var validationType: ValidationType {
        let type = ValidationType.successAndRedirectCodes
        return type
    }
}

The validationType attribute will make Moya call the .validate() function when handing the request to Alamofire.

GitHub

link
Stars: 3
Last commit: 18 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Dependencies

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics