Skip to content

Netw is a lightweight iOS wrapper around URLSession that allows you to define clean HTTP requests

License

Notifications You must be signed in to change notification settings

franckclement/Netw

Repository files navigation

Netw

Netw is a lightweight iOS wrapper around URLSession that allows you to define clean HTTP requests

Swift Version Build Status codecov License CocoaPods Compatible

Netw provides a clean way to define and make your networking calls. It supports standard HTTP methods as well as multipart/formdata media upload.

Features

  • Define clean and isolated HTTP requests
  • Support for multipart/formdata requests
  • Built on top of URLSession
  • No external libraries imported

Requirements

  • iOS 10.0+

Installation

CocoaPods

You can use CocoaPods to install Netw by adding it to your Podfile:

pod 'Netw'

Carthage

Create a Cartfile that lists the framework and run carthage update. Follow the instructions to add $(SRCROOT)/Carthage/Build/iOS/Netw.framework to an iOS project.

github "franckclement/Netw"

Usage example

1) Create a Request struct with your request settings

For a standard HTTP request

import Netw

struct GithubRepositorySearchRequest: Request {
    // Response type must conforms to Decodable
    typealias Response = GithubSearchResults<GithubRepository>
    
    // Setup your requests parameters as properties
    let queryRequest: String
    
    var data: DataType {
        let data = RequestData(path: "https://api.github.com/search/repositories",
                               method: .get,
                               queryParams: ["q": queryRequest])
        
        return DataType.standard(data: data)
    }
    
    // Initialize your custom request passing your custom parameters
    init(queryRequest: String) {
        self.queryRequest = queryRequest
    }
}

GithubSearchResults<> and GithubRepository types can be seen here

For a media HTTP multipart/formdata POST request

import Netw

struct ImgurMediaPostRequest: Request {
    typealias Response = ImgurImageUploadResponse
    
    var data: DataType {
        // We get an in-bundle image for test purpose
        let bundle = Bundle.init(for: YouBundleClass.self)
        let image = UIImage(named: "mockImage", in: bundle, compatibleWith: nil)!
        // Use the built in Media type to create your medias to ulpload
        let media = Media(key: "image",
                          fileName: "mockImage",
                          data: image.jpegData(compressionQuality: 0.5)!,
                          mimeType: "image/png")
        let data = RequestMediaData(path: "https://api.imgur.com/3/upload",
                                    medias: [media])
        return DataType.media(data: data)
    }
}

ImgurImageUploadResponse type can be seen here

2) Use your custom Request.execute(completion:) method to dispatch network calls

let repositoryRequest = GithubRepositorySearchRequest(queryRequest: "RxSwift")
_ = repositoryRequest.execute(completion: { result in
    switch result {
    case .success(let response):
        print(response)
    case .failure(let error):
        print(error)
        break
    }
})

Contribute

Submit a pull request 🚀

Meta

Franck CLEMENT – @Twitter

Distributed under the MIT license. See LICENSE for more information.

About

Netw is a lightweight iOS wrapper around URLSession that allows you to define clean HTTP requests

Resources

License

Stars

Watchers

Forks

Packages

No packages published