@@ -65,86 +65,86 @@ You can just import `HTTPClient` to use it.
6565
66661 . Implement a request body structure that conforms to the ` Encodable ` protocol. (Optional)
6767
68- ``` swift
69- struct RegisterUserRequestBody : Encodable {
70- let name: String
71- let description: String
72- }
73- ```
68+ ``` swift
69+ struct RegisterUserRequestBody : Encodable {
70+ let name: String
71+ let description: String
72+ }
73+ ```
7474
75752 . Implement a response body structure that conforms to the `Decodable` protocol.
7676
77- ``` swift
78- struct RegisterUserResponseBody : Decodable {
79- let id: String
80- }
81-
82- extension RegisterUserResponseBody {
83- func convertToUserID () -> UserID { .init (id : self .id ) }
84- }
85- ```
77+ ```swift
78+ struct RegisterUserResponseBody : Decodable {
79+ let id: String
80+ }
81+
82+ extension RegisterUserResponseBody {
83+ func convertToUserID () -> UserID { .init (id : self .id ) }
84+ }
85+ ```
8686
87873 . Implement a request structure that conforms to the `Request` protocol.
8888
89- ``` swift
90- import HTTPClient
91-
92- struct RegisterUserRequest : Request {
93- typealias ResponseBody = RegisterUserResponseBody
94- var path: String { " user/create_user" }
95- var httpMethod: HTTPMethod { .post }
96- var httpHeaders: [HTTPHeaderField: String ]? { [.contentType : ContentType.applicationJson .rawValue ] }
97- }
98- ```
89+ ```swift
90+ import HTTPClient
91+
92+ struct RegisterUserRequest : Request {
93+ typealias ResponseBody = RegisterUserResponseBody
94+ var path: String { " user/create_user" }
95+ var httpMethod: HTTPMethod { .post }
96+ var httpHeaders: [HTTPHeaderField: String ]? { [.contentType : ContentType.applicationJson .rawValue ] }
97+ }
98+ ```
9999
1001004 . Create an instance of the `HTTPClient` class and call the `request` method.
101101
102- ``` swift
103- struct UserID : Identifiable , Equatable {
104- let id: String
105- }
106- ```
107-
108- ``` swift
109- protocol VersatileRepository {
110- func registerUser (name : String , description : String , completion : @escaping (Result<UserID, Error >) -> Void )
111- }
112- ```
102+ ```swift
103+ struct UserID: Identifiable , Equatable {
104+ let id: String
105+ }
106+ ```
113107
114- ``` swift
115- import HTTPClient
108+ ```swift
109+ protocol VersatileRepository {
110+ func registerUser (name : String , description : String , completion : @escaping (Result<UserID, Error >) -> Void )
111+ }
112+ ```
116113
117- final class VersatileAPIClient {
118- static let shared = VersatileAPIClient ()
114+ ```swift
115+ import HTTPClient
119116
120- private let httpClient = HTTPClient (baseURLString : " https://example.com/api/" )
121- }
122-
123- extension VersatileAPIClient : VersatileRepository {
124- func registerUser (name : String , description : String , completion : @escaping (Result<UserID, Error >) -> Void ) {
125- let requestBody = RegisterUserRequestBody (name : name, description : description)
126- httpClient.request (RegisterUserRequest (), requestBody : requestBody) { result in
127- switch result {
128- case let .success (responseBody):
129- completion (.success (responseBody.convertToUserID ()))
130- case let .failure (error):
131- completion (.failure (error))
117+ final class VersatileAPIClient {
118+ static let shared = VersatileAPIClient ()
119+
120+ private let httpClient = HTTPClient (baseURLString : " https://example.com/api/" )
121+ }
122+
123+ extension VersatileAPIClient : VersatileRepository {
124+ func registerUser (name : String , description : String , completion : @escaping (Result<UserID, Error >) -> Void ) {
125+ let requestBody = RegisterUserRequestBody (name : name, description : description)
126+ httpClient.request (RegisterUserRequest (), requestBody : requestBody) { result in
127+ switch result {
128+ case let .success (responseBody):
129+ completion (.success (responseBody.convertToUserID ()))
130+ case let .failure (error):
131+ completion (.failure (error))
132+ }
132133 }
133134 }
134135 }
135- }
136- ```
137-
138- ``` swift
139- VersatileAPIClient. shared . registerUser ( name : " Uhooi " , description : " Green monster. " ) { result in
140- switch result {
141- case let . success (userID) :
142- // Do something.
143- case let . failure ( error) :
144- // Do error handling.
136+ ```
137+
138+ ```swift
139+ VersatileAPIClient. shared . registerUser ( name : " Uhooi " , description : " Green monster. " ) { result in
140+ switch result {
141+ case let . success (userID) :
142+ // Do something.
143+ case let . failure (error) :
144+ // Do error handling.
145+ }
145146 }
146- }
147- ```
147+ ```
148148
149149## Contribution
150150
0 commit comments