You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
And... voila! We defined a `login(_:)` request which will request login endpoint by sending a `UserBody` and waiting for a `UserResponse`. Now it's time to use it.
22
+
The package come with 2 modules:
24
23
25
-
You can declare constant endpoints if needed (refer to Endpoint documentation to see more):
24
+
-`SimpleHTTP` which bring the full framework API described in this README
25
+
-`SimpleHTTPFoundation` which only bring a few addition to Foundation API. See this [article](https://swiftunwrap.com/article/designing-http-framework-foundation/) or [API doc](https://pjechris.github.io/SimpleHTTP/) to have a glimpse of what is provided.
26
26
27
-
```swift
28
-
extensionEndpoint {
29
-
staticlet login: Endpoint ="login"
30
-
}
27
+
## Basic Usage
31
28
29
+
### Building a request
30
+
31
+
You make requests by creating `Request` objects. You can either create them manually or provide static definition by extending `Request`:
This defines a `Request.login(_:)` method which create a request targeting "login" endpoint by sending a `UserBody` and expecting a `UserResponse` as response.
42
+
39
43
### Sending a request
40
44
41
-
To send a request use a `Session` instance. `Session` is somewhat similar to `URLSession` but providing additional functionalities.
45
+
You can use your request along `URLSession` by converting it into a `URLRequest` by calling `request.toURLRequest(encoder:relativeTo:accepting)`.
46
+
47
+
You can also use a `Session` object. `Session` is somewhat similar to `URLSession` but providing additional functionalities:
48
+
49
+
- encoder/decoder for all requests
50
+
- error handling
51
+
- ability to [intercept](#interceptor) requests
42
52
43
53
```swift
44
54
45
-
let session =Session(baseURL: URL(string: "https://github.com")!, encoder: JSONEncoder(), decoder: JSONDecoder())
You can now use the returned publisher however you want. Its result is similar to what you have received with `URLSession.shared.dataTaskPublisher(for: ...).decode(type: UserResponse.self, decoder: JSONDecoder())`.
52
-
53
65
A few words about Session:
54
66
55
67
-`baseURL` will be prepended to all call endpoints
@@ -58,69 +70,99 @@ A few words about Session:
58
70
59
71
## Send a body
60
72
73
+
Request support two body types:
74
+
75
+
-[Encodable](#encodable)
76
+
-[Multipart](#multipart)
77
+
61
78
### Encodable
62
79
63
-
You will build your request by sending your `body` to construct it:
80
+
To send an Encodable object just set it as your Request body:
Protocol `Interceptor` enable powerful request interceptions. This include authentication, logging, request retrying, etc...
159
+
When using Session you can add automatic behavior to your requests/responses using `Interceptor` like authentication, logging, request retrying, etc...
118
160
119
161
### `RequestInterceptor`
120
162
121
-
`RequestInterceptor`allow to adapt a or retry a request whenever it failed:
163
+
`RequestInterceptor`allows to adapt and/or retry a request:
122
164
123
-
-`adaptRequest` method is called before making a request and allow you to transform it adding headers, changing path, ...
165
+
-`adaptRequest` method is called before making a request allowing you to transform it adding headers, changing path, ...
124
166
-`rescueRequestError` is called whenever the request fail. You'll have a chance to retry the request. This can be used to re-authenticate the user for instance
0 commit comments