Skip to content

Commit

Permalink
#13: Add getQuota request
Browse files Browse the repository at this point in the history
  • Loading branch information
lika-vorobeva committed Oct 10, 2022
1 parent 145b980 commit 37ebcdc
Showing 1 changed file with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import Vapor
import SentinelWallet
import SOLARAPI

private struct Constants {
let path: PathComponent = "subscriptions"
Expand All @@ -18,6 +19,7 @@ struct SubscriptionsRouteCollection: RouteCollection {

func boot(routes: RoutesBuilder) throws {
routes.get(constants.path, use: getSubscriptions)
routes.get(constants.path, ":node", use: getQuota)
routes.post(constants.path, use: subscribe)
routes.delete(constants.path, use: unsubscribe)
}
Expand All @@ -29,7 +31,7 @@ extension SubscriptionsRouteCollection {
context.subscriptionsService.loadActiveSubscriptions { result in
switch result {
case let .failure(error):
continuation.resume(throwing: Abort(.init(statusCode: 500), reason: error.localizedDescription))
continuation.resume(throwing: error.encodedError())

case let .success(subscriptions):
let subscriptions = subscriptions.map { $0.node }
Expand All @@ -41,6 +43,37 @@ extension SubscriptionsRouteCollection {
})
}

func getQuota(_ req: Request) async throws -> String {
try await withCheckedThrowingContinuation({ (continuation: CheckedContinuation<String, Error>) in
guard let node = req.parameters.get("node") else {
continuation.resume(throwing: Abort(.badRequest))
return
}

context.subscriptionsService.loadActiveSubscriptions { result in
switch result {
case let .success(subscriptions):
guard let subscription = subscriptions.last(where: { $0.node == node })?.id else {
continuation.resume(throwing: Abort(.notFound))
return
}

context.subscriptionsService.queryQuota(for: subscription) { result in
switch result {
case let .failure(error):
continuation.resume(throwing: error.encodedError())

case let .success(quota):
Encoder.encode(model: quota, continuation: continuation)
}
}
case let .failure(error):
continuation.resume(throwing: error.encodedError())
}
}
})
}

func subscribe(_ req: Request) async throws -> Response {
try await withCheckedThrowingContinuation({ (continuation: CheckedContinuation<Response, Error>) in
do {
Expand All @@ -52,7 +85,7 @@ extension SubscriptionsRouteCollection {
) { result in
switch result {
case let .failure(error):
continuation.resume(throwing: Abort(.init(statusCode: 401), reason: error.localizedDescription))
continuation.resume(throwing: error.encodedError(status: 401))

case let .success(result):
guard result else {
Expand Down Expand Up @@ -81,22 +114,24 @@ extension SubscriptionsRouteCollection {
switch result {
case let .success(subscriptions):
let subscriptionsToCancel = subscriptions.filter { $0.node == nodeAddress }.map { $0.id }
guard !subscriptionsToCancel.isEmpty else {
continuation.resume(throwing: Abort(.notFound))
return
}

context.subscriptionsService.cancel(
subscriptions: subscriptionsToCancel,
with: nodeAddress
) { result in
switch result {
case let .failure(error):
continuation.resume(
throwing: Abort(.init(statusCode: 500), reason: error.localizedDescription)
)
continuation.resume(throwing: error.encodedError())
case .success:
continuation.resume(returning: Response(status: .ok))
}
}
case let .failure(error):
continuation.resume(throwing: Abort(.init(statusCode: 500), reason: error.localizedDescription))
continuation.resume(throwing: error.encodedError())
}
}
} catch {
Expand Down

0 comments on commit 37ebcdc

Please sign in to comment.