Skip to content
zzjzz9266a edited this page Apr 24, 2017 · 20 revisions

Import

If you drag Pitaya project into your project, you may need to import it before use it:

import Pitaya

Use

Basic Use

Pita.build(HTTPMethod: .GET, url: "https://httpbin.org/get?hello=Hello%20Pitaya!")
    .responseJSON { (json, response) -> Void in
        print(json["args"]["hello"].stringValue)
}

A basic request needs only two method: build() and responseData(), and other modifications are all optional.

Response

There are alse responseData() and responseString() can give us a response.

responseData

Pita.build(HTTPMethod: .GET, url: "https://httpbin.org/get?hello=Hello%20Pitaya!")
    .responseData({ (data, response) -> Void in
        dump(data)
}

responseString

Pita.build(HTTPMethod: .GET, url: "https://httpbin.org/get?hello=Hello%20Pitaya!")
    .responseString { (string, response) -> Void in
        print(string)
}

Error callback

Pita.build(HTTPMethod: .GET, url: "https://httpbin.org/get?hello=Hello%20Pitaya!")
    .onNetworkError({ (error) -> Void in
        print("network offline!")
    })
    .responseJSON { (json, response) -> Void in
        print(json["args"]["hello"].stringValue)
}

Sync Request

Pita.build(HTTPMethod: .GET, url: "http://httpbin.org/delay/3", timeout: 10, execution: .sync)
	.responseString { (string, response) in
            self.resultLabel.text = string
        }

HTTP Basic Auth

Pita.build(HTTPMethod: .GET, url: "http://httpbin.org/basic-auth/user/passwd")
    .setBasicAuth("user", password: "passwd")
    .responseData { (data, response) -> Void in
        print(response?.statusCode) // get '200'
}

Params

Pitaya will deal with params in different ways whether under 'GET' or 'POST' method, just give your params to her.

Pita.build(HTTPMethod: .GET, url: "http://httpbin.org/get?hello=param")
    .responseJSON { (json, response) -> Void in
        print(json["args"]["hello"].stringValue) // get 'param'
}

Files

let file = File(name: "file", url: NSBundle.mainBundle().URLForResource("Pitaya", withExtension: "png")!)
Pita.build(HTTPMethod: .POST, url: "http://staticonsae.sinaapp.com/pitaya.php")
    .addParams(["param": "test"])
    .addFiles([file])
    .responseString{ (string, response) -> Void in
        print(string!) // get '1'
}

Custom request HTTP headers

let name = "Accept"
let value = "application/json"

Pita.build(HTTPMethod: .GET, url: "http://httpbin.org/headers")
    .setHTTPHeader(Name: name, Value: value)
    .responseJSON { (json, response) -> Void in
        print(json["headers"][name].stringValue) // get 'application/json'
}

SSL pinning

let certData = NSData(contentsOfFile: NSBundle.mainBundle().pathForResource("lvwenhancom", ofType: "cer")!)!

Pita.build(HTTPMethod: .GET, url: "https://lvwenhan.com/")
    .addSSLPinning(LocalCertData: self.certData, SSLValidateErrorCallBack: { () -> Void in
        print("Under the Man-in-the-middle attack!")
    })
    .responseString { (string, response) -> Void in
        dump(string)
}

HTTP Raw Body

Pita.build(HTTPMethod: .POST, url: "http://httpbin.org/post")
    .setHTTPBodyRaw("http body")
    .responseJSON { (json, response) -> Void in
        print(json["data"].stringValue) // get 'http body'
}

If you want to set a JSON string to http body in raw:

let json: JSONND = ["user": "JohnLui", "love": "you"]

Pita.build(HTTPMethod: .POST, url: "http://httpbin.org/post")
    .setHTTPBodyRaw(json.RAWValue, isJSON: true)
    .responseJSON { (json, response) -> Void in
        print(json["data"].stringValue)
        // get:
        // {
        //   "love" : "you",
        //   "user" : "JohnLui"
        // }
        print(json["json"]["user"].stringValue) // get 'JohnLui'
        print(json["json"]["love"].stringValue) // get 'you'
}

Structure

Pitaya class is an API layer of PitayaManager to make APIs more elegant. If you want to send a request with Pitaya, just do 3 steps:

build up a Pitaya object

let pitaya = Pita.build(HTTPMethod: .GET, url: "https://httpbin.org/get?hello=Hello%20Pitaya!")

change something of the Pitaya object

pitaya.addParams(["hello": "params"])
pitaya.addFiles([file])
pitaya.setHTTPHeader(Name: "Accept", Value: "application/json")
pitaya.setBasicAuth("user", password: "passwd")
pitaya.setHTTPBodyRaw(json.RAWValue)
pitaya.onNetworkError({ (error) -> Void in
    print("network offline!")
})
pitaya.addSSLPinning(LocalCertData: certData) { () -> Void in
    print("Under Man-in-the-middle attack!")
}

fire up

pitaya.responseJSON { (json, response) -> Void in
    print(json["args"]["hello"].stringValue)
}

Control

cancel the request

let pita = Pita.build(HTTPMethod: .GET, url: "http://httpbin.org/")
pita.responseString { (string, response) -> Void in
    print(string)
}
pita.cancel { () -> Void in
    print("Request Cancelled!")
}

You can also get data in NSData or String type by using some other methods.