cocoa - Example of Swift and a Put request for a RESTful API -



cocoa - Example of Swift and a Put request for a RESTful API -

i'm trying larn swift creating osx app phillips hue lite api. however, , sense silly here, can't simple illustration working. i'm using library in x code 6.1: https://github.com/hallas/agent

here's code i'm using:

import foundation allow done = { (response: nshttpurlresponse!, data: agent.data!, error: nserror!) -> void in // react result of request }; agent.put("/api/[username]/lights/2/state", headers: [ "header": "value" ], data: [ "hue": 35000 ], done: done)

needless not doing anything. doing wrong?

this sample of set operation, using simple class wrap http functionality:

allow url = nsurl(string:"http://example.com") allow text = "text put" var mydata: nsdata? = text.datausingencoding(nsutf8stringencoding) var headers = dictionary<string, string>() http().put(url!, headers: headers, data:mydata!) { (result) in if result.success { if allow jsonobject: anyobject = result.jsonobject { println(jsonobject) } } } class http { func put(url: nsurl, headers: dictionary<string, string>, data: nsdata, completionhandler: ((result: httpresult) -> void)!) { action("put", url: url, headers: headers, data: info ) { (result) in completionhandler(result: result) } } func action(verb: string, url: nsurl, headers: dictionary<string, string>, data: nsdata, completionhandler: ((result: httpresult) -> void)!) { allow httprequest = nsmutableurlrequest(url: url) httprequest.httpmethod = verb (headerkey, headervalue) in headers { httprequest.setvalue(headervalue, forhttpheaderfield: headerkey) } allow task = nsurlsession.sharedsession().uploadtaskwithrequest(httprequest, fromdata: data) { (data, response, error) in completionhandler(result: httpresult(data: data, request: httprequest, response: response, error: error)) } task.resume() } } class httpresult { var request: nsurlrequest var response: nshttpurlresponse? var data: nsdata? var error: nserror? var statuscode: int = 0 var success: bool = false var headers : dictionary<string, string> { { if allow responsevalue = response { homecoming responsevalue.allheaderfields dictionary<string,string> } else { homecoming dictionary<string, string>() } } } init(data: nsdata?, request: nsurlrequest, response: nsurlresponse?, error : nserror?) { self.data = info self.request = request self.response = response nshttpurlresponse? self.error = error self.success = false if error != nil { println("http.\(request.httpmethod!): \(request.url)") println("error: \(error!.localizeddescription)") } else { if allow responsevalue = self.response { statuscode = responsevalue.statuscode if statuscode >= 200 && statuscode < 300 { success = true } else { println("http.\(request.httpmethod!) \(request.url)") println("status: \(statuscode)") if allow jsonerror: anyobject = jsonobject { var err: nserror? var errdata = nsjsonserialization.datawithjsonobject(jsonerror, options:nsjsonwritingoptions.prettyprinted, error: &err) var errmessage = nsstring(data: errdata!, encoding: nsutf8stringencoding) println("error: \(errmessage)") } } } } } var jsonobject: anyobject? { var resultjsonobject: anyobject? var jsonerror: nserror? if allow contenttype = headers["content-type"] { if contenttype.contains("application/json") { resultjsonobject = nsjsonserialization.jsonobjectwithdata(data!, options: .allowfragments, error: &jsonerror) anyobject? } } homecoming resultjsonobject } }

cocoa rest swift philips-hue

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -