SimpleREST 0.2.1

SimpleREST 0.2.1

Alexander Gaidukov 维护。



  • alexander-gaidukov

SimpleREST

CI Status Version License Platform

概览

Simple REST 是一个具有缓存响应数据能力的 RESTful 客户端。

如何使用

简单请求

struct Object: Decodable {
// some properties here
}

struct APIError: Error, Decodable {
// some properties here
}

let resource = Resource<Object, CustomError>(baseURL: URL(string: "https://...")!
path: "resource_path",
params: ["param1": "value1", "param2": "value2"],
method: .get,
headers: ["headerField1": "value1"],
decoder: JSONDecoder())

let task = URLSession.shared.load(resource: resource) { result in
    switch result {
    case .success(let object):
        // handle object
    case .failure(let error):
        // handle error
    }
}

map 和 compactMap

使用 map 方法来改变资源的结果类型。

struct Object {
    var property: String
}

let resource = Resource<Object, CustomError>(...).map { $0.property }

resource 类型是 Resource<String, CustomError>

如果属性类型是可选的,您可以使用 compactMap

struct Object {
    var property: String?
}

let resource = Resource<Object, CustomError>(...).compactMap { $0.property }

在请求正文中发送数据

对于application/json数据

let resource = Resource<Object, CustomError>(baseURL: URL(string: "https://...")!
path: "resource_path",
params: [:],
method: .post(.json(["param1": "value1", "param2": "value2"])),
headers: ["headerField1": "value1"],
decoder: JSONDecoder())

对于multipart/form-data数据

let attachment = try! Attachment(path: <path to file>)
let attachments = ["images": [attachment]]

let resource = Resource<Object, CustomError>(baseURL: URL(string: "https://...")!
path: "resource_path",
params: [:],
method: .post(.multipart(params: ["param1": "value1", "param2": "value2"], attachments: attachments)),
headers: ["headerField1": "value1"],
decoder: JSONDecoder())

任务取消

task?.cancel()

缓存

使用缓存

let resource = Resource<Object, CustomError>(...)

let cacheableResource = resource.cacheable()

let task = URLSession.shared.load(resource: cacheableResource) { result in
    switch result {
    case .success(let object):
        // handle object
    case .failure(let error):
        // handle error
    }
}

如果不想使用默认的缓存键(路径 + 所有参数)和缓存存活时间(默认为永久),可以指定缓存键

let cacheableResource = resource.cacheable(key: "custom_key", liveTime: 60)

如果需要清除缓存存储,请使用以下命令

HTTPCache.shared.clear()

您还可以清除特定资源的缓存

HTTPCache.shared.clearCache(for:)

合并请求

有时您需要连续或并行执行多个请求。使用flatMapzipWith方法来执行这些操作。

let resource = Resource<Object, CustomError>(...)
let combinedResource = resource.flatMap { object in
    return Resource<OtherObject, CustomError>(...)
}
URLSession.shared.load(combinedResource: combinedResource) { result in
    switch result {
    case .success(let otherObject):
        // handle object
    case .failure(let error):
        // handle error
    }
}
let resource = Resource<Object, CustomError>(...)
let otherResource = Resource<OtherObject, CustomError>(...)
let combinedResource = resource.zipWith(otherResource) { object, otherObject in
    return <object that created from object and otherObject>
}
URLSession.shared.load(combinedResource: combinedResource) { result in
    switch result {
    case .success(let otherObject):
        // handle object
    case .failure(let error):
        // handle error
    }
}

在连续请求的情况下,有时只有当第一个请求返回特定参数时,您才需要发起第二个请求。如果不想发起第二个请求,则需要返回.value(A)

let resource = Resource<Object, CustomError>(...)
let combinedResource = resource.flatMap { object in
    if object.needAdditionalData {
        return Resource<OtherObject, CustomError>(...).map { 
            object.additionalData = $0
            return object
        }.combined
    } else {
        return .value(object)
    }
}

示例

要运行示例项目,请先克隆仓库,然后从Example目录运行pod install

需求

  • ARC
  • iOS 9.0+

安装

SimpleREST可通过CocoaPods获取。要安装它,只需将以下行添加到您的Podfile中

pod 'SimpleREST'

作者

alexander-gaidukov, [email protected]

授权

SimpleREST遵循MIT授权许可。更多信息请参阅LICENSE文件。