Swiftpack.co - terwanerik/HTTPCodable as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by terwanerik.
terwanerik/HTTPCodable 0.2.0
Make a REST call, and get a Codable back, wrapped in a Future.
⭐️ 2
🕓 4 years ago
.package(url: "https://github.com/terwanerik/HTTPCodable.git", from: "0.2.0")

HTTPCodable

Build Status swift 4.2 osx | ubuntu | docker

HTTPCodable allows you to send HTTP requests (with Codable's as body) and get a Codable, wrapped in a Future, back. A picture is worth a thousand words:

struct Todo: Codable {
  var id: Int
  var userId: Int
  var title: String
  var completed: Bool
}

let url = "https://jsonplaceholder.typicode.com/todos"

Client.shared.get(url, as: [Todo].self).map { todos in
  // todos is an array of Todo objects
  return todos.filter { $0.completed }
}.whenFulfilled { completed in
  // completed is an array of completed todos
}

It heavily relies on Futures and will return a Future for all requests.

Examples

Have a look at HTTPCodableTests for more examples, the code itself is also pretty explanatory.

import HTTPCodable

/// Our object
struct Todo: Codable {
  var id: Int
  var userId: Int
  var title: String
  var completed: Bool
}

/// You can define your ?query=string&s=foo as Codable structs
struct TodoQuery: Codable {
  var id: Int?
  var userId: Int?
  var completed: Bool?
}

Client.shared.baseUrl = "https://jsonplaceholder.typicode.com/"

// Simple GET
Client.shared.get("/todos", as: [Todo].self).whenFulfilled { todos in
  // todos is an array of Todo objects
}

// GET with query
Client.shared.get("/todos", as: [Todo].self, query: TodoQuery(id: nil, userId: 1, completed: false)).whenFulfilled { todos in
  // todos is an array of Todo objects, with userId=1 and completed=false
}

// Post
let data = Todo(id: 1, userId: 1, title: "Foo", completed: true)
Client.shared.post(data, to: "/todos", as: Todo.self).whenFulfilled { todo in
  // todo is our newly created todo
}

// Put
Client.shared.put(data, to: "/todos/1", as: Todo.self).whenFulfilled { todo in
  // todo is our updated todo
}

// Patch
Client.shared.patch(data, to: "/todos/1", as: Todo.self).whenFulfilled { todo in
  // todo is our patched todo
}

// Delete
struct EmptyResponse: Codable {}

Client.shared.delete("/todos/1", as: EmptyResponse.self).whenFulfilled { _ in
  // removed
}

Getting started

HTTPCodable can be added to your project either using Swift package manager or manually (for now);

SPM

Add HTTPCodable as a dependency

dependencies: [
    .package(url: "https://github.com/terwanerik/HTTPCodable.git", from: "0.1.0")
]

Then import the module

import HTTPCodable

Manually

Download one of the releases, and just drag the /Sources/HTTPCodable folder in Xcode. Make sure to install Futures as a framework (e.g. via Carthage)

Supported Platforms

HTTPCodable supports all platforms where Futures is supported; so all platforms where Swift 3 and later is supported.

  • Ubuntu 14.04
  • macOS 10.9
  • tvOS 9.0
  • iOS 8.0
  • watchOS 2.0

GitHub

link
Stars: 2
Last commit: 4 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Dependencies

Release Notes

v0.2.0
4 years ago

Added methods that also pass the URLSessionTask so it can be cancelled if needed.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics