Skip to content

Commit 21b9c82

Browse files
committed
- documentation updated
1 parent afcd181 commit 21b9c82

File tree

1 file changed

+53
-4
lines changed

1 file changed

+53
-4
lines changed

README.md

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ pod 'RESTAPI'
4040

4141
# Examples
4242

43-
### Simple request
43+
## Request
44+
45+
### Basic request
4446

4547
By default you can perform a single request which returns a simple JSON response.
4648

@@ -50,6 +52,7 @@ testServerApi.get("/posts") { (error, data) in
5052
//This data will be SwiftyJSON's Optional JSON type by default (data: JSON?)
5153
}
5254
```
55+
5356
Or if you want to, you can get a parsed response type with the same request.
5457

5558
### Request with expected response type
@@ -94,20 +97,25 @@ testServerApi.get("/posts", query: ["userId" : "1"]) { (error, object: [ExampleR
9497

9598
Values in the query parameter dictionary must implement the `Queryable` protocol, `String` and `Array` types implement this by default.
9699

97-
### Body parameters
100+
## Body parameters
101+
102+
### Simple body
98103

99104
```swift
100105
testServerApi.post("/posts", data: ["body": "something","id": 1, "title": "Some title", "userId": 9]) { (error, object) in
101106
//...
102107
}
103108
```
109+
104110
Body parameters should comform to `ValidJSONObject` protocol. `Array` and `Dictionary` types implement this by default.
105111
Any custom type can implement `ValidJSONObject`, which requres a function that converts your type to `Data`.
106112

107113
```swift
108114
func JSONFormat() throws -> Data
109115
```
110116

117+
### Custom object in body
118+
111119
If you don't want that much controll and resposibility, you can implement `JSONConvertible`, which has a property named `parameterValue` which can be any `ValidJSONObject`. Practically you just have to convert your type to a `Dictionary` or an other `ValidJSONObject` just it like this:
112120

113121
```swift
@@ -125,6 +133,7 @@ extension ExampleData: JSONConvertible {
125133
}
126134
}
127135
```
136+
128137
Additional fields can be added anytime to this property, also, you can exclude any properties. After implementing it, uploading is simple:
129138

130139
```swift
@@ -135,7 +144,30 @@ testServerApi.post("/posts", data: uploadData) { (error, object) in
135144
}
136145
```
137146

138-
### Authenticating requests
147+
### Form encoded request
148+
149+
The framework also supports `form-encoded` requests as long as the response is in `JSON` format. To send one use a similar json request, but the body must comform to `ValidFormData` protocol.
150+
151+
If you want to save time, and ok with simple `[String: String]` format, just implement the `FormEncodable` protocol, which is much easyer and automatically conform to `ValidFormData`.
152+
153+
`Dictionary` has an added element called `formValue`, which is automatically conforms to `ValidFormData`. This is mostly a workaruond for a problem described [later](###Disclaimer)
154+
155+
```swift
156+
157+
extension ExampleData: FormEncodable {
158+
159+
var parameters: [String: String] {
160+
return ["body": body, "id": "\(id)", "title": title, "userId": "\(userId)"]
161+
}
162+
}
163+
164+
// ...
165+
oldServerApi.post("/post.php", query: ["dir": "gujci_test"], data: uploadData.formValue){ (error, response) in
166+
// ... do something
167+
}
168+
```
169+
170+
## Authenticating requests
139171

140172
To authenticate a request, you have to set the `authentication` property of the API instance. For now this framework supports simple, access token based authentications in HTTP header and URL query. This is a simple example to show how to set up a HTTP header based authentication.
141173

@@ -159,6 +191,23 @@ var sessionAuthenticator: RequestAuthenticator {
159191
//...
160192
```
161193

194+
## General body protocol
195+
196+
Any request body sent, must conform to `ValidRequestData` protocol. The 2 mentioned above are just some pre-implemented, widely used cases. If you use a custom protocol, or something, which is not implemented in this framework, just made the desired types conform to `ValidRequestData`, and they can be automatically used with the framework.
197+
198+
```swift
199+
public protocol ValidRequestData {
200+
// defines the content type header
201+
func type() -> ContentType
202+
// returns the data, to sent to server
203+
func requestData() throws -> Data
204+
}
205+
```
206+
207+
### Disclaimer
208+
209+
One type must not implement this protocol twice or more, meaning it is not supported to conform to both `ValidFormData` and `JSONConvertible`.
210+
162211
# Debugging
163212

164213
To turn on request logging add `APIRequestLoggingEnabled` to 'Arguments passed on launch' at Schema/Run/Arguments.
@@ -175,4 +224,4 @@ To log server sent errrors turn on `APIErrorLoggingEnabled`.
175224
- [ ] make JSON and [JSON] comform to JSONParseable to reduce redundant code
176225
- [ ] Add more unit tests
177226
- [x] Travis
178-
- [ ] Document form-encoded-support related changes
227+
- [x] Document form-encoded-support related changes

0 commit comments

Comments
 (0)