Giter Club home page Giter Club logo

pitaya's Introduction

#Pitaya Platform License Carthage compatible

Pitaya is a sweet HTTP networking library especially for large file uploads written in Swift. Inspired by Alamofire and JustHTTP.

Pitaya logo

##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.

pitaya's People

Contributors

johnlui avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.