Skip to content

Instantly share code, notes, and snippets.

@backslash-f
Last active May 27, 2019 13:43
Show Gist options
  • Save backslash-f/0f17656e65040d55d84b1a3c891df0bf to your computer and use it in GitHub Desktop.
Save backslash-f/0f17656e65040d55d84b1a3c891df0bf to your computer and use it in GitHub Desktop.
Vapor, POST returning Future<SomeContent>
// `POST` receiving an `Request` and returning an `ReceiptValidation`.
func validate(_ request: Request) throws -> Future<ReceiptValidation> {
let promise = request.eventLoop.newPromise(ReceiptValidation.self)
DispatchQueue.global().async {
guard let receipt = try? request.content.decode(Receipt.self).wait() else {
promise.fail(error: Abort(HTTPResponseStatus.badRequest))
return
}
let validation = ReceiptValidation(result: "Original receipt": \(receipt.data)")
promise.succeed(result: validation)
}
return promise.futureResult
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment