In order to be able to perform HTTP requests, Swift provides the URLSession class with the Foundation Framework. This is traditionally implemented in an object-oriented style with a singleton instance and according to today's standards is no longer really intuitive to use. Instead of receiving all request-related attributes in a constructor, it is necessary to create an object, set its attributes and then execute the resume() function on it. If you want to use the response of a request in your app, which seems obvious, a main thread dispatch is also required. All of this boiler plate means that code quality decreases and with it its readability and maintainability.
This package is supposed to make it possible to send HTTP requests in a highly intuitive way. In contrast to, for example Alamofire, it shall also appear as minimalistic as possible.
Since it is often required to for example start a loading indicator with a http request and end it with its response, this module comes with Lifecycle hooks:
- beforeRequest
- response
- afterResponse
The response object is a tuple consisting of the response status and the response body. It can be destructured at the beginning of the closure or be passed as a object:
HTTPClient.get(url: "www.abc.com") { responseStatus, responseBody in
}
HTTPClient.get(url: "www.abc.com") { response in
response.body // Data
response.status // HTTPClient.ResponseStatus
response.status.code // Int
}
To further reduce complexity in your project the response body parameter accepts decodable object, which it automatically converts to json Strings and adds the corresponding content type as http header.
- Copy the resource url:
https://github.com/swiftui-packages/http-client-module.git
-
Open your Xcode project
-
Two options a and b for step 3
a) At the menu bar navigate to File / Swift Packages / Add Package Dependency
b1) Select the project's root folder
b2) select your app name under PROJECT
b3) Open Swift Packages tab on the right side of Info and Build Settings
b4) Hit the + button at the bottom of the list -
Here you should be prompted to "Choose Package Repository:"
-
Paste the resource url
-
Select Next to go with the latest version or select a specific version or branch
-
After a short loading period of package resolution you get prompted to Choose package products and targets (the default should be fine)
-
The complete hit the Finish button
-
Import HTTPClientModule into the files where you want to use it
-
maintaining README.md file
- GIF of importing Swift Package Manager Packages into Xcode Projects
- phrasing an introduction text
- writing usage instructions with code examples
-
feature: response encoding with type.self as parameter
-
further research about http for file upload and form data