Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash for SMS sent from network request #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Sources/Twilio/Twilio.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Vapor

public protocol TwilioProvider {
func send(_ sms: OutgoingSMS) -> EventLoopFuture<ClientResponse>
func send(_ sms: OutgoingSMS, on eventLoop: EventLoopGroup?) -> EventLoopFuture<ClientResponse>
}

public struct Twilio: TwilioProvider {
Expand Down Expand Up @@ -32,18 +32,18 @@ extension Twilio {
// MARK: Send message

extension Twilio {
/// Send sms
/// Send sms. If you are sending SMS as result of network request, you need to use Request.eventLoop to send SMS, or your Vapor app is going to crash.
///
/// - Parameters:
/// - content: outgoing sms
/// - container: Container
/// - sms: outgoing sms
/// - eventLoop: EventLoop, on which SMS needs to be sent. Defaults to Application.eventLoopGroup.
/// - Returns: Future<Response>
public func send(_ sms: OutgoingSMS) -> EventLoopFuture<ClientResponse> {
public func send(_ sms: OutgoingSMS, on eventLoop: EventLoopGroup? = nil) -> EventLoopFuture<ClientResponse> {
guard let configuration = self.configuration else {
fatalError("Twilio not configured. Use app.twilio.configuration = ...")
}

return application.eventLoopGroup.future().flatMapThrowing { _ -> HTTPHeaders in
return (eventLoop ?? application.eventLoopGroup).future().flatMapThrowing { _ -> HTTPHeaders in
let authKeyEncoded = try self.encode(accountId: configuration.accountId, accountSecret: configuration.accountSecret)
var headers = HTTPHeaders()
headers.add(name: .authorization, value: "Basic \(authKeyEncoded)")
Expand Down