Skip to content

Commit dc3a2ff

Browse files
author
burak.uzunboy
committed
New documentations are added
1 parent 66e9fef commit dc3a2ff

2 files changed

Lines changed: 175 additions & 0 deletions

File tree

README.md

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,161 @@
99
From Xcode simply select `File > Swift Packages > Add Package Dependency...` and paste `https://github.com/exozet/iOSUsefulNetworkLayer` to search field. You can specify rules according to your preferences and you are ready to use.
1010

1111

12+
## Usage
13+
14+
### Creating a response object
15+
16+
NetworkLayer needs an object which is inherited from `ResponseBodyParsable` class in order to create responses. There is two `required` initializer from that object, whether from the `Data` or JSON object. One of them should return `nil` to give access to other initializer.
17+
18+
In below, Response object will be created from the JSON response:
19+
20+
```swift
21+
/// Initializes object from JSON response.
22+
class ExampleResponseObject: ResponseBodyParsable {
23+
24+
var userId: Int
25+
var id: Int
26+
var title: String
27+
var completed: Bool
28+
29+
required init?(_ data: Data) {
30+
return nil
31+
}
32+
33+
required init?(_ response: Any?) {
34+
guard let dict = response as? [String:Any] else { return nil }
35+
guard let userId = dict["userId"] as? Int,
36+
let id = dict["id"] as? Int,
37+
let title = dict["title"] as? String,
38+
let completed = dict["completed"] as? Bool else { return nil }
39+
40+
self.userId = userId
41+
self.id = id
42+
self.title = title
43+
self.completed = completed
44+
super.init(response)
45+
}
46+
}
47+
```
48+
49+
50+
Or using data, for instance image, can be created by the data response:
51+
52+
```swift
53+
/// Initializes object from Data.
54+
class ExampleImageObject: ResponseBodyParsable {
55+
56+
var image: UIImage
57+
58+
required init?(_ data: Data) {
59+
guard let image = UIImage(data: data) else {
60+
return nil
61+
}
62+
63+
self.image = image
64+
super.init(data)
65+
}
66+
67+
required init?(_ response: Any?) {
68+
return nil
69+
}
70+
}
71+
```
72+
73+
### Creating an API configuration
74+
75+
Using highly customizable `APIConfiguration` object, API requests can be defined easily and requests from Network Layer.
76+
77+
In below example, uses `ExampleResponseObject` as a response model to create API configuration with basic parameters.
78+
79+
```swift
80+
let api = APIConfiguration(hostURL: "https://jsonplaceholder.typicode.com",
81+
endPoint: "todos/1",
82+
responseBodyObject: ExampleResponseObject.self)
83+
84+
```
85+
86+
Or, advanced parameters can be applied.
87+
88+
```swift
89+
let api = APIConfiguration(hostURL: "https://jsonplaceholder.typicode.com",
90+
endPoint: "todos/1",
91+
requestType: .get,
92+
headers: nil, body: nil,
93+
responseBodyObject: ExampleResponseObject.self,
94+
priority: .low,
95+
cachingTime: .init(seconds: 60),
96+
isMainOperation: false, autoCache: false)
97+
98+
```
99+
100+
### Requesting API from Network Layer
101+
102+
When operation is completed, completion block will return with two cases, whether error or successful response.
103+
104+
```swift
105+
guard let apiReq = api else {
106+
print("Something wrong with the configuration")
107+
return
108+
}
109+
110+
apiReq.request { (result) in
111+
switch result {
112+
case .error(let errResponse):
113+
print("Error: \(errResponse.error.localizedDescription)")
114+
case .success(let successfulResponse):
115+
print("Response Body: \(successfulResponse.responseBody)")
116+
}
117+
}
118+
```
119+
120+
## Advanced Usage
121+
122+
### Defining AutoCache
123+
In default, responses will be cached by the given `cachingTime` property. However, `ResponseBodyParsable` gives flexibility to define time value dynamically from the object itself. By overriding `cachingEndsAt:` method from the object, new time value for cache expiry can be defined.
124+
125+
In below, same example object overrides the method and uses, for instance one of the variables in the response to define caching expiry.
126+
127+
```swift
128+
/// Initializes object from JSON response.
129+
class ExampleResponseObject: ResponseBodyParsable {
130+
131+
var userId: Int
132+
var id: Int
133+
var title: String
134+
var completed: Bool
135+
var expiry: Date?
136+
137+
required init?(_ data: Data) {
138+
return nil
139+
}
140+
141+
required init?(_ response: Any?) {
142+
guard let dict = response as? [String:Any] else { return nil }
143+
guard let userId = dict["userId"] as? Int,
144+
let id = dict["id"] as? Int,
145+
let title = dict["title"] as? String,
146+
let completed = dict["completed"] as? Bool else { return nil }
147+
148+
self.userId = userId
149+
self.id = id
150+
self.title = title
151+
self.completed = completed
152+
153+
// gets expiry value from the response
154+
self.expiry = dict["expiry"] as? Date
155+
156+
super.init(response)
157+
}
158+
159+
override func cachingEndsAt() -> Date? {
160+
return self.expiry
161+
}
162+
}
163+
```
164+
Then, in the API configuration, `autoCache` parameter should be set to `true`.
165+
166+
### Operation Queues and priority of the requests
167+
`iOSUsefulNetworkLayer` holds two queues, one is called as `main` and the other one as `background`. In default, all request operations will be handled in the `background` queue unless `isMainOperation` property is set to `true`. It effects resource levels in the system level to use more resources, so requests which should needs to get answered and completed as soon as possible can moved into the `main` queue to use more resources from the system.
168+
169+
In addition, for the each request operation in the same queue will be handled by looking their `priority` specification. In default, all requests are marked as `normal` level, but by using that property more important requests can be moved into top by giving higher priority.

