Skip to content

Commit

Permalink
Merge pull request #5 from vkill/via-async-http-client
Browse files Browse the repository at this point in the history
 Replace Vapor to AsyncHTTPClient
  • Loading branch information
Anthony authored Sep 6, 2019
2 parents 68ffc5f + b2e00b5 commit 1b3af5e
Show file tree
Hide file tree
Showing 13 changed files with 182 additions and 259 deletions.
13 changes: 13 additions & 0 deletions .swiftformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# file options

--exclude .build
--exclude Tests/OneSignalTests/XCTestManifests.swift

# format options

--self insert
--patternlet inline
--stripunusedargs unnamed-only
--comments ignore

# rules
142 changes: 8 additions & 134 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -2,156 +2,30 @@
"object": {
"pins": [
{
"package": "Console",
"repositoryURL": "https://github.com/vapor/console.git",
"package": "async-http-client",
"repositoryURL": "https://github.com/swift-server/async-http-client.git",
"state": {
"branch": null,
"revision": "d6cf07af59ae63cd95c4b5f98cf1f25627750fd1",
"version": "3.1.0"
}
},
{
"package": "Core",
"repositoryURL": "https://github.com/vapor/core.git",
"state": {
"branch": null,
"revision": "1794ff138bd669175a2528d27695028d7cb30471",
"version": "3.5.0"
}
},
{
"package": "Crypto",
"repositoryURL": "https://github.com/vapor/crypto.git",
"state": {
"branch": null,
"revision": "bce9ac891c9b33fc045deda713e0d976d13b749a",
"version": "3.3.1"
}
},
{
"package": "DatabaseKit",
"repositoryURL": "https://github.com/vapor/database-kit.git",
"state": {
"branch": null,
"revision": "3557894af50914e134803a684b42a9ea6eefaea2",
"version": "1.3.2"
}
},
{
"package": "HTTP",
"repositoryURL": "https://github.com/vapor/http.git",
"state": {
"branch": null,
"revision": "6973bf50dab8dd00e4daf8cb82ca72b33f5db016",
"version": "3.1.6"
}
},
{
"package": "Multipart",
"repositoryURL": "https://github.com/vapor/multipart.git",
"state": {
"branch": null,
"revision": "e57007c23a52b68e44ebdfc70cbe882a7c4f1ec3",
"version": "3.0.2"
}
},
{
"package": "Routing",
"repositoryURL": "https://github.com/vapor/routing.git",
"state": {
"branch": null,
"revision": "3219e328491b0853b8554c5a694add344d2c6cfb",
"version": "3.0.1"
}
},
{
"package": "Service",
"repositoryURL": "https://github.com/vapor/service.git",
"state": {
"branch": null,
"revision": "281a70b69783891900be31a9e70051b6fe19e146",
"version": "1.0.0"
"revision": "64851a1a0a2a9e8fa7ae7b3508ce46a1da4a2e1d",
"version": "1.0.0-alpha.2"
}
},
{
"package": "swift-nio",
"repositoryURL": "https://github.com/apple/swift-nio.git",
"state": {
"branch": null,
"revision": "a20e129c22ad00a51c902dca54a5456f90664780",
"version": "1.12.0"
"revision": "790827800d6af12ca6a8b1dca7a9072606fd2a1e",
"version": "2.7.0"
}
},
{
"package": "swift-nio-ssl",
"repositoryURL": "https://github.com/apple/swift-nio-ssl.git",
"state": {
"branch": null,
"revision": "0f3999f3e3c359cc74480c292644c3419e44a12f",
"version": "1.4.0"
}
},
{
"package": "swift-nio-ssl-support",
"repositoryURL": "https://github.com/apple/swift-nio-ssl-support.git",
"state": {
"branch": null,
"revision": "c02eec4e0e6d351cd092938cf44195a8e669f555",
"version": "1.0.0"
}
},
{
"package": "swift-nio-zlib-support",
"repositoryURL": "https://github.com/apple/swift-nio-zlib-support.git",
"state": {
"branch": null,
"revision": "37760e9a52030bb9011972c5213c3350fa9d41fd",
"version": "1.0.0"
}
},
{
"package": "TemplateKit",
"repositoryURL": "https://github.com/vapor/template-kit.git",
"state": {
"branch": null,
"revision": "aff2d6fc65bfd04579b0201b31a8d6720239c1cf",
"version": "1.1.1"
}
},
{
"package": "URLEncodedForm",
"repositoryURL": "https://github.com/vapor/url-encoded-form.git",
"state": {
"branch": null,
"revision": "932024f363ee5ff59059cf7d67194a1c271d3d0c",
"version": "1.0.5"
}
},
{
"package": "Validation",
"repositoryURL": "https://github.com/vapor/validation.git",
"state": {
"branch": null,
"revision": "4de213cf319b694e4ce19e5339592601d4dd3ff6",
"version": "2.1.1"
}
},
{
"package": "Vapor",
"repositoryURL": "https://github.com/vapor/vapor.git",
"state": {
"branch": null,
"revision": "54cbf396147aba77ecb32e7fc75a3d13251551b1",
"version": "3.1.1"
}
},
{
"package": "WebSocket",
"repositoryURL": "https://github.com/vapor/websocket.git",
"state": {
"branch": null,
"revision": "21eb4773e25a8ff96fe347a31fe106900a69fa6a",
"version": "1.1.1"
"revision": "f5dd7a60ff56f501ff7bf9be753e4b1875bfaf20",
"version": "2.4.0"
}
}
]
Expand Down
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:4.0
// swift-tools-version:5.0
// Managed by ice

import PackageDescription
Expand All @@ -9,10 +9,10 @@ let package = Package(
.library(name: "OneSignal", targets: ["OneSignal"]),
],
dependencies: [
.package(url: "https://github.com/vapor/vapor.git", from: "3.0.0")
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.0.0-alpha.1"),
],
targets: [
.target(name: "OneSignal", dependencies: ["Vapor"]),
.target(name: "OneSignal", dependencies: ["AsyncHTTPClient"]),
.testTarget(name: "OneSignalTests", dependencies: ["OneSignal"]),
]
)
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ Using OneSignal on Vapor.
For sending to an array of device tokens:

