Pitaya is a sweet HTTP networking library especially for large file uploads written in Swift. Inspired by Alamofire and JustHTTP.
##Features
- Fast file upload through "Content-Type: multipart/form-data"
- HTTP Basic Authorization supported
- Asynchronous & Blocking(blocked in thread II)
- Multi-level API to keep your code clean
- Well tested
- Fully JSON support
- Asynchronous and synchronous support
##Requirements
- iOS 8.0+
- Xcode 6.4 (Swift 1.2) in v0.2.3
- Xcode 7 (Swift 2.0) after v0.3
##Installation
###Carthage
Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate Pitaya into your Xcode project using Carthage, specify it in your Cartfile:
github "JohnLui/Pitaya"
Then fetch and build Pitaya:
carthage update
At last, add it to "Embedded Binaries" in the general panel use the "Add Other..." button. The Pitaya.framework binary file is lying in ./Carthage/Build/iOS
directory.
###Manually
git submodule add https://github.com/johnlui/Pitaya.git
open Pitaya/Pitaya
then drag Pitaya.xcodeproj to your Project, that's it!
If you want to run your project on device with Pitaya, just go to PROJECT->TARGETS->[your prokect name]->General->Embedded Binaries, click ๏ผ, select Pitaya.frameWork and click "Add".
###Source File
Drag Pitaya/Pitaya/Pitaya.swift
into your project.
##Usage
###Import Only for sub-project using.
import Pitaya
####Make a request:
Pitaya.request(.GET, url: "http://pitayaswift.sinaapp.com/pitaya.php", errorCallback: { (error) -> Void in
NSLog(error.localizedDescription)
}) { (data, response, error) -> Void in
let string = NSString(data: data!, encoding: NSUTF8StringEncoding) as! String
print("HTTP body: " + string, appendNewline: true)
print("HTTP status: " + response!.statusCode.description, appendNewline: true)
}
####with params:
Pitaya.request(.POST, url: "http://pitayaswift.sinaapp.com/pitaya.php", params: ["post": "pitaya"], errorCallback: { (error) -> Void in
NSLog(error.localizedDescription)
}) { (data, response, error) -> Void in
let string = NSString(data: data!, encoding: NSUTF8StringEncoding) as! String
print("HTTP body: " + string, appendNewline: true)
print("HTTP status: " + response!.statusCode.description, appendNewline: true)
}
####upload files:
let file = File(name: "file", url: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Pitaya", ofType: "png")!))
Pitaya.request(.POST, url: "http://pitayaswift.sinaapp.com/pitaya.php", files: [file], errorCallback: { (error) -> Void in
NSLog(error.localizedDescription)
}) { (data, response, error) -> Void in
let string = NSString(data: data!, encoding: NSUTF8StringEncoding) as! String
print("HTTP body: " + string, appendNewline: true)
print("HTTP status: " + response!.statusCode.description, appendNewline: true)
}
####POST params and files:
let file = File(name: "file", url: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Pitaya", ofType: "png")!))
Pitaya.request(.POST, url: "http://pitayaswift.sinaapp.com/pitaya.php", ["post": "pitaya", "post2": "pitaya2"], files: [file], errorCallback: { (error) -> Void in
NSLog(error.localizedDescription)
}) { (data, response, error) -> Void in
let string = NSString(data: data!, encoding: NSUTF8StringEncoding) as! String
print("HTTP body: " + string, appendNewline: true)
print("HTTP status: " + response!.statusCode.description, appendNewline: true)
}
####RAW HTTP BODY
let pitaya = PitayaManager.build(.POST, url: "http://httpbin.org/post")
pitaya.setHTTPBodyRaw("{\"fuck\":\"you\"}")
pitaya.fire({ (error) -> Void in
NSLog(error.localizedDescription)
}) { (data, response, error) -> Void in
let string = NSString(data: data!, encoding: NSUTF8StringEncoding) as! String
print("HTTP body: " + string, appendNewline: true)
print("HTTP status: " + response!.statusCode.description, appendNewline: true)
}
####HTTP Basic Authorization
let pitaya = PitayaManager.build(.GET, url: "http://httpbin.org/basic-auth/user/passwd")
pitaya.fireWithBasicAuth(("user", "passwd"), errorCallback: { (error) -> Void in
NSLog(error.localizedDescription)
}) { (data, response, error) -> Void in
let string = NSString(data: data!, encoding: NSUTF8StringEncoding) as! String
print("HTTP body: " + string, appendNewline: true)
print("HTTP status: " + response!.statusCode.description, appendNewline: true)
}
####Params and Files with HTTP Basic Authorization
let pitaya = PitayaManager.build(.GET, url: "http://httpbin.org/basic-auth/user/passwd")
// add params
pitaya.addParams(["hello": "pitaya"])
// add files
let file = File(name: "file", url: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Pitaya", ofType: "png")!))
pitaya.addFiles([file])
pitaya.fireWithBasicAuth(("user", "passwd"), errorCallback: { (error) -> Void in
NSLog(error.localizedDescription)
}) { (data, response, error) -> Void in
let string = NSString(data: data!, encoding: NSUTF8StringEncoding) as! String
print("HTTP body: " + string, appendNewline: true)
print("HTTP status: " + response!.statusCode.description, appendNewline: true)
}
###They are all Asynchronous.
##Play with JSON
You can use SwiftyJSON to parse string to JSON:
Pitaya.request(.GET, "http://pitayaswift.sinaapp.com/pitaya.php", { (error) -> Void in
NSLog(error.localizedDescription)
}) { (data, response, error) -> Void in
let json = JSON(data: data)
print(json["title"])
}
##Contribution
You are welcome to fork and submit pull requests.
##License
Pitaya is open-sourced software licensed under the MIT license.