Delicious HTTP requests 🔥 🥔
I got tired of writing networking code over and over again for side projects, so I made something I could quickly toss in.
- Simple HTTP requests
- Automatic parsing of
Decodable
models
- Download HottPotato and do a
pod install
on the includedHottPotatoDemo
app to see it in action - Check out the documentation for a more comprehensive look at the classes available in HottPotata
Currently compatible with Swift 5.0.
pod 'HottPotato', :git => 'https://github.com/hkellaway/HottPotato.git', :tag => '0.1.0'
github "hkellaway/HottPotato"
import PackageDescription
let package = Package(
name: "HelloWorld",
dependencies: [
.Package(url: "https://github.com/hkellaway/HottPotato.git", majorVersion: 0, minor: 1)
]
)
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)")
}
}
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.
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)
})
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)
})
HottPotato was created by Harlan Kellaway.
HottPotato is available under the MIT license. See the LICENSE file for more info.