Contents

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

  1. Copy the resource url:
https://github.com/swiftui-packages/http-client-module.git
  1. Open your Xcode project
  1. Two options a and b for step 3<br>

a) &nbsp; 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>

  1. Here you should be prompted to "Choose Package Repository:"
  1. Paste the resource url
  1. Select Next to go with the latest version or select a specific version or branch
  1. After a short loading period of package resolution you get prompted to Choose package products and targets (the default should be fine)
  1. The complete hit the Finish button
  1. 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