Skip to content

keygx/RapidFire

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RapidFire

RapidFire is a simple networking library. It is suitable for casual use such as prototyping and example project. It wraps URLSession and is written in Swift.

Requirements

  • Swift 5
  • iOS 8.0 or later

Features

  • Chainable Request
  • GET / POST / PUT / PATCH / DELETE
  • URL / JSON Parameter Encoding
  • multipart/form-data
  • JSON to Array / JSON to Dictionary Helper Method

Installation

Carthage

github "keygx/RapidFire"

Swift versions support

  • Swift 5, tag "swift5"
  • Swift 4.2, tag "swift4.2"
  • Swift 4.1, tag "swift4.1"
  • Swift 4.0, tag "swift4.0"

Usage

Basic Request

RapidFire(.get, "https://example.com/users")
	.setCompletionHandler({ (response: RapidFire.Response) in
		switch response.result {
		case .success:
            // success
        case .failure:
            // failure
        }
	})
	.fire()

Init

RapidFire(HTTP Method, baseURL)
// HTTP Method
public enum HTTPMethod: String {
    case get    = "GET"
    case post   = "POST"
    case put    = "PUT"
    case patch  = "PATCH"
    case delete = "DELETE"
}
// baseURL
ex: "https://example.com"

Path

.setPath("/path")

Headers

.setHeaders(["x-myheader-1":"value1", "x-myheader-2":"value2"])

Query Parameters

.setQuery(["a":"1", "b":"2"]) //?a=1&b=2

Body Parameters

.setBody(["a":"1", "b":"2"])

application/json

.setJSON(["a":"1", "b":"2"])

multipart/form-data

.setPartData(["a":"1", "b":"2"])
.setPartData(RapidFire.PartData(name: "image", filename: "sample.png", value: imageData, mimeType: "image/png"))

Timeout

.setTimeout(30) //sec.

Retry

.setRetry(3) //default interval 15 sec.

.setRetry(3, intervalSec: 30)

Response

.setCompletionHandler({ (response: RapidFire.Response) in
    switch response.result {
    case .success:
        print("success:\n \(response.statusCode as Any): \(response.response as Any)")
        print(response.toDictionary())
    case .failure:
        print("error:\n \(response.statusCode as Any): \(response.error as Any)")
    }
})
RapidFire.Response.swift
public enum Result {
    case success
    case failure
}
    
public var result:     Result
public var statusCode: Int?
public var data:       Data?
public var response:   URLResponse?
public var error:      Error?


// Convert JSON to Dictionary
public func toDictionary() -> [String: Any]

// Convert JSON to Array
public func toArray() -> [[String: Any]]

// Convert JSON to String
public func toString() -> String

Utilities

// Convert JSON to Dictionary
RapidFire.Util.toDictionary(from: response.data)

// Convert JSON to Array
RapidFire.Util.toArray(from: response.data)

// Convert JSON to String
RapidFire.Util.toString(from: response.data)

// Convert to JSON
RapidFire.Util.toJson(from: ["a":"1", "b":"2"])

License

RapidFire is released under the MIT license. See LICENSE for details.

Author

Yukihiko Kagiyama (keygx) https://twitter.com/keygx