mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2024-12-23 14:52:24 +00:00
98 lines
2.8 KiB
Swift
98 lines
2.8 KiB
Swift
|
//
|
||
|
// ServiceCom.swift
|
||
|
// ZeroTier One
|
||
|
//
|
||
|
// Created by Grant Limberg on 5/16/16.
|
||
|
// Copyright © 2016 ZeroTier, Inc. All rights reserved.
|
||
|
//
|
||
|
|
||
|
import Cocoa
|
||
|
|
||
|
class ServiceCom: NSObject {
|
||
|
static let baseURL = "http://localhost:9993"
|
||
|
static var key: NSString? = "ddeb3b1e6996b6b4f2d12d10"
|
||
|
|
||
|
static func getNetworkList() {
|
||
|
|
||
|
let urlString = baseURL + "/network?auth=\(ServiceCom.key!)"
|
||
|
|
||
|
let url = NSURL(string: urlString)
|
||
|
|
||
|
if let u = url {
|
||
|
let request = NSMutableURLRequest(URL: u)
|
||
|
let session = NSURLSession.sharedSession()
|
||
|
let task = session.dataTaskWithRequest(request) { (data, response, error) in
|
||
|
let httpResponse = response as! NSHTTPURLResponse
|
||
|
let status = httpResponse.statusCode
|
||
|
|
||
|
if status == 200 {
|
||
|
do {
|
||
|
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions())
|
||
|
print("\(json)")
|
||
|
}
|
||
|
catch {
|
||
|
print("JSON Error: \(error)")
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
task.resume()
|
||
|
}
|
||
|
else {
|
||
|
print("bad URL")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
static func joinNetwork(network: String) {
|
||
|
let urlString = baseURL + "/network/\(network)?auth=\(ServiceCom.key!)"
|
||
|
let url = NSURL(string: urlString)
|
||
|
|
||
|
if let u = url {
|
||
|
let request = NSMutableURLRequest(URL: u)
|
||
|
request.HTTPMethod = "POST"
|
||
|
|
||
|
let session = NSURLSession.sharedSession()
|
||
|
let task = session.dataTaskWithRequest(request) { (data, response, error) in
|
||
|
let httpResponse = response as! NSHTTPURLResponse
|
||
|
let status = httpResponse.statusCode
|
||
|
|
||
|
if status == 200 {
|
||
|
print("join ok")
|
||
|
}
|
||
|
else {
|
||
|
print("join error: \(status)")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
task.resume()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static func leaveNetwork(network: String) {
|
||
|
let urlString = baseURL + "/network/\(network)?auth=\(ServiceCom.key!)"
|
||
|
let url = NSURL(string: urlString)
|
||
|
|
||
|
if let u = url {
|
||
|
let request = NSMutableURLRequest(URL: u)
|
||
|
request.HTTPMethod = "DELETE"
|
||
|
|
||
|
let session = NSURLSession.sharedSession()
|
||
|
let task = session.dataTaskWithRequest(request) { (data, response, error) in
|
||
|
let httpResponse = response as! NSHTTPURLResponse
|
||
|
let status = httpResponse.statusCode
|
||
|
|
||
|
if status == 200 {
|
||
|
print("leave ok")
|
||
|
}
|
||
|
else {
|
||
|
print("leave error: \(status)")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
task.resume()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|