MinimalNetworking 0.2.0

MinimalNetworking 0.2.0

Maintained by Ivan Tchernev.



  • By
  • Firanus

MinimalNetworking

CI Status Version License Platform

Minimal networking is exactly what's written on the tin; a simple, dependence free networking library that aims to streamline simple networking requests without any unnecessary bells and whistles.

Installation

MinimalNetworking is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'MinimalNetworking'

Usage

Using Minimal Networking is as simple as installing the pod (see Installation) and initialising an HttpClient. From there, you can access the get and post methods.

The get method is

let httpService = HttpService()
_ = httpService.get(url: "url_to_hit", 
                    responseContentType: ResponseContentType.json, 
                    additionalHeaders: ["Accept": "application/json"]) 
{ (networkResponse: NetworkResponse<ParsedServerData?>) in
    DispatchQueue.main.async {
        switch networkResponse {
        case .success(let parsedData, let response):
            //use your data
        case .failure(let error):
            //handle the error
        }
    }
}

Minimal Networking allows you to specify the expected response type of your data using the ResponseContentType enumeration. It then automatically parses the server response if it is a supported type (at the moment, only JSON is supported).

The post method is almost identical. The only difference is that it takes a body parameter of type POSTBodyEncodable, which must provide a String detailing how the class will be decoded into a bodyParam:

struct PostQuery: POSTBodyEncodable {
  //struct implementation details...

  var asFormUrlEncoded: String = "minimalNetworking=Awesome!"
}
_ = httpService.post(url: "https://httpbin.org/post", 
                    body: PostQuery(), 
                    responseContentType: ResponseContentType.json, 
                    additionalHeaders: ["Accept": "application/json"]) 
{     (networkResponse: NetworkResponse<PostResponse?>) in 
  //completion handler functionality
}

The parsed response is returned to the user into a simple enum, with structure:

public enum NetworkResponse<T> {
    case success(T, HTTPURLResponse)
    case failure(Error)
}

In the case of a success, you will return your parsed class, and the HTTPUrlResponse class. Otherwise, you will receive a failure message method with details on the error you have received.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

MinimalNetworking requires iOS 9.3 or above. Update dem projects yo.

Future

The Minimal Networking Pod is still in a pre-release state, and there are a number of features still planned for before full launch. These include:

  • Support for more request types
  • Providing functionality to automatically encode a class into a POST body
  • Better support for requests that do not expect data to be returned
  • and more!

Please check out the Github project tab for more information.

Author

Firanus, [email protected]

License

MinimalNetworking is available under the MIT license. See the LICENSE file for more info.