```swift
let apiKey = "YourApiKey"
let appId = "YourAppId"

let deviceTokens = ["foo...", "bar..."]
let message = OneSignalMessage("Hello Vapor!")
let notif = OneSignalNotification(message: message, iosDeviceTokens: [deviceTokens])
let app = OneSignalApp(apiKey: Environment.get("ONESIGNAL_API_KEY") ?? "", appId: Environment.get("ONESIGNAL_APP_ID") ?? "")
let result = try OneSignal.makeService(for: request).send(notification: notif, toApp: app)
let notif = OneSignalNotification(message: message, users: deviceTokens)
let app = OneSignalApp(apiKey: apiKey, appId: appId)

let eventLoop = MultiThreadedEventLoopGroup(numberOfThreads: 1).next()
let resultFuture = try OneSignal(on: eventLoop).send(notification: notif, toApp: app)
```
48 changes: 25 additions & 23 deletions Sources/OneSignal/OneSignal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,34 @@
//
// Created by Anthony Castelli on 9/5/18.
//

import AsyncHTTPClient
import Foundation
import Vapor

public final class OneSignal: ServiceType {

var worker: Container
var client: FoundationClient

public static func makeService(for worker: Container) throws -> OneSignal {
return try OneSignal(worker: worker)
import NIO

public final class OneSignal {
let httpClient: HTTPClient

public init(httpClient: HTTPClient) {
self.httpClient = httpClient
}

public init(worker: Container) throws {
self.worker = worker
self.client = try FoundationClient.makeService(for: worker)

public init(on eventLoop: EventLoop) {
self.httpClient = HTTPClient(eventLoopGroupProvider: .shared(eventLoop))
}


deinit {
try? httpClient.syncShutdown()
}

/// Send the message
public func send(notification: OneSignalNotification, toApp app: OneSignalApp) throws -> Future<OneSignalResult> {
return try self.client.send(notification.generateRequest(on: self.worker, for: app)).map(to: OneSignalResult.self) { response in
guard let body = response.http.body.data else {
public func send(notification: OneSignalNotification, toApp app: OneSignalApp) throws -> EventLoopFuture<OneSignalResult> {
return try self.sendRaw(notification: notification, toApp: app).map { response in
guard var responseBody = response.body, let body = responseBody.readBytes(length: responseBody.readableBytes) else {
return OneSignalResult.error(error: OneSignalError.internal)
}
guard response.http.status == .ok else {

guard response.status == .ok else {
if let message = String(bytes: body, encoding: .utf8) {
return OneSignalResult.error(error: OneSignalError.requestError(value: message))
}
Expand All @@ -37,9 +40,8 @@ public final class OneSignal: ServiceType {
return OneSignalResult.success
}
}
public func sendRaw(notification: OneSignalNotification, toApp app: OneSignalApp) throws -> Future<Response> {
return try self.client.send(notification.generateRequest(on: self.worker, for: app))

public func sendRaw(notification: OneSignalNotification, toApp app: OneSignalApp) throws -> EventLoopFuture<HTTPClient.Response> {
return try self.httpClient.execute(request: notification.generateRequest(for: app))
}

}
4 changes: 2 additions & 2 deletions Sources/OneSignal/OneSignalApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ public struct OneSignalApp: Codable {
case apiKey = "api_key"
case appId = "app_id"
}

public var apiKey: String
public var appId: String

public init(apiKey: String, appId: String) {
self.apiKey = apiKey
self.appId = appId
Expand Down
6 changes: 3 additions & 3 deletions Sources/OneSignal/OneSignalMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import Foundation

public struct OneSignalMessage: Codable {
internal var messages: [String : String]
internal var messages: [String: String]

public init(_ message: String) {
// English is required by `OneSignal`
self.messages = ["en": message]
}

public subscript(key: String) -> String? {
get {
return self.messages[key]
Expand Down
Loading

0 comments on commit 1b3af5e

Please sign in to comment.