Skip to content

Commit cfb4ec7

Browse files
dtvdorakaro
authored andcommitted
Add guidelines for ObjectMapper/Argo/Himotoki
1 parent a59a27d commit cfb4ec7

File tree

4 files changed

+159
-3
lines changed

4 files changed

+159
-3
lines changed

ArgoUsage.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,49 @@
11
## How to use APIKit + Argo
2+
Example using APIKitExt to combine APIKit and Argo.
3+
Note that a complete example is available at [Argo Sample folder](https://github.com/DTVD/APIKitExt/tree/master/Example/Argo%20Sample).
4+
5+
### Model (Argo)
6+
Our model file will follow normal Argo's guide.
7+
```swift
8+
struct Repo {
9+
var id: Int
10+
var fullName: String
11+
var stargazersCount: Int
12+
}
13+
14+
extension Repo: Decodable {
15+
static func decode(_ j: JSON) -> Decoded<Repo> {
16+
return curry(Repo.init)
17+
<^> j <| "id"
18+
<*> j <| "full_name"
19+
<*> j <| "stargazers_count"
20+
}
21+
}
22+
```
23+
24+
### Defining request type (APIKit + APIKitExt)
25+
With APIKit used along with APIKitExt, we can parse Json result and immediately return Response type :tada:
26+
```swift
27+
struct RepoRequest: Request {
28+
typealias Response = [Repo]
29+
// ...
30+
}
31+
32+
extension RepoRequest {
33+
func response(from object: Any, urlResponse: HTTPURLResponse) -> [Repo] {
34+
guard
35+
let tree = object as? [String: Any],
36+
let items = tree["items"],
37+
let repos = try? Repo.mapArray(items) // <- notice mapArray method here !
38+
else {
39+
return []
40+
}
41+
return repos
42+
}
43+
}
44+
```
45+
46+
### Supported method
47+
Here is list of supported methods
48+
* `mapObject` : map a json object to a model object.
49+
* `mapArray` : map a json object to a list of model objects.

HimotokiUsage.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,53 @@
11
## How to use APIKit + Himotoki
2+
Example using APIKitExt to combine APIKit and Himotoki.
3+
Note that a complete example is available at [Himotoki Sample folder](https://github.com/DTVD/APIKitExt/tree/master/Example/Himotoki%20Sample).
4+
5+
### Model (Himotoki)
6+
Our model file will follow normal Himotoki's guide.
7+
```swift
8+
struct Repo {
9+
var id: Int
10+
var fullName: String
11+
var avatar: String
12+
var stargazersCount: Int
13+
}
14+
15+
extension Repo: Decodable {
16+
static func decode(_ e: Extractor) throws -> Repo {
17+
return try Repo(
18+
id: e <| "id",
19+
fullName: e <| "full_name",
20+
avatar: e <| ["owner", "avatar_url"],
21+
stargazersCount: e <| "stargazers_count"
22+
)
23+
}
24+
}
25+
26+
```
27+
28+
### Defining request type (APIKit + APIKitExt)
29+
With APIKit used along with APIKitExt, we can parse Json result and immediately return Response type :tada:
30+
```swift
31+
struct RepoRequest: Request {
32+
typealias Response = [Repo]
33+
// ...
34+
}
35+
36+
extension RepoRequest {
37+
func response(from object: Any, urlResponse: HTTPURLResponse) -> [Repo] {
38+
guard
39+
let tree = object as? [String: Any],
40+
let items = tree["items"],
41+
let repos = try? Repo.mapArray(items) // <- notice mapArray method here !
42+
else {
43+
return []
44+
}
45+
return repos
46+
}
47+
}
48+
```
49+
50+
### Supported method
51+
Here is list of supported methods
52+
* `mapObject` : map a json object to a model object.
53+
* `mapArray` : map a json object to a list of model objects.

ObjectMapperUsage.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,48 @@
11
## How to use APIKit + ObjectMapper
2+
Example using APIKitExt to combine APIKit and ObjectMapper.
3+
Note that a complete example is available at [Object Mapper Sample folder](https://github.com/DTVD/APIKitExt/tree/master/Example/ObjectMapper%20Sample).
4+
5+
### Model (ObjectMapper)
6+
Our model file will follow normal ObjectMapper's guide.
7+
```swift
8+
struct Repo: Mappable {
9+
var id: Int!
10+
var fullName: String!
11+
var stargazersCount: Int!
12+
13+
init?(map: Map) {}
14+
15+
mutating func mapping(map: Map) {
16+
id <- map["id"]
17+
fullName <- map["full_name"]
18+
stargazersCount <- map["stargazers_count"]
19+
}
20+
}
21+
```
22+
23+
### Defining request type (APIKit + APIKitExt)
24+
With APIKit used along with APIKitExt, we can parse Json result and immediately return Response type :tada:
25+
```swift
26+
struct RepoRequest: Request {
27+
typealias Response = [Repo]
28+
// ...
29+
}
30+
31+
extension RepoRequest {
32+
func response(from object: Any, urlResponse: HTTPURLResponse) -> [Repo] {
33+
guard
34+
let tree = object as? [String: Any],
35+
let items = tree["items"],
36+
let repos = try? Repo.mapArray(items) // <- notice mapArray method here !
37+
else {
38+
return []
39+
}
40+
return repos
41+
}
42+
}
43+
```
44+
45+
### Supported method
46+
Here is list of supported methods
47+
* `mapObject` : map a json object to a model object.
48+
* `mapArray` : map a json object to a list of model objects.

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# APIKitExt
22
[![Build Status](https://travis-ci.org/DTVD/APIKitExt.svg?branch=master)](https://travis-ci.org/DTVD/APIKitExt)
33
[![Version](https://img.shields.io/cocoapods/v/APIKitExt.svg)]()
4+
[![Language](https://img.shields.io/badge/language-swift3.0-f48041.svg?style=flat)](https://developer.apple.com/swift)
45

56
Extensions for famous type-safe networking library [APIKit](https://github.com/ishkawa/APIKit), provides easy binding with various JSON serialization libraries such as [ObjectMapper](https://github.com/Hearst-DD/ObjectMapper), [Argo](https://github.com/thoughtbot/Argo), [Himotoki](https://github.com/ikesyo/Himotoki) and also Reactive Extension for [RxSwift](https://github.com/ReactiveX/RxSwift) :tada:
67

@@ -12,11 +13,19 @@ Extensions for famous type-safe networking library [APIKit](https://github.com/i
1213
* tvOS 9.0 or later
1314

1415
## Installation
15-
APIKitExt is available through [CocoaPods](http://cocoapods.org). To install
16-
it, simply add the following line to your Podfile:
16+
APIKitExt is available through [CocoaPods](http://cocoapods.org). Depend on which Json serialization library that you want to use with APIKit, you can add to your Podfile as below:
1717

1818
```ruby
19-
pod "APIKitExt"
19+
# APIKit + ObjectMapper
20+
pod "APIKitExt/ObjectMapper"
21+
```
22+
```ruby
23+
# APIKit + Argo
24+
pod "APIKitExt/Argo"
25+
```
26+
```ruby
27+
# APIKit + Himotoki
28+
pod "APIKitExt/Himotoki"
2029
```
2130

2231
# Usage

0 commit comments

Comments
 (0)