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.defaultUserAgent
string 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
handleDdosGuard
methodThis delay is only added for
POST
,PUT
, orDELETE
requests, 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
unready
before 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
userAgent
The new user agent to use
-
Change the delay added before performing a
POST
,PUT
, orDELETE
request (in seconds)Note
The minimum value is capped at 0.05 seconds. Default is 0.1Declaration
Swift
public func setDdosGuardDelay(_ delay: Double)
Parameters
delay
The 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
maxConnections
The 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
type
The type of cookie to set
value
The value of the cookie to set
sessionOnly
Whether the cookie should be deleted at the end of the session
secure
Whether 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
type
The 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
type
The type of cookie to delete
-
Perform an async get request
Declaration
Swift
public func get(url: URL, completion: @escaping RequestCompletion)
Parameters
url
The URL to fetch
completion
The 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
ping
request to make sure theddosGuard1
cookie is set.Precondition
The.ddosGuard
cookie must be setDeclaration
Swift
public func post<T>(url: URL, content: T, completion: @escaping RequestCompletion) where T : Encodable
Parameters
url
The URL to load
content
The object to JSON-encode in the request’s body
completion
The callback at the end of the request
-
Perform an async put request
Precondition
The.ddosGuard
cookie must be setDeclaration
Swift
public func put<T>(url: URL, content: T, completion: @escaping RequestCompletion) where T : Encodable
Parameters
url
The URL to load
content
The object to JSON-encode in the request’s body
completion
The callback at the end of the request
-
Perform an async put request
Precondition
The.ddosGuard
cookie must be setDeclaration
Swift
public func delete(url: URL, completion: @escaping RequestCompletion)
Parameters
url
The URL to load
content
The object to JSON-encode in the request’s body
completion
The callback at the end of the request