Skip to content

Commit

Permalink
#5: Add GET countries request
Browse files Browse the repository at this point in the history
  • Loading branch information
VictoriaKostyleva committed Oct 3, 2022
1 parent bb93a3b commit d45a3d9
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Country.swift
// SOLARAPI
//
// Created by Viktoriia Kostyleva on 03.10.2022.
//

import Foundation

public struct Country: Codable {
public let code: String
public let nodesCount: Int

public init(code: String, nodesCount: Int) {
self.code = code
self.nodesCount = nodesCount
}
}

// MARK: - Codable implementation

public extension Country {
enum CodingKeys: String, CodingKey {
case code
case nodesCount = "nodes_count"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Alamofire
enum NodesAPITarget {
case getNodes(GetNodesRequest)
case postNodesByAddress(PostNodesByAddressRequest)
case getCountries
}

extension NodesAPITarget: APITarget {
Expand All @@ -20,6 +21,8 @@ extension NodesAPITarget: APITarget {
return .get
case .postNodesByAddress:
return .post
case .getCountries:
return .get
}
}

Expand All @@ -29,6 +32,8 @@ extension NodesAPITarget: APITarget {
return "dvpn/getNodes"
case .postNodesByAddress:
return "dvpn/postNodesByAddress"
case .getCountries:
return "dvpn/getCountries"
}
}

Expand All @@ -38,6 +43,8 @@ extension NodesAPITarget: APITarget {
return .requestParameters(parameters: request.dictionary ?? [:], encoding: URLEncoding.default)
case let .postNodesByAddress(request):
return .requestJSONEncodable(request)
case .getCountries:
return .requestPlain
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ extension NodesProvider: NodesProviderType {
.validate()
.responseDecodable(completionHandler: getResponseHandler(completion: completion))
}

public func getCountries(completion: @escaping (Result<[Country], NetworkError>) -> Void) {
AF
.request(request(for: .getCountries))
.validate()
.responseDecodable(completionHandler: getResponseHandler(completion: completion))
}
}

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

func getCountries(completion: @escaping (Result<[Country], NetworkError>) -> Void)
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
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 */; };
923C372828EAF0E8003CFC03 /* Country.swift in Sources */ = {isa = PBXBuildFile; fileRef = 923C372728EAF0E8003CFC03 /* Country.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 @@ -180,6 +181,7 @@
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>"; };
923C372728EAF0E8003CFC03 /* Country.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Country.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 @@ -529,6 +531,7 @@
92D6B45B28E46350004CF9DF /* Models */ = {
isa = PBXGroup;
children = (
923C372728EAF0E8003CFC03 /* Country.swift */,
92D6B45C28E4636B004CF9DF /* GetNodesRequest.swift */,
92D6B46228E47240004CF9DF /* Node.swift */,
92D6B46028E463EF004CF9DF /* NodeStatusType.swift */,
Expand Down Expand Up @@ -841,6 +844,7 @@
92D6B46328E47240004CF9DF /* Node.swift in Sources */,
92D6B45F28E4639D004CF9DF /* OrderType.swift in Sources */,
92D6B45228E45F03004CF9DF /* NodesProvider.swift in Sources */,
923C372828EAF0E8003CFC03 /* Country.swift in Sources */,
92D6B44628E34D7C004CF9DF /* APIRequest.swift in Sources */,
92D6B42828E32900004CF9DF /* SOLARAPI.docc in Sources */,
92D6B45A28E46283004CF9DF /* NetworkError.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ struct NodesRouteCollection: RouteCollection {
let context: HasNodesService

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

Expand Down Expand Up @@ -45,4 +46,8 @@ extension NodesRouteCollection {
page: body.page
)
}

func getCountries(_ req: Request) async throws -> String {
try await context.nodesService.getCountries()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,17 @@ extension NodesService: NodesServiceType {
}
})
}

func getCountries() async throws -> String {
try await withCheckedThrowingContinuation({ (continuation: CheckedContinuation<String, Error>) in
nodesProvider.getCountries() { 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))
}
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ protocol NodesServiceType {
by: [String],
page: Int?
) async throws -> String

func getCountries() async throws -> String
}

0 comments on commit d45a3d9

Please sign in to comment.