Skip to content

Instantly share code, notes, and snippets.

@emehrkay
Last active June 6, 2016 13:01
Show Gist options
  • Save emehrkay/ba42d6fb6e1075308eb7fca96dea9041 to your computer and use it in GitHub Desktop.
Save emehrkay/ba42d6fb6e1075308eb7fca96dea9041 to your computer and use it in GitHub Desktop.
Quick Gremlin test from Swift
import Cocoa
class GremlinConnection{
var host : String = ""
var port : String = "8182"
var processor : String = ""
var op : String = "eval"
var request : NSMutableURLRequest?
var web_socket : WebSocket?
init(host : String, port : String, processor : String = "", op : String = "eval"){
self.host = host
self.port = port
self.processor = processor
self.op = op
self.request = nil
self.web_socket = nil
}
func connect(){
let uri = "\(self.host):\(self.port)"
self.request = NSMutableURLRequest(URL: NSURL(string:uri)!)
self.web_socket = WebSocket(request: self.request!)
print("connected to \(uri)")
}
func query(gremlin : String, bindings : Dictionary<String, AnyObject> = [:], onComplete: ((response : GremlinResponse) -> Void), rebindings : Dictionary<String, AnyObject> = [:]){
self.connect()
let params = [
"requestId": NSUUID().UUIDString,
"op": self.op,
"processor": self.processor,
"args": [
"gremlin": gremlin,
"bindings": bindings,
"language": "gremlin-groovy",
"rebindings": rebindings
]
]
let jsonData = try! NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions())
let jsonString = NSString(data: jsonData, encoding: NSUTF8StringEncoding) as! String
let aj = "application/json"
let aj_bytes = [UInt8](aj.utf8)
let json_bytes = [UInt8](jsonString.utf8)
let len = UInt8(aj_bytes.count)
let buf = [len] + aj_bytes + json_bytes
let send : () -> () = {
print("Sending \(params)")
self.web_socket!.send(buf)
}
self.web_socket!.event.open = {
send()
}
self.web_socket!.event.message = { message in
var data: [UInt8] = []
for (x) in (message as! Array<UInt8>){
data.append(x)
}
let z = NSString(bytes: data, length: data.count, encoding: NSUTF8StringEncoding)
print(z)
let z_data = z?.dataUsingEncoding(NSUTF8StringEncoding)
do {
let j = try NSJSONSerialization.JSONObjectWithData((z_data)!, options: []) as? [String:AnyObject]
print (j)
onComplete(response: GremlinResponse(response : j!))
}catch let error as NSError {
let resp = GremlinResponse()
resp.error = error
onComplete(response: resp)
}
}
}
}
class GremlinResponse {
var data : Dictionary<String, AnyObject>?
var status : NSNumber?
var message : String?
var attributes : Dictionary<String, AnyObject>?
var error: NSError?
init(response : Dictionary<String, AnyObject> = [:]){
if let status = response["status"]?["code"] {
self.status = status as? NSNumber
print("STATUS \(self.status)")
}
if let message = response["status"]?["message"] {
self.message = message as? String
}
if let attributes = response["status"]?["attributes"] {
self.attributes = attributes as? Dictionary<String, AnyObject>
}
if let data = response["status"]?["data"] {
self.data = data as? Dictionary<String, AnyObject>
}
}
}
@emehrkay
Copy link
Author

emehrkay commented Jun 3, 2016

This leverages https://github.com/tidwall/SwiftWebSocket

Example usage:

        let cb = { (response:GremlinResponse) in
            print(response) //do your work here
        }

        connection.query(self.input.stringValue, onComplete: cb)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment