Skip to content

Commit 39d3baf

Browse files
committed
pages: Created a quick test file.
1 parent f8e4d9d commit 39d3baf

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//: [Previous](@previous)
2+
3+
import Foundation
4+
5+
var api = API(apiURI: "https://108.41.124.60:8000") // Initialize API instance
6+
7+
var test = api.Accounts.NewAccount() // Initialize Polaris account
8+
9+
print(test) // Log account
10+
11+
//: [Next](@next)

swift-polaris-playground.playground/Sources/API.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import Foundation
22

33
// Class API defines the swift-polaris API.
4-
class API {
4+
public class API {
55
var URI: String // API lookup path
66

7-
var Accounts: AccountsAPI // Accounts API instance
7+
public var Accounts: AccountsAPI // Accounts API instance
88

99
// init initializes a new API instance with the given api URI.
10-
init(apiURI: String) {
10+
public init(apiURI: String) {
1111
URI = apiURI // Set URI
1212

1313
Accounts = AccountsAPI(apiURI: apiURI) // Initialize accounts API

swift-polaris-playground.playground/Sources/AccountsAPI.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Foundation
22

33
// Class AccounstAPI represents the accounts API.
4-
class AccountsAPI {
4+
public class AccountsAPI: NSObject, URLSessionDelegate {
55
var URI: String // API lookup path
66

77
// init initializes a new AccountsAPI instance.
@@ -10,7 +10,7 @@ class AccountsAPI {
1010
}
1111

1212
// NewAccount initializes a new account via the provieded API lookup URI.
13-
func NewAccount() -> ([String: Any]?, Error?) {
13+
public func NewAccount() -> ([String: Any]?, Error?) {
1414
let requestContents: [String: Any] = [:] // Empty JSON request
1515

1616
let jsonData = try? JSONSerialization.data(withJSONObject: requestContents) // Get JSON representation
@@ -24,18 +24,29 @@ class AccountsAPI {
2424

2525
var err: Error? = nil // Init error buffer
2626

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
2830
guard let data = data, error == nil else { // Check for errors
2931
err = error! // Set error
3032

3133
return // Return
3234
}
3335

3436
responseJSON = try! JSONSerialization.jsonObject(with: data, options: []) as! [String : Any] // Set response JSON
37+
38+
sem.signal() // Dispatch signal
3539
}
3640

3741
task.resume() // Start task
3842

43+
sem.wait() // Wait for finish
44+
3945
return (responseJSON, err) // Return response
4046
}
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+
}
4152
}

0 commit comments

Comments
 (0)