项目作者: hkellaway

项目描述 :
Delicious HTTP requests for iOS :fire: :potato:
高级语言: Swift
项目地址: git://github.com/hkellaway/HottPotato.git
创建时间: 2019-05-10T01:54:01Z
项目社区:https://github.com/hkellaway/HottPotato

开源协议:MIT License

下载


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

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

Installation with Carthage

  1. github "hkellaway/HottPotato"

Installation with Swift Package Manager

  1. import PackageDescription
  2. let package = Package(
  3. name: "HelloWorld",
  4. dependencies: [
  5. .Package(url: "https://github.com/hkellaway/HottPotato.git", majorVersion: 0, minor: 1)
  6. ]
  7. )

Usage

Retrieving Models

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

  1. let httpClient = HottPotato.shared
  2. let resource = HTTPResource<GitHubProfile>(
  3. method: .GET,
  4. baseURL: "https://api.github.com",
  5. path: "/users/hkellaway"
  6. )
  7. httpClient.sendRequest(for: resource) { result in
  8. switch result {
  9. case .success(let profile):
  10. print("Hello world from \(profile.login)")
  11. case .failure(let error):
  12. print("Goodbye world: \(error.localizedDescription)")
  13. }
  14. }

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:

  1. guard let request = resource.toHTTPRequest() else {
  2. return
  3. }
  4. httpClient.sendJSONRequest(with: request, success: { (_, json) in
  5. print(json)
  6. }, failure: { error in
  7. print(error)
  8. })

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:

  1. httpClient.sendHTTPRequest(with: request, success: { (_, response) in
  2. print(response.statusCode)
  3. }, failure: { error in
  4. print(error)
  5. })

Credits

HottPotato was created by Harlan Kellaway.

License

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