Skip to content

Instantly share code, notes, and snippets.

@vzool
Last active August 29, 2015 14:26
Show Gist options
  • Save vzool/841f5759f6735fe83ce5 to your computer and use it in GitHub Desktop.
Save vzool/841f5759f6735fe83ce5 to your computer and use it in GitHub Desktop.
// Playground - noun: a place where people can play
import Foundation
import UIKit
import XCPlayground
XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: true)
// ----- GET -----
let url: NSURL? = NSURL(string: "http://json-api.rocks/api/movies/1")
var request: NSMutableURLRequest = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
(data, response, error) in
let response = response as NSHTTPURLResponse
println(response.statusCode)
println(NSString(data: data, encoding: NSUTF8StringEncoding))
}
task.resume()
// ----- POST -----
let url: NSURL? = NSURL(string: "http://json-api.rocks/api/movies/8")
let data: [String: String] = [
"director": "Stanley Kubrick",
"title" : "The Shining"
]
var request: NSMutableURLRequest = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(
data, options: nil, error: nil
)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
(data, response, error) in
let response = response as NSHTTPURLResponse
println(response.statusCode)
println(NSString(data: data, encoding: NSUTF8StringEncoding))
}
task.resume()
// ----- PUT -----
let url: NSURL? = NSURL(string: "http://json-api.rocks/api/movies/1")
let payload: [String: String] = [
"director": "Rashomon",
"title" : "Akira Kurozawa"
]
var request: NSMutableURLRequest = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "PUT"
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(
payload,
options: nil,
error: nil
)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
(data, response, error) in
let response = response as NSHTTPURLResponse
println("Error: \(error)")
println("Status: \(response.statusCode)")
println(NSString(data: data, encoding: NSUTF8StringEncoding))
}
task.resume()
// ----- DELETE -----
let url: NSURL? = NSURL(string: "http://json-api.rocks/api/movies/6")
var request: NSMutableURLRequest = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "DELETE"
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
(data, response, error) in
println(NSString(data: data, encoding: NSUTF8StringEncoding))
}
task.resume()
// ------------ Simple Repository -------------
class JsonRepository {
func getUrl(url: String, completionHandler:((NSData!, NSURLResponse!, NSError!) -> Void)?) {
let url: NSURL? = NSURL(string: url)
var request: NSMutableURLRequest = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = NSURLSession.sharedSession()
.dataTaskWithRequest(request, completionHandler)
task.resume()
}
}
JsonRepository().getUrl("http://json-api.rocks/api/movies") {
(data, response, error) in
let response = response as NSHTTPURLResponse
println("Error: \(error)")
println("Status: \(response.statusCode)")
println(NSString(data: data, encoding: NSUTF8StringEncoding))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment