swiftui-packages/http-client-module
In order to be able to perform HTTP requests, Swift provides the [URLSession class](https://developer.apple.com/documentation/foundation/urlsession) with the Foundation Framework. This is traditionally implemented in an object-oriented style with a singleton instance and accordin
Usage
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.
<br>
Integration
- 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<br>
a) At the menu bar navigate to File / Swift Packages / Add Package Dependency<br> b1) Select the project's root folder<br> b2) select your app name under PROJECT<br> b3) Open Swift Packages tab on the right side of Info and Build Settings<br> b4) Hit the + button at the bottom of the list<br>
- 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
<br>
Can also be found here
<br>
ToDos
- 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
Package Metadata
Repository: swiftui-packages/http-client-module
Default branch: main
README: README.md