1
1
import Foundation
2
2
3
3
// Class AccounstAPI represents the accounts API.
4
- class AccountsAPI {
4
+ public class AccountsAPI : NSObject , URLSessionDelegate {
5
5
var URI : String // API lookup path
6
6
7
7
// init initializes a new AccountsAPI instance.
@@ -10,7 +10,7 @@ class AccountsAPI {
10
10
}
11
11
12
12
// NewAccount initializes a new account via the provieded API lookup URI.
13
- func NewAccount( ) -> ( [ String : Any ] ? , Error ? ) {
13
+ public func NewAccount( ) -> ( [ String : Any ] ? , Error ? ) {
14
14
let requestContents : [ String : Any ] = [ : ] // Empty JSON request
15
15
16
16
let jsonData = try ? JSONSerialization . data ( withJSONObject: requestContents) // Get JSON representation
@@ -24,18 +24,29 @@ class AccountsAPI {
24
24
25
25
var err : Error ? = nil // Init error buffer
26
26
27
- let task = URLSession . shared. dataTask ( with: request) { data, response, error in
27
+ let sem = DispatchSemaphore ( value: 0 ) // Create sync buffer
28
+
29
+ let task = URLSession ( configuration: URLSessionConfiguration . default, delegate: self , delegateQueue: OperationQueue . init ( ) ) . dataTask ( with: request) { data, response, error in
28
30
guard let data = data, error == nil else { // Check for errors
29
31
err = error! // Set error
30
32
31
33
return // Return
32
34
}
33
35
34
36
responseJSON = try ! JSONSerialization . jsonObject ( with: data, options: [ ] ) as! [ String : Any ] // Set response JSON
37
+
38
+ sem. signal ( ) // Dispatch signal
35
39
}
36
40
37
41
task. resume ( ) // Start task
38
42
43
+ sem. wait ( ) // Wait for finish
44
+
39
45
return ( responseJSON, err) // Return response
40
46
}
47
+
48
+ // urlSession forces a self-signed TLS cert.
49
+ public func urlSession( _ session: URLSession , didReceive challenge: URLAuthenticationChallenge , completionHandler: @escaping ( URLSession . AuthChallengeDisposition , URLCredential ? ) -> Void ) {
50
+ completionHandler ( . useCredential, URLCredential ( trust: challenge. protectionSpace. serverTrust!) ) // Use credential
51
+ }
41
52
}
0 commit comments