From 2ab6701ea396acc186e11cc96c77461265cde2a4 Mon Sep 17 00:00:00 2001 From: Simon Kempendorf Date: Thu, 21 Feb 2019 08:25:45 +0100 Subject: [PATCH] Return GorushResponse instead of HTTPResponse --- Sources/Gorush/Gorush.swift | 8 +++++--- Sources/Gorush/GorushResponse.swift | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 Sources/Gorush/GorushResponse.swift diff --git a/Sources/Gorush/Gorush.swift b/Sources/Gorush/Gorush.swift index 65b081b..61772ec 100644 --- a/Sources/Gorush/Gorush.swift +++ b/Sources/Gorush/Gorush.swift @@ -21,19 +21,21 @@ public final class Gorush: Service { self.url = url } - public func dispatch(_ gorushMessage: GorushMessage, on worker: Worker) -> Future { + public func dispatch(_ gorushMessage: GorushMessage, on worker: Worker) -> Future { return HTTPClient.connect(scheme: httpScheme, hostname: hostname, port: port, on: worker).flatMap { client in let headers = [("Content-Type", "application/json")] let body = try JSONEncoder().encode(gorushMessage) let request = HTTPRequest(method: .POST, url: self.url, headers: HTTPHeaders(headers), body: body) - return client.send(request) + return client.send(request).map { response in + return try JSONDecoder().decode(GorushResponse.self, from: response.body.data!) + } } } // MARK: Convenience method - public func dispatch(_ gorushNotification: GorushNotification, on worker: Worker) -> Future { + public func dispatch(_ gorushNotification: GorushNotification, on worker: Worker) -> Future { return dispatch(GorushMessage(notifications: [gorushNotification]), on: worker) } } diff --git a/Sources/Gorush/GorushResponse.swift b/Sources/Gorush/GorushResponse.swift new file mode 100644 index 0000000..55bb50f --- /dev/null +++ b/Sources/Gorush/GorushResponse.swift @@ -0,0 +1,23 @@ +// +// GorushNotification.swift +// Gorush +// +// Created by Simon Kempendorf on 15.01.19. +// + +import Foundation +import Vapor + +public struct GorushResponse: Content { + public let counts: Int + public let success: String + public let logs: [Log] + + public struct Log: Content { + public let type: String + public let platform: String + public let token: String + public let message: String + public let error: String + } +}