{"id":20678761,"url":"https://github.com/derekcoder/giraffe","last_synced_at":"2025-04-19T22:59:18.505Z","repository":{"id":56912749,"uuid":"125958625","full_name":"derekcoder/Giraffe","owner":"derekcoder","description":"Lightweight \u0026 Elegant RESTful Networking Library in Swift","archived":false,"fork":false,"pushed_at":"2019-11-29T08:28:33.000Z","size":302,"stargazers_count":4,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-11-14T14:04:57.861Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/derekcoder.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-03-20T04:00:31.000Z","updated_at":"2019-11-29T08:27:38.000Z","dependencies_parsed_at":"2022-08-20T20:10:07.024Z","dependency_job_id":null,"html_url":"https://github.com/derekcoder/Giraffe","commit_stats":null,"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derekcoder%2FGiraffe","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derekcoder%2FGiraffe/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derekcoder%2FGiraffe/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derekcoder%2FGiraffe/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/derekcoder","download_url":"https://codeload.github.com/derekcoder/Giraffe/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224970854,"owners_count":17400472,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-16T21:22:07.448Z","updated_at":"2024-11-16T21:22:08.153Z","avatar_url":"https://github.com/derekcoder.png","language":"Swift","readme":"# Giraffe\n\n[![CI Status](http://img.shields.io/travis/derekcoder/Giraffe.svg?style=flat)](https://travis-ci.org/derekcoder/Giraffe)\n[![Version](https://img.shields.io/cocoapods/v/Giraffe.svg?style=flat)](http://cocoapods.org/pods/Giraffe)\n[![License](https://img.shields.io/cocoapods/l/Giraffe.svg?style=flat)](http://cocoapods.org/pods/Giraffe)\n[![Platform](https://img.shields.io/cocoapods/p/Giraffe.svg?style=flat)](http://cocoapods.org/pods/Giraffe)\n\n## Features\n\n- [x] HTTP Method\n    - [x] GET\n    - [x] POST\n    - [x] PUT\n    - [x] PATCH\n    - [x] DELETE\n- [x] HTTP Headers\n- [x] Cancel Request\n- [x] Complete Tests\n- [ ] Complete Documentation\n\n## Requirements\n\n- iOS 10.0+\n- Swift 5+\n- Xcode 10.2+\n\n## Installation\n\nGiraffe is available through [CocoaPods](http://cocoapods.org). To install\nit, simply add the following line to your Podfile:\n\n```ruby\npod 'Giraffe'\n```\n\n## Usage\n\n```swift\nstruct Post {\n    let id: Int\n    let title: String\n    let body: String\n}\n\nextension Post {\n    static let endPoint = URL(string: \"https://jsonplaceholder.typicode.com/posts\")!\n    \n    init?(json: JSONDictionary) {\n        guard let id = json[\"id\"] as? Int,\n            let title = json[\"title\"] as? String,\n            let body = json[\"body\"] as? String else { return nil }\n        self.id = id\n        self.title = title\n        self.body = body\n    }\n}\n\nlet webservice = Webservice()\n```\n\n### GET\n\n```swift\nextension Post {\n    static var postsResource: Resource\u003c[Post]\u003e {\n        return Resource\u003c[Post]\u003e(url: Post.endPoint, parseJSON: { obj in\n            guard let json = obj as? [JSONDictionary] else {\n                return Result.failure(.invalidResponse)\n            }\n            let posts = json.compactMap(Post.init)\n            return Result.success(posts)\n        })\n    }\n}\n\nwebservice.load(Post.postsResource) { response in \n    switch response {\n    case .failure(let error): // handle error\n    case .success(let posts): // posts: [Post]\n    }\n}\n```\n\n### POST\n\n```swift\nextension Post {\n    static func createResource(title: String, body: String) -\u003e Resource\u003cPost\u003e {\n        let data: JSONDictionary = [\"title\": title, \"body\": body]\n        return Resource(url: Post.endPoint, jsonMethod: .post(data: data), parseJSON: { obj in\n            guard let json = obj as? JSONDictionary,\n                let post = Post(json: json) else {\n                return Result.failure(.invalidResponse)\n            }\n            return Result.success(post)\n        })\n    }\n}\n\nlet resource = Post.createResource(title: \"xxx\", body: \"xxx\")\nwebservice.load(resource) { response in \n    switch response {\n    case .failure(let error): // handle error\n    case .success(let post):  // post: Post\n    }\n}\n```\n\n### PUT\n\n```swift\nextension Post {\n    var updateResource: Resource\u003cPost\u003e {\n        let url = Post.endPoint.appendingPathComponent(\"\\(id)\")\n        let data: JSONDictionary = [\"id\": id, \"title\": title, \"body\": body]\n        return Resource(url: url, jsonMethod: .put(data: data), parseJSON: { obj in\n            guard let json = obj as? JSONDictionary,\n                let post = Post(json: json) else {\n                return Result.failure(.invalidResponse)\n            }\n            return Result.success(post)\n        })\n    }\n}\n\nlet post = Post(id: xxx, title: \"xxx\", body: \"xxx\")\nwebservice.load(post.updateResource) { response in \n    switch response {\n    case .failure(let error): // handle error\n    case .success(let post):  // post: Post\n    }\n}\n```\n\n### PATCH\n\n```swift\nextension Post {\n    var udpateTitleResource: Resource\u003cPost\u003e {\n        let url = Post.endPoint.appendingPathComponent(\"\\(id)\")\n        let data: JSONDictionary = [\"title\": title]\n        return Resource(url: url, jsonMethod: .patch(data: data), parseJSON: { obj in\n            guard let json = obj as? JSONDictionary,\n                let post = Post(json: json) else {\n                return Result.failure(.invalidResponse)\n            }\n            return Result.success(post)\n        })\n    }\n}\n\nlet post = Post(id: xxx, title: \"xxx\", body: \"xxx\")\nwebservice.load(post.udpateTitleResource) { response in \n    switch response {\n    case .failure(let error): // handle error\n    case .success(let post):  // post: Post\n    }\n}\n```\n\n### DELETE\n\n```swift\nextension Post {\n  var deleteResource: Resource\u003cBool\u003e {\n        let url = Post.endPoint.appendingPathComponent(\"\\(id)\")\n        return Resource(url: url, method: .delete, parse: { _ in\n            return Result.success(true)\n        })\n    }\n}\n\nlet post = Post(id: xxx, title: \"xxx\", body: \"xxx\")\nwebservice.load(post.deleteResource) { response in \n    switch response {\n    case .failure(let error): // handle error\n    case .success(let flag):  // flag: Bool\n    }\n}\n```\n\n## Apps Using Giraffe\n\n|\u003cimg alt=\"Coderx\" src=\"https://is2-ssl.mzstatic.com/image/thumb/Purple123/v4/0c/80/87/0c8087b1-c31f-1226-6f6f-be7230bc0c88/source/100x100bb.jpg\" width=\"48\"\u003e| \n| :---: |\n| [Coderx for GitHub](https://itunes.apple.com/app/apple-store/id1371929193?mt=8) | \n\n## References\n\n- [Swift Talk](https://talk.objc.io) - [Tiny Networking Library](https://talk.objc.io/episodes/S01E1-tiny-networking-library)\n- [@onevcat](https://github.com/onevcat) - [Result\u003cT\u003e 还是 Result\u003cT, E: Error\u003e](https://onevcat.com/2018/10/swift-result-error/)\n\n## License\n\nGiraffe is available under the MIT license. See the LICENSE file for more info.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fderekcoder%2Fgiraffe","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fderekcoder%2Fgiraffe","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fderekcoder%2Fgiraffe/lists"}