Skip to content

KevinZhouRafael/Shrimp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Shrimp

Version Carthage compatible License Platform

Shrimp is an simplify HTTP networking library written in Swift.

Features

  • Chainable Request / Response Methods
  • Parameter Encoding
  • GET / POST / PUT / DELETE
  • Builtin JSON Request Serialization
  • Resume Download Datas
  • Download with Progress Notification
  • Auto Adjust Server Date

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Usage

GET

        Shrimp.request(.GET, urlString: "https://httpbin.org/get")
        .responseString ({ (string, response) in
            debugPrint("GET: \(string)")
        })

POST

        Shrimp.request(.POST, urlString: "http://www.mocky.io/v2/56c5b7a80f0000d027a204e2", parameters: ["username":"rafael",
            "password":"123456"])
        .responseJSONObject({ (json, response) in
                debugPrint(json["first_name"])
                debugPrint(json["last_name"])
                debugPrint(json["gender"])            
            }, errorHandler: { (error) in
                
        })

Get Server Time

Shrimp can auto adjust server time. Get server time like this:

extension Date{
    static func serverNow()->Date{
        Date(ShrimpConfigure.serverTimeInterval())
    }
}

Date.serverNow()

DOWNLOAD

More powerfull tools use Reed Download.

Start Download

DownloadManager.download(withURL: downloadURLString, withDestPath: destPath)

Resume Download

DownloadManager.resumeDownload(withURL: downloadURLString, withDestPath: destPath)

Register Download Notifications

NotificationCenter.default.addObserver(self, selector: SELECTOR ), name: NSNotification.Name(rawValue: Noti_DownloadManager_Start), object: nil)
NotificationCenter.default.addObserver(self, selector: SELECTOR ), name: NSNotification.Name(rawValue: Noti_DownloadManager_Progress), object: nil)
NotificationCenter.default.addObserver(self, selector: SELECTOR ), name: NSNotification.Name(rawValue: Noti_DownloadManager_Complete), object: nil)
NotificationCenter.default.addObserver(self, selector: SELECTOR ), name: NSNotification.Name(rawValue: Noti_DownloadManager_Failed), object: nil)
NotificationCenter.default.addObserver(self, selector: SELECTOR ), name: NSNotification.Name(rawValue: Noti_DownloadManager_Cancel), object: nil)

Observe Notifications

func downloadProgress(noti:Notification) {
        let url = noti.userInfo!["url"] as! String
        let progress = noti.userInfo!["progress"] as! Float
        let bytesWritten = noti.userInfo!["bytesWritten"] as! Int64
        let totalBytesWritten = noti.userInfo!["totalBytesWritten"] as! Int64
        let totalBytesExpectedToWrite = noti.userInfo!["totalBytesExpectedToWrite"] as! Int64
        
        debugPrint("url:\(url),progress:\(progress),bytesWritten:\(bytesWritten),totalBytesWritten\(totalBytesWritten),totalBytesExpectedToWrite:\(totalBytesExpectedToWrite)")
        
        //url:http://www.abc.com/xyz.zip, progress :0.6, bytesWritten :30, totalBytesWritten :6000, totalBytesExpectedToWrite :10000 
            
}


func downloadComplete(noti:Notification) {
    let url = noti.userInfo!["url"] as! String
    let destPath = noti.userInfo!["destPath"] as! String

}

Other Methods

    DownloadManager.pauseDownload(withURL url:String)
    DownloadManager.pauseDownloadAll()
    DownloadManager.isDownloading(url:String)    
    DownloadManager.isHasResumDate(url:String)

Requirements

  • iOS 8.0+
  • Xcode 11.3
  • Swift 5

Installation

Cocoapods

Shrimp is available through CocoaPods. To install it, simply add the following line to your Podfile:

# swift5+ ,ios 8.0+
pod "Shrimp"

Carthage

If you're using Carthage, you can add a dependency on Shrimp by adding it to your Cartfile:

github "KevinZhouRafael/Shrimp"

Author

Rafael Zhou

License

Shrimp is available under the MIT license. See the LICENSE file for more info.