Ghost 1.4.0

Ghost 1.4.0

Meniny维护。



Ghost 1.4.0

Ghost
Version Author Build Passing Swift
Platforms MIT
Cocoapods Carthage SPM

简介

Ghost 是一个用Swift编写的多功能的HTTP(s)网络框架。

🌟特性

  • 链式请求/响应方法
  • 异步和同步任务执行
  • 基本的、Beaner和自定义授权处理
  • URL / JSON / 属性列表参数编码
  • 上传文件 / 数据 / 流 / 表单数据
  • 使用请求/恢复数据下载文件
  • 使用URLCredential进行身份验证
  • 自定义缓存控制
  • 自定义内容类型
  • 上传/下载进度闭包
  • 输出cURL命令调试
  • 请求/响应拦截器
  • 响应对象类型的推断
  • 网络可达性
  • TLS证书和公钥固定
  • 重试请求
  • 兼容Codable协议(JSON / 属性列表)
  • 兼容watchOS
  • 兼容tvOS
  • 兼容macOS

📋要求

  • iOS 8.0+
  • macOS 10.9+
  • tvOS 9.0+
  • watchOS 2.0+
  • Xcode 9.0+ with Swift 4.0+

📲安装

Ghost可在CocoaPods上使用

use_frameworks!
pod 'Ghost'

🔧用法

构建GhostRequest

import Ghost

do {
    let request = try GhostRequest.builder("YOUR_URL")!
                .setAccept(.json)
                .setCache(.reloadIgnoringLocalCacheData)
                .setMethod(.PATCH)
                .setTimeout(20)
                .setJSONBody(["foo", "bar"])
                .setContentType(.json)
                .setServiceType(.background)
                .setCacheControls([.maxAge(500)])
                .setURLParameters(["foo": "bar"])
                .setAcceptEncodings([.gzip, .deflate])
                .setBasicAuthorization(user: "user", password: "password")
                .setHeaders(["foo": "bar"])
                .build()
} catch {
    print("Request error: \(error)")
}

异步请求

import Ghost

let ghost = GhostURLSession()

ghost.data(URL(string: "YOUR_URL")!).async { (response, error) in
    do {
        if let object: [AnyHashable: Any] = try response?.object() {
            print("Response dictionary: \(object)")
        } else if let error = error {
            print("Net error: \(error)")
        }
    } catch {
        print("Parse error: \(error)")
    }
}

同步请求

import Ghost

let ghost = GhostURLSession()

do {
    let object: [AnyHashable: Any] = try ghost.data("YOUR_URL").sync().object()
    print("Response dictionary: \(object)")
} catch {
    print("Error: \(error)")
}

从缓存请求

import Ghost

let ghost = GhostURLSession()

do {
    let object: [AnyHashable: Any] = try ghost.data("YOUR_URL").cached().object()
    print("Response dictionary: \(object)")
} catch {
    print("Error: \(error)")
}

跟踪进度

import Ghost

let ghost = GhostURLSession()

do {
    let task = try ghost.data("YOUR_URL").progress({ progress in
        print(progress)
    }).sync()
} catch {
    print("Error: \(error)")
}

为所有请求添加拦截器

import Ghost

let ghost = GhostURLSession()

ghost.addRequestInterceptor { request in
    request.addHeader("foo", value: "bar")
    request.setBearerAuthorization(token: "token")
    return request
}

重试请求

import Ghost

let ghost = GhostURLSession()

ghost.retryClosure = { response, _, _ in response?.statusCode == XXX }

do {
    let task = try ghost.data("YOUR_URL").retry({ response, error, retryCount in
        return retryCount < 2
    }).sync()
} catch {
    print("Error: \(error)")
}

🧙‍♂️ Codable

Encodable

import Ghost

let request = GhostRequest.builder("YOUR_URL")!
            .setJSONObject(Encodable())
            .build()

Decodable

import Ghost

let ghost = URLSession()

do {
    let object: Decodable = try ghost.data("YOUR_URL").sync().decode()
    print("Response object: \(object)")
} catch {
    print("Error: \(error)")
}

🌙GhostHunter

let url = URL.init(string: "YOUR_URL")!
do {
    try GhostHunter.async(.GET, url: url, parameters: ["name": "elias"], headers: ["Content-Type": "text/json"], progress: { (pregress) in
        print(pregress)
    }, completion: { (response, error) in
        do {
            if let result: SomeCodableType = try response?.decode() {
                print("GhostHunter Asynchronous: \(result)")
            } else if let error = error {
                print("GhostHunter Asynchronous: Ghost error: \(error)")
            }
        } catch {
            print("GhostHunter: Parse error: \(error)")
        }
    }
} catch {
    print("GhostHunter: Request error: \(error)")
}

❤️贡献

欢迎您进行分支操作并提交拉取请求。

🔖许可协议

Ghost 是开源软件,采用 MIT 许可协议。