From 8fbf83943715adafc50b78c160eff5daf14182aa Mon Sep 17 00:00:00 2001 From: Viktoriia Kostyleva Date: Thu, 6 Oct 2022 14:36:49 +0300 Subject: [PATCH] #7: Add GET subscriptions request --- .../project.pbxproj | 12 ++++++ .../SubscriptionsRouteCollection.swift | 40 +++++++++++++++++++ .../Root/Server/DVPNServer.swift | 1 + 3 files changed, 53 insertions(+) create mode 100644 SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/Subscriptions/SubscriptionsRouteCollection.swift diff --git a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS.xcodeproj/project.pbxproj b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS.xcodeproj/project.pbxproj index f77abb3..9655130 100644 --- a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS.xcodeproj/project.pbxproj +++ b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS.xcodeproj/project.pbxproj @@ -98,6 +98,7 @@ 923C373A28EC79E8003CFC03 /* Wallet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 923C373928EC79E8003CFC03 /* Wallet.swift */; }; 923C373C28ED79A2003CFC03 /* Mnemonic.swift in Sources */ = {isa = PBXBuildFile; fileRef = 923C373B28ED79A2003CFC03 /* Mnemonic.swift */; }; 923C373E28EDB109003CFC03 /* PostMnemonicResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 923C373D28EDB109003CFC03 /* PostMnemonicResponse.swift */; }; + 923C3B9628EEF06D003CFC03 /* SubscriptionsRouteCollection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 923C3B9528EEF06D003CFC03 /* SubscriptionsRouteCollection.swift */; }; 92D6B3FD28E19E20004CF9DF /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92D6B3FC28E19E20004CF9DF /* AppDelegate.swift */; }; 92D6B3FF28E19E20004CF9DF /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92D6B3FE28E19E20004CF9DF /* SceneDelegate.swift */; }; 92D6B40128E19E20004CF9DF /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 92D6B40028E19E20004CF9DF /* ViewController.swift */; }; @@ -261,6 +262,7 @@ 923C373928EC79E8003CFC03 /* Wallet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Wallet.swift; sourceTree = ""; }; 923C373B28ED79A2003CFC03 /* Mnemonic.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Mnemonic.swift; sourceTree = ""; }; 923C373D28EDB109003CFC03 /* PostMnemonicResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostMnemonicResponse.swift; sourceTree = ""; }; + 923C3B9528EEF06D003CFC03 /* SubscriptionsRouteCollection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubscriptionsRouteCollection.swift; sourceTree = ""; }; 92D6B3F928E19E20004CF9DF /* SOLARdVPNCommunityCoreiOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SOLARdVPNCommunityCoreiOS.app; sourceTree = BUILT_PRODUCTS_DIR; }; 92D6B3FC28E19E20004CF9DF /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 92D6B3FE28E19E20004CF9DF /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; @@ -631,6 +633,7 @@ children = ( 225A83AD28EED6AE00F66619 /* DNS */, 923C372428E72FF0003CFC03 /* Nodes */, + 923C3B9428EEF058003CFC03 /* Subscriptions */, 225A83A428EC297500F66619 /* Tunnel */, 923C373528EC7904003CFC03 /* Wallet */, ); @@ -683,6 +686,14 @@ path = Models; sourceTree = ""; }; + 923C3B9428EEF058003CFC03 /* Subscriptions */ = { + isa = PBXGroup; + children = ( + 923C3B9528EEF06D003CFC03 /* SubscriptionsRouteCollection.swift */, + ); + path = Subscriptions; + sourceTree = ""; + }; 92D6B3F028E19E20004CF9DF = { isa = PBXGroup; children = ( @@ -1094,6 +1105,7 @@ 22C3EED228E48B52007DB01B /* TunnelContainer.swift in Sources */, 22488A9A28E729E600FE29C3 /* GeneralSettingsStorage.swift in Sources */, 22C3EEFB28E48F95007DB01B /* NotificationToken.swift in Sources */, + 923C3B9628EEF06D003CFC03 /* SubscriptionsRouteCollection.swift in Sources */, 225A836B28EACE6C00F66619 /* ConnectionModelError.swift in Sources */, 22C3EED428E48B52007DB01B /* TunnelsService.swift in Sources */, 225A839D28EAE54400F66619 /* SecurityService.swift in Sources */, diff --git a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/Subscriptions/SubscriptionsRouteCollection.swift b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/Subscriptions/SubscriptionsRouteCollection.swift new file mode 100644 index 0000000..29de087 --- /dev/null +++ b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/RouteCollections/Subscriptions/SubscriptionsRouteCollection.swift @@ -0,0 +1,40 @@ +// +// SubscriptionsRouteCollection.swift +// SOLARdVPNCommunityCoreiOS +// +// Created by Viktoriia Kostyleva on 06.10.2022. +// + +import Vapor + +private struct Constants { + let path: PathComponent = "subscriptions" +} +private let constants = Constants() + +struct SubscriptionsRouteCollection: RouteCollection { + let context: HasSubscriptionsService + + func boot(routes: RoutesBuilder) throws { + routes.get(constants.path, use: getSubscriptions) + } +} + +extension SubscriptionsRouteCollection { + func getSubscriptions(_ req: Request) async throws -> String { + try await withCheckedThrowingContinuation({ (continuation: CheckedContinuation) in + context.subscriptionsService.loadActiveSubscriptions { result in + switch result { + case let .failure(error): + continuation.resume(throwing: Abort(.init(statusCode: 500), reason: error.localizedDescription)) + + case let .success(subscriptions): + let subscriptions = subscriptions.map { $0.node } + let response = Array(Set(subscriptions)) + + Encoder.encode(model: response, continuation: continuation) + } + } + }) + } +} diff --git a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/Server/DVPNServer.swift b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/Server/DVPNServer.swift index 82730dc..457a4a0 100644 --- a/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/Server/DVPNServer.swift +++ b/SolardVPNCommunityCoreiOS/SOLARdVPNCommunityCoreiOS/Root/Server/DVPNServer.swift @@ -29,6 +29,7 @@ extension DVPNServer { try api.register(collection: NodesRouteCollection(context: context)) try api.register(collection: WalletRouteCollection(context: context)) try api.register(collection: DNSRouteCollection(context: context)) + try api.register(collection: SubscriptionsRouteCollection(context: context)) try api.register( collection: TunnelRouteCollection(