Skip to content

Latest commit

 

History

History
87 lines (61 loc) · 4.9 KB

README.md

File metadata and controls

87 lines (61 loc) · 4.9 KB

HTTP Client Module

build test swiftlint Codacy Badge swiftpackageindex swift versions swiftpackageindex platforms License: GPL v3

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.


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.


Integration

  1. Copy the resource url:
https://github.com/swiftui-packages/http-client-module.git
  1. Open your Xcode project

  2. 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

  3. Here you should be prompted to "Choose Package Repository:"

  4. Paste the resource url

  5. Select Next to go with the latest version or select a specific version or branch

  6. After a short loading period of package resolution you get prompted to Choose package products and targets (the default should be fine)

  7. The complete hit the Finish button

  8. Import HTTPClientModule into the files where you want to use it


Can also be found here


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