Swiftpack.co - Package - hkellaway/HottPotato

HottPotato

Delicious HTTP requests :fire: :potato:

I got tired of writing networking code over and over again for side projects, so I made something I could quickly toss in.

Features

CocoaPods Carthage compatible SPM Build Status

  • Simple HTTP requests
  • Automatic parsing of Decodable models

Getting Started

  • Download HottPotato and do a pod install on the included HottPotatoDemo app to see it in action
  • Check out the documentation for a more comprehensive look at the classes available in HottPotata

Swift Version

Currently compatible with Swift 5.0.

Installation with CocoaPods

pod 'HottPotato', :git => 'https://github.com/hkellaway/HottPotato.git', :tag => '0.1.0'

Installation with Carthage

github "hkellaway/HottPotato"

Installation with Swift Package Manager

import PackageDescription

let package = Package(
    name: "HelloWorld",
    dependencies: [
        .Package(url: "https://github.com/hkellaway/HottPotato.git", majorVersion: 0, minor: 1)
    ]
)

Usage

Retrieving Models

To make a request, create an HTTPResource with a type that is Decodable and enjoy receiving the Result.

let httpClient = HottPotato.shared
let resource = HTTPResource<GitHubProfile>(
    method: .GET,
    baseURL: "https://api.github.com",
    path: "/users/hkellaway"
)

httpClient.sendRequest(for: resource) { result in
	switch result {
		case .success(let profile):
			print("Hello world from \(profile.login)")
		case .failure(let error):
			print("Goodbye world: \(error.localizedDescription)")
	}
}

Discussion

HottPotato simply wraps URLSession and leverages some preferred context and patterns. Namely, it assumes we're making HTTP requests, we're receiving JSON back, and our requested models conform toDecodable. Additionally, it uses the Result type in its response.

Why? Because this is a typical setup for applications & libraries I've worked on. Plus, I'm a big fan of the Result type.

Retrieving JSON

Internally, HottPotato retrieves JSON - if you'd rather use it to retrieve JSON, simply:

guard let request = resource.toHTTPRequest() else {
	return
}

httpClient.sendJSONRequest(with: request, success: { (_, json) in
	print(json)
}, failure: { error in
	print(error)
})

Retrieving Raw Data and HTTPURLResponse

At its very heart, HottPotato simply makes a URLRequest using URLSession and returns the retrieved Data and HTTPURLResponse. If you'd rather use it to get back the raw response, simply:

httpClient.sendHTTPRequest(with: request, success: { (_, response) in
	print(response.statusCode)
}, failure: { error in
	print(error)
})

Credits

HottPotato was created by Harlan Kellaway.

License

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

Github

link
Stars: 0
Help us keep the lights on

Dependencies

Used By

Total: 1

Releases

0.1.0 - May 10, 2019

Updates:

  • Simple HTTP requests