牛羚 1.2.5

牛羚 1.2.5

Derek Coder 维护。



牛羚 1.2.5

牛羚

CI Status Version License Platform

功能

  • HTTP方法
    • GET
    • POST
    • PUT
    • PATCH
    • DELETE
  • HTTP头部
  • 取消请求
  • 完成测试
  • 完成文档

要求

  • iOS 10.0+
  • Swift 5+
  • Xcode 10.2+

安装

牛羚可以通过CocoaPods获取。要安装它,只需在您的Podfile文件中添加以下行

pod 'Giraffe'

使用

struct Post {
    let id: Int
    let title: String
    let body: String
}

extension Post {
    static let endPoint = URL(string: "https://jsonplaceholder.typicode.com/posts")!
    
    init?(json: JSONDictionary) {
        guard let id = json["id"] as? Int,
            let title = json["title"] as? String,
            let body = json["body"] as? String else { return nil }
        self.id = id
        self.title = title
        self.body = body
    }
}

let webservice = Webservice()

GET

extension Post {
    static var postsResource: Resource<[Post]> {
        return Resource<[Post]>(url: Post.endPoint, parseJSON: { obj in
            guard let json = obj as? [JSONDictionary] else {
                return Result.failure(.invalidResponse)
            }
            let posts = json.compactMap(Post.init)
            return Result.success(posts)
        })
    }
}

webservice.load(Post.postsResource) { response in 
    switch response {
    case .failure(let error): // handle error
    case .success(let posts): // posts: [Post]
    }
}

POST

extension Post {
    static func createResource(title: String, body: String) -> Resource<Post> {
        let data: JSONDictionary = ["title": title, "body": body]
        return Resource(url: Post.endPoint, jsonMethod: .post(data: data), parseJSON: { obj in
            guard let json = obj as? JSONDictionary,
                let post = Post(json: json) else {
                return Result.failure(.invalidResponse)
            }
            return Result.success(post)
        })
    }
}

let resource = Post.createResource(title: "xxx", body: "xxx")
webservice.load(resource) { response in 
    switch response {
    case .failure(let error): // handle error
    case .success(let post):  // post: Post
    }
}

PUT

extension Post {
    var updateResource: Resource<Post> {
        let url = Post.endPoint.appendingPathComponent("\(id)")
        let data: JSONDictionary = ["id": id, "title": title, "body": body]
        return Resource(url: url, jsonMethod: .put(data: data), parseJSON: { obj in
            guard let json = obj as? JSONDictionary,
                let post = Post(json: json) else {
                return Result.failure(.invalidResponse)
            }
            return Result.success(post)
        })
    }
}

let post = Post(id: xxx, title: "xxx", body: "xxx")
webservice.load(post.updateResource) { response in 
    switch response {
    case .failure(let error): // handle error
    case .success(let post):  // post: Post
    }
}

PATCH

extension Post {
    var udpateTitleResource: Resource<Post> {
        let url = Post.endPoint.appendingPathComponent("\(id)")
        let data: JSONDictionary = ["title": title]
        return Resource(url: url, jsonMethod: .patch(data: data), parseJSON: { obj in
            guard let json = obj as? JSONDictionary,
                let post = Post(json: json) else {
                return Result.failure(.invalidResponse)
            }
            return Result.success(post)
        })
    }
}

let post = Post(id: xxx, title: "xxx", body: "xxx")
webservice.load(post.udpateTitleResource) { response in 
    switch response {
    case .failure(let error): // handle error
    case .success(let post):  // post: Post
    }
}

DELETE

extension Post {
  var deleteResource: Resource<Bool> {
        let url = Post.endPoint.appendingPathComponent("\(id)")
        return Resource(url: url, method: .delete, parse: { _ in
            return Result.success(true)
        })
    }
}

let post = Post(id: xxx, title: "xxx", body: "xxx")
webservice.load(post.deleteResource) { response in 
    switch response {
    case .failure(let error): // handle error
    case .success(let flag):  // flag: Bool
    }
}

使用 Giraffe 的应用

Coderx
Coderx for GitHub

参考文献

协议

Giraffe遵循MIT许可证。更多信息请参阅LICENSE文件。