MDRequestHandler
public class MDRequestHandler : NSObject
The class responsible for performing requests
-
The different types of cookies that can be changed by the API
See moreDeclaration
Swift
public enum CookieType : String -
An alias for the completion blocks called after requests
Parameters are the underlying response, its string content and its error (if relevant)
Declaration
Swift
public typealias RequestCompletion = (HTTPURLResponse?, String?, MDApiError?) -> Void -
User-Agent used for calls by this instance
During init, WKWebView is used to get the device’s real User-Agent. The
MDApi.defaultUserAgentstring is then appended to that User-AgentDeclaration
Swift
public private(set) var userAgent: String { get } -
The current session used for requests
Declaration
Swift
public private(set) var session: URLSession { get } -
The cookies valid for this session
Declaration
Swift
public private(set) var cookieJar: HTTPCookieStorage { get } -
The delay (in seconds) added before doing a requests which goes through the
handleDdosGuardmethodThis delay is only added for
POST,PUT, orDELETErequests, so it will be mostly invisible to the user during normal useDeclaration
Swift
public private(set) var ddosGuardDelay: Double { get } -
Boolean indicating whether the handler is ready to start performing requests
The handler is considered
unreadybefore its User-Agent has been set, because some requests (mainly those requiring login) fail if a proper User-Agent isn’t sentDeclaration
Swift
public private(set) var isReady: Bool { get } -
List of requests that haven’t been started yet
Requests are added to the queue before the handler is ready. Once ready, all the requests are automatically started
Declaration
Swift
public private(set) var requestQueue: [(NSMutableURLRequest, RequestCompletion)] { get } -
Change the User-Agent used for every API call
Declaration
Swift
public func setUserAgent(_ userAgent: String)Parameters
userAgentThe new user agent to use
-
Change the delay added before performing a
POST,PUT, orDELETErequest (in seconds)Note
The minimum value is capped at 0.05 seconds. Default is 0.1Declaration
Swift
public func setDdosGuardDelay(_ delay: Double)Parameters
delayThe delay (in seconds) added before each request
-
Change the maximum number of concurrent connections that will be made by the handler
Note
The maximum value is capped at 25, and the minimum at 1. Default is 5Declaration
Swift
public func setMaxConcurrentConnections(_ maxConnections: Int)Parameters
maxConnectionsThe maximum number of concurrent connections
-
Reset the session (clear cookies, credentials, caches…)
Note
Custom set cookies have to be reset as they will be deletedDeclaration
Swift
public func resetSession() -
Set a cookie’s value for the following requests
Declaration
Swift
public func setCookie(type: CookieType, value: String, sessionOnly: Bool = true, secure: Bool = false)Parameters
typeThe type of cookie to set
valueThe value of the cookie to set
sessionOnlyWhether the cookie should be deleted at the end of the session
secureWhether the cookie should only be sent over secure connections
-
Get the value of the cookie with the given type, if set
Declaration
Swift
public func getCookie(type: CookieType) -> String?Parameters
typeThe type of cookie to read
Return Value
The value of the cookie, if any
-
Delete the the cookie with the given type, if set
Declaration
Swift
public func deleteCookie(type: CookieType)Parameters
typeThe type of cookie to delete
-
Perform an async get request
Declaration
Swift
public func get(url: URL, completion: @escaping RequestCompletion)Parameters
urlThe URL to fetch
completionThe callback at the end of the request
-
Perform an async post request
Because of the way DDoS-Guard works, this request cannot be the first one to ever be done. It is best to always start with a
pingrequest to make sure theddosGuard1cookie is set.Precondition
The.ddosGuardcookie must be setDeclaration
Swift
public func post<T>(url: URL, content: T, completion: @escaping RequestCompletion) where T : EncodableParameters
urlThe URL to load
contentThe object to JSON-encode in the request’s body
completionThe callback at the end of the request
-
Perform an async put request
Precondition
The.ddosGuardcookie must be setDeclaration
Swift
public func put<T>(url: URL, content: T, completion: @escaping RequestCompletion) where T : EncodableParameters
urlThe URL to load
contentThe object to JSON-encode in the request’s body
completionThe callback at the end of the request
-
Perform an async put request
Precondition
The.ddosGuardcookie must be setDeclaration
Swift
public func delete(url: URL, completion: @escaping RequestCompletion)Parameters
urlThe URL to load
contentThe object to JSON-encode in the request’s body
completionThe callback at the end of the request
View on GitHub
MDRequestHandler Class Reference