Skip to content

Commit

Permalink
#4: Add nodes service provider
Browse files Browse the repository at this point in the history
  • Loading branch information
lika-vorobeva committed Oct 3, 2022
1 parent 68efb28 commit 4f3ec6e
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// StartSessionRequest.swift
// SOLARAPI
//
// Created by Lika Vorobeva on 26.09.2022.
//

import Foundation

public struct StartSessionRequest: Codable {
public let key: String
public let signature: String

public init(key: String, signature: String) {
self.key = key
self.signature = signature
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// StartSessionResponse.swift
// SOLARAPI
//
// Created by Lika Vorobeva on 26.09.2022.
//

import Foundation

public struct StartSessionResponse: Codable {
public let success: Bool
public let result: String?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// NodeSessionAPITarget.swift
// SOLARAPI
//
// Created by Lika Vorobeva on 26.09.2022.
//

import Foundation
import Alamofire

enum NodeSessionAPITarget {
case createClient(address: String, id: String, request: StartSessionRequest)
}

extension NodeSessionAPITarget: APITarget {
var method: HTTPMethod {
switch self {
case .createClient:
return .post
}
}

var path: String {
switch self {
case let .createClient(address, id, _):
return "accounts/\(address)/sessions/\(id)"
}
}

var parameters: Parameters {
switch self {
case let .createClient(_, _, data):
return .requestJSONEncodable(data)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// NodeSessionProvider.swift
// SOLARAPI
//
// Created by Lika Vorobeva on 26.09.2022.
//

import Foundation
import Alamofire

public final class NodeSessionProvider: SOLARAPIProvider {
public init() {}
}

// MARK: - StealthProviderType implementation

extension NodeSessionProvider: NodeSessionProviderType {
public func createClient(
remoteURL: URL,
address: String,
id: String,
request: StartSessionRequest,
completion: @escaping (Result<StartSessionResponse, NetworkError>) -> Void
) {
let apiRequest = APIRequest(
baseURL: remoteURL,
target: NodeSessionAPITarget.createClient(address: address, id: id, request: request)
)

AF.request(apiRequest)
.validate()
.responseDecodable(completionHandler: getResponseHandler(mapsInnerErrors: true, completion: completion))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// NodeSessionProviderType.swift
// SOLARAPI
//
// Created by Lika Vorobeva on 26.09.2022.
//

import Foundation
import Alamofire

public protocol NodeSessionProviderType {
func createClient(
remoteURL: URL,
address: String,
id: String,
request: StartSessionRequest,
completion: @escaping (Result<StartSessionResponse, NetworkError>) -> Void
)
}

0 comments on commit 4f3ec6e

Please sign in to comment.