HTTPCodable

0.2.0

Make a REST call, and get a Codable back, wrapped in a Future.
terwanerik/HTTPCodable

What's New

v0.2.0

2019-12-19T10:35:56Z

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

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

Description

  • Swift Tools 4.0.0
View More Packages from this Author

Dependencies

Last updated: Sun Mar 17 2024 14:24:29 GMT-0900 (Hawaii-Aleutian Daylight Time)