https://github.com/derekcoder/giraffe
Lightweight & Elegant RESTful Networking Library in Swift
https://github.com/derekcoder/giraffe
Last synced: 10 months ago
JSON representation
Lightweight & Elegant RESTful Networking Library in Swift
- Host: GitHub
- URL: https://github.com/derekcoder/giraffe
- Owner: derekcoder
- License: mit
- Created: 2018-03-20T04:00:31.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-11-29T08:28:33.000Z (about 6 years ago)
- Last Synced: 2024-11-14T14:04:57.861Z (about 1 year ago)
- Language: Swift
- Homepage:
- Size: 295 KB
- Stars: 4
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Giraffe
[](https://travis-ci.org/derekcoder/Giraffe)
[](http://cocoapods.org/pods/Giraffe)
[](http://cocoapods.org/pods/Giraffe)
[](http://cocoapods.org/pods/Giraffe)
## Features
- [x] HTTP Method
- [x] GET
- [x] POST
- [x] PUT
- [x] PATCH
- [x] DELETE
- [x] HTTP Headers
- [x] Cancel Request
- [x] Complete Tests
- [ ] Complete Documentation
## Requirements
- iOS 10.0+
- Swift 5+
- Xcode 10.2+
## Installation
Giraffe is available through [CocoaPods](http://cocoapods.org). To install
it, simply add the following line to your Podfile:
```ruby
pod 'Giraffe'
```
## Usage
```swift
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
```swift
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
```swift
extension Post {
static func createResource(title: String, body: String) -> Resource {
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
```swift
extension Post {
var updateResource: Resource {
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
```swift
extension Post {
var udpateTitleResource: Resource {
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
```swift
extension Post {
var deleteResource: Resource {
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
}
}
```
## Apps Using Giraffe
|
|
| :---: |
| [Coderx for GitHub](https://itunes.apple.com/app/apple-store/id1371929193?mt=8) |
## References
- [Swift Talk](https://talk.objc.io) - [Tiny Networking Library](https://talk.objc.io/episodes/S01E1-tiny-networking-library)
- [@onevcat](https://github.com/onevcat) - [Result 还是 Result](https://onevcat.com/2018/10/swift-result-error/)
## License
Giraffe is available under the MIT license. See the LICENSE file for more info.