Skip to content
Swift NetworkSDK
Swift Shell Other
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
NetworkSDK
NetworkSDKDemo
.gitignore
.swift-version
.travis.yml
LICENSE
NetworkSDK.podspec
Package.swift
README.md
logo.png

README.md

Network

MIT Licence

open source NetworkSDK based on Alamofire

Example
import NetworkSDK
import ObjectMapper

some configurations:

Network.defaultHeader = ["a": "b", "c": "d"]
Network.baseURL = "http://192.168.199.173"

use objectMapper, create a model:

struct Options: Mappable {
  var imageUri: String?
  var message: String?
  var dataPath: String?
  var userUri: String?
  var status: String?
  var dataVersion: String?
  var bizUri: String?

  init?(map: Map) {

  }

  mutating func mapping(map: Map) {
    imageUri    <- map["imageUri"]
    message     <- map["message"]
    dataPath    <- map["dataPath"]
    userUri     <- map["userUri"]
    status      <- map["status"]
    dataVersion <- map["dataVersion"]
    bizUri      <- map["bizUri"]
  }
}

then, fire:

let request = NetworkRequest<Options>("call.json")

request.send {
  if let option = $0 {
    print(option.toJSON())
  } else {
    print($1 ?? Error())
}

code above will send request like:

http://192.168.199.173/call.json
Download request
Network.baseURL = "http://ocef2grmj.bkt.clouddn.com"

// http://ocef2grmj.bkt.clouddn.com
// LLWeChat-master.zip 75.8MB
// 1083748_3.jpg 51.67kb

let documentURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileURL = documentURL.appendingPathComponent("b.png")

let downloadRequest = NetworkRequest<NetworkModel<Any>>("Group.png", destination: fileURL, true)
downloadRequest.download({

  if $1 == nil { //
    print($0) 
  }
}, {
  debugPrint($0.fractionCompleted)
})
Upload request
let multipartdata: (Data, String, String, String) = (data!, "fileData", "a.png", "image/png")
let uploadRequest = NetworkRequest<NetworkModel<Any>>("uploadResources.json", [multipartdata], ["category": "HEAD"])
uploadRequest.baseURL = "http://your.domain.com"

uploadRequest.upload({ 
  debugPrint($0 ?? "")
  debugPrint($1 ?? "")
}, {
  debugPrint("upload", $0.fractionCompleted)
})
Multi download request
let documentURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileURL = documentURL.appendingPathComponent("b.jpg")

let downloadRequest0 = NetworkRequest<NetworkModel<Any>>("Group.png", destination: fileURL, true)
let downloadRequest1 = NetworkRequest<NetworkModel<Any>>("1083748_3.jpg", destination: fileURL, true)

NetworkMultiRequest<NetworkModel<Any>>().download([downloadRequest0, downloadRequest1], handler: { object in
  print(object)
}, { progress in
  print("0:", progress.fractionCompleted)
}, { progress in
  print("1:", progress.fractionCompleted)
})
Others

debug log:

Network.debug = false
Features
  • Simple for use
  • Custom request
  • Load from URLCache when remote load failed
  • Simple for resuming a download request safer
  • Simple upload
  • Multi download
API docs

docs

Installation

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

pod "NetworkSDK", "~> 0.2.3"

dependency version:

dependency 'ObjectMapper', '~> 2.2.1'
dependency 'Alamofire', '~> 4.1.0'
Author

Dylan, dylan@china.com

License

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

You can’t perform that action at this time.