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

throw on error so the net time the command runes the requests will be… #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
14 changes: 13 additions & 1 deletion Sources/App/Commands/StackoverflowCommand.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Vapor
import FluentPostgreSQL

final class StackoverflowCommand: Command {
var arguments: [CommandArgument] {
Expand Down Expand Up @@ -40,7 +41,18 @@ final class StackoverflowCommand: Command {
guard let postingQuestion = questions.popLast()
else { return .done(on:context.container) }
return try webhookService.postContent(postingQuestion.link).flatMap { _ in
return try self.postNewQuestions(questions, context: context)
return try self.postNewQuestions(questions, context: context).catchFlatMap { err in
if let err = err as? DiscordError {
return self.deleteUnpostedQuestion(err.link, context: context)
}
return .done(on: context.container)
}
}
}

private func deleteUnpostedQuestion(_ link: String, context: CommandContext) -> Future<Void>{
return context.container.withPooledConnection(to: .psql) { (conn) -> EventLoopFuture<Void> in
return StackOverflowQuestion.query(on: conn).filter(\.link == link).delete()
}
}
}
21 changes: 16 additions & 5 deletions Sources/App/Services/DiscordWebhookService.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import Vapor

struct DiscordError: Error {
let link: String
}

// Read more about discord webhooks here
// https://discordapp.com/developers/docs/resources/webhook#execute-webhook
struct DiscordWebhookService: Service {
let client: Client
let webhookUrl: String
let logger: Logger
let jsonEncoder: DataEncoder

func postContent(_ content: String) throws -> Future<Void> {
let content = DiscordWebhookPayload(content: content)
let payload = DiscordWebhookPayload(content: content)
return client.post(webhookUrl, headers: HTTPHeaders()) { (req) in
req.http.body = try jsonEncoder.encode(content).convertToHTTPBody()
}.map(to: Void.self) { _ in
self.logger.info("Posted: \(content) response")
req.http.body = try jsonEncoder.encode(payload).convertToHTTPBody()
}.map(to: Void.self) { resp in
let code = resp.http.status.code
if code >= 200 && code < 300 {
self.logger.info("Posted: \(content) response")
} else if code == 429 {
throw DiscordError(link: content)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might make sense to rename DiscordError to better indicate that it's meant to be used for rate limited stuff, otherwise this looks good

} else {
self.logger.error("Unhandled code receviced in DiscordWebhookService.postContent")
}
}
}
}
Expand Down