Skip to content

Commit

Permalink
#6: Add POST nodes by address request, add Encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
VictoriaKostyleva committed Oct 3, 2022
1 parent 0b98c3b commit bb93a3b
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// PostNodesByAddressRequest.swift
// SOLARAPI
//
// Created by Viktoriia Kostyleva on 30.09.2022.
//

import Foundation

public struct PostNodesByAddressRequest: Codable {
let addresses: [String]
let page: Int?

public init(addresses: [String], page: Int?) {
self.addresses = addresses
self.page = page
}
}

// MARK: - Codable implementation

public extension PostNodesByAddressRequest {
enum CodingKeys: String, CodingKey {
case addresses = "blockchain_addresses"
case page
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,34 @@ import Alamofire

enum NodesAPITarget {
case getNodes(GetNodesRequest)
case postNodesByAddress(PostNodesByAddressRequest)
}

extension NodesAPITarget: APITarget {
var method: HTTPMethod {
switch self {
case .getNodes:
return .get
case .postNodesByAddress:
return .post
}
}

var path: String {
switch self {
case .getNodes:
return "dvpn/getNodes"
case .postNodesByAddress:
return "dvpn/postNodesByAddress"
}
}

var parameters: Parameters {
switch self {
case let .getNodes(request):
return .requestParameters(parameters: request.dictionary ?? [:], encoding: URLEncoding.default)
case let .postNodesByAddress(request):
return .requestJSONEncodable(request)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ extension NodesProvider: NodesProviderType {
.validate()
.responseDecodable(completionHandler: getResponseHandler(completion: completion))
}

public func postNodesByAddress(
_ postNodesRequest: PostNodesByAddressRequest,
completion: @escaping (Result<PageResponse<Node>, NetworkError>) -> Void
) {
AF
.request(request(for: .postNodesByAddress(postNodesRequest)))
.validate()
.responseDecodable(completionHandler: getResponseHandler(completion: completion))
}
}

private extension NodesProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ public protocol NodesProviderType {
_ getNodesRequest: GetNodesRequest,
completion: @escaping (Result<PageResponse<Node>, NetworkError>) -> Void
)

func postNodesByAddress(
_ postNodesRequest: PostNodesByAddressRequest,
completion: @escaping (Result<PageResponse<Node>, NetworkError>) -> Void
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
923C371628E5B9DE003CFC03 /* ContextBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 923C371528E5B9DE003CFC03 /* ContextBuilder.swift */; };
923C371828E5D20D003CFC03 /* GetNodesRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 923C371728E5D20D003CFC03 /* GetNodesRequest.swift */; };
923C371C28E5DA05003CFC03 /* OrderType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 923C371B28E5DA05003CFC03 /* OrderType.swift */; };
923C372028E71D19003CFC03 /* PostNodesByAddressRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 923C371F28E71D19003CFC03 /* PostNodesByAddressRequest.swift */; };
923C372228E72C87003CFC03 /* NodesByAddressPostBody.swift in Sources */ = {isa = PBXBuildFile; fileRef = 923C372128E72C87003CFC03 /* NodesByAddressPostBody.swift */; };
923C372628EAE93E003CFC03 /* Encoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 923C372528EAE93E003CFC03 /* Encoder.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 */; };
Expand Down Expand Up @@ -174,6 +177,9 @@
923C371528E5B9DE003CFC03 /* ContextBuilder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContextBuilder.swift; sourceTree = "<group>"; };
923C371728E5D20D003CFC03 /* GetNodesRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetNodesRequest.swift; sourceTree = "<group>"; };
923C371B28E5DA05003CFC03 /* OrderType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OrderType.swift; sourceTree = "<group>"; };
923C371F28E71D19003CFC03 /* PostNodesByAddressRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostNodesByAddressRequest.swift; sourceTree = "<group>"; };
923C372128E72C87003CFC03 /* NodesByAddressPostBody.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NodesByAddressPostBody.swift; sourceTree = "<group>"; };
923C372528EAE93E003CFC03 /* Encoder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Encoder.swift; sourceTree = "<group>"; };
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 = "<group>"; };
92D6B3FE28E19E20004CF9DF /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -366,9 +372,9 @@
22C3EF0828E6FEBA007DB01B /* Root */ = {
isa = PBXGroup;
children = (
923C372328E72FDA003CFC03 /* RouteCollections */,
92D6B3FC28E19E20004CF9DF /* AppDelegate.swift */,
92D6B41328E1E133004CF9DF /* DVPNServer.swift */,
92D6B41728E2DC85004CF9DF /* NodesRouteCollection.swift */,
92D6B3FE28E19E20004CF9DF /* SceneDelegate.swift */,
92D6B40028E19E20004CF9DF /* ViewController.swift */,
);
Expand Down Expand Up @@ -402,6 +408,23 @@
path = Context;
sourceTree = "<group>";
};
923C372328E72FDA003CFC03 /* RouteCollections */ = {
isa = PBXGroup;
children = (
923C372428E72FF0003CFC03 /* Nodes */,
);
path = RouteCollections;
sourceTree = "<group>";
};
923C372428E72FF0003CFC03 /* Nodes */ = {
isa = PBXGroup;
children = (
923C372128E72C87003CFC03 /* NodesByAddressPostBody.swift */,
92D6B41728E2DC85004CF9DF /* NodesRouteCollection.swift */,
);
path = Nodes;
sourceTree = "<group>";
};
92D6B3F028E19E20004CF9DF = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -454,6 +477,7 @@
children = (
22C3EEFA28E48F94007DB01B /* NotificationToken.swift */,
92D6B41D28E2F64D004CF9DF /* ClientConstants.swift */,
923C372528EAE93E003CFC03 /* Encoder.swift */,
);
path = Utilities;
sourceTree = "<group>";
Expand Down Expand Up @@ -509,6 +533,7 @@
92D6B46228E47240004CF9DF /* Node.swift */,
92D6B46028E463EF004CF9DF /* NodeStatusType.swift */,
92D6B45E28E4639D004CF9DF /* OrderType.swift */,
923C371F28E71D19003CFC03 /* PostNodesByAddressRequest.swift */,
);
path = Models;
sourceTree = "<group>";
Expand Down Expand Up @@ -760,6 +785,7 @@
files = (
92D6B40128E19E20004CF9DF /* ViewController.swift in Sources */,
22C3EEED28E48D9A007DB01B /* SentinelNode+Ext.swift in Sources */,
923C372628EAE93E003CFC03 /* Encoder.swift in Sources */,
92D6B41828E2DC85004CF9DF /* NodesRouteCollection.swift in Sources */,
923C371428E5B898003CFC03 /* CommonContext.swift in Sources */,
92D6B3FD28E19E20004CF9DF /* AppDelegate.swift in Sources */,
Expand Down Expand Up @@ -787,6 +813,7 @@
92D6B47428E48F64004CF9DF /* Continent.swift in Sources */,
92D6B47128E47E71004CF9DF /* NodesServiceType.swift in Sources */,
22C3EEA828E4733C007DB01B /* Keychain.swift in Sources */,
923C372228E72C87003CFC03 /* NodesByAddressPostBody.swift in Sources */,
22C3EECE28E48B52007DB01B /* PeersModel.swift in Sources */,
92D6B41E28E2F64D004CF9DF /* ClientConstants.swift in Sources */,
923C371628E5B9DE003CFC03 /* ContextBuilder.swift in Sources */,
Expand Down Expand Up @@ -817,6 +844,7 @@
92D6B44628E34D7C004CF9DF /* APIRequest.swift in Sources */,
92D6B42828E32900004CF9DF /* SOLARAPI.docc in Sources */,
92D6B45A28E46283004CF9DF /* NetworkError.swift in Sources */,
923C372028E71D19003CFC03 /* PostNodesByAddressRequest.swift in Sources */,
92D6B46C28E47519004CF9DF /* Encodable+Ext.swift in Sources */,
92D6B45628E4609E004CF9DF /* APITarget.swift in Sources */,
92D6B45828E46118004CF9DF /* SOLARAPIProvider.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// Encoder.swift
// SOLARdVPNCommunityCoreiOS
//
// Created by Viktoriia Kostyleva on 03.10.2022.
//

import Foundation

enum EncoderError: LocalizedError {
case failToEncode

var errorDescription: String? {
switch self {
case .failToEncode:
return "Fail to encode"
}
}
}

enum Encoder {
static func encode(
model: Codable,
continuation: CheckedContinuation<String, Error>
) {
do {
let result = try JSONEncoder().encode(model)
let string = String(decoding: result, as: UTF8.self)
continuation.resume(returning: string)
} catch {
continuation.resume(throwing: EncoderError.failToEncode)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// NodesByAddressPostBody.swift
// SOLARdVPNCommunityCoreiOS
//
// Created by Viktoriia Kostyleva on 30.09.2022.
//

import Vapor

struct NodesByAddressPostBody: Content {
let blockchain_addresses: [String]
let page: Int?
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct NodesRouteCollection: RouteCollection {

func boot(routes: RoutesBuilder) throws {
routes.get("nodes", use: getNodes)
routes.post("nodesByAddress", use: postNodesByAddress)
}
}

Expand All @@ -35,4 +36,13 @@ extension NodesRouteCollection {
page: page
)
}

func postNodesByAddress(_ req: Request) async throws -> String {
let body = try req.content.decode(NodesByAddressPostBody.self)

return try await context.nodesService.getNodes(
by: body.blockchain_addresses,
page: body.page
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,24 @@ extension NodesService: NodesServiceType {
)
) { result in
switch result {
case let .success(success):
let result = try! JSONEncoder().encode(success)
let string = String(decoding: result, as: UTF8.self)
continuation.resume(returning: string)

case let .success(response):
Encoder.encode(model: response, continuation: continuation)
case let .failure(error):
continuation.resume(throwing: Abort(.init(statusCode: error.code), reason: error.localizedDescription))
}
}
})
}

func getNodes(
by addresses: [String],
page: Int?
) async throws -> String {
try await withCheckedThrowingContinuation({ (continuation: CheckedContinuation<String, Error>) in
nodesProvider.postNodesByAddress(.init(addresses: addresses, page: page)) { result in
switch result {
case let .success(response):
Encoder.encode(model: response, continuation: continuation)
case let .failure(error):
continuation.resume(throwing: Abort(.init(statusCode: error.code), reason: error.localizedDescription))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ protocol NodesServiceType {
query: String?,
page: Int?
) async throws -> String

func getNodes(
by: [String],
page: Int?
) async throws -> String
}

0 comments on commit bb93a3b

Please sign in to comment.