Sources/iOSUsefulNetworkLayer/NetworkLayer/APIConfiguration.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,16 @@ public struct APIConfiguration<T> where T: ResponseBodyParsable {
5858
/**
5959
Initializes Configuration with the host URL and endpoint separately.
6060
Returns nil if request URL cannot be created successfuly.
61+
- parameter hostURL: Base URL for the request. (e.g. "https://example.com")
62+
- parameter endPoint: Endpoint for the API request. (e.g. "v1/api/test")
63+
- parameter priority: Defines the priority of the operation in its queue.
64+
- parameter timeOut: Timeout value for the request to fail if it cannot get answer from network in seconds. In default, it's 30 seconds.
65+
- parameter cachingTime: Default caching time for the response, in default it's 1 hour.
66+
- parameter requestType: Type of the request. In default it's `get`.
6167
- parameter isMainOperation: If `true`, operation will be performed in special queue and return to main queue.
68+
- parameter headers: Headers for the API request. Don't need to add `content-Type` for JSON request bodies, which will be automatically added.
69+
- parameter body: Request body for the API request.
70+
- parameter responseBodyObject: Type of the Response Object to create.
6271
- parameter autoCache: To use that, override `cachingEndsAt:` method of Response Body Object.
6372
Then specified custom caching will be applied for that request.
6473
*/
@@ -91,7 +100,15 @@ public struct APIConfiguration<T> where T: ResponseBodyParsable {
91100

92101
/**
93102
Initializes Configuration with the URL
103+
- parameter url: Request URL.
104+
- parameter priority: Defines the priority of the operation in its queue.
105+
- parameter timeOut: Timeout value for the request to fail if it cannot get answer from network in seconds. In default, it's 30 seconds.
106+
- parameter cachingTime: Default caching time for the response, in default it's 1 hour.
107+
- parameter requestType: Type of the request. In default it's `get`.
94108
- parameter isMainOperation: If `true`, operation will be performed in special queue and return to main queue.
109+
- parameter headers: Headers for the API request. Don't need to add `content-Type` for JSON request bodies, which will be automatically added.
110+
- parameter body: Request body for the API request.
111+
- parameter responseBodyObject: Type of the Response Object to create.
95112
- parameter autoCache: To use that, override `cachingEndsAt:` method of Response Body Object.
96113
Then specified custom caching will be applied for that request.
97114
*/

0 commit comments

Comments
 (0)