Skip to content

Commit 21ac4f8

Browse files
committed
Add support to Kucoin's "Get an Account" (spot)
1 parent 1344e51 commit 21ac4f8

13 files changed

+237
-53
lines changed

Sources/SwiftTrader/Model/Error/SwiftTraderError+Details.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public extension SwiftTraderError {
2525
isSuccess: ftxError.isSuccess,
2626
errorMessage: ftxError.errorMessage
2727
)
28-
case .kucoinAccounts,
28+
case .kucoinSpotListAccounts,
29+
.kucoinSpotGetAccount,
2930
.kucoinFuturesAccountOverview,
3031
.kucoinFuturesCancelStopOrders,
3132
.kucoinFuturesOrderList,

Sources/SwiftTrader/Model/Error/SwiftTraderError.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,11 @@ public enum SwiftTraderError: Error {
8080

8181
// MARK: Accounts
8282

83-
/// And error ocurred while executing the function `SwiftTrader.kucoinAccounts(currencySymbol:)`.
84-
case kucoinAccounts(error: Error)
83+
/// And error ocurred while executing the function `SwiftTrader.kucoinListAccounts(currencySymbol:)`.
84+
case kucoinSpotListAccounts(error: Error)
85+
86+
/// And error ocurred while executing the function `SwiftTrader.kucoinGetAccount(accountID:)`.
87+
case kucoinSpotGetAccount(error: Error)
8588

8689
// MARK: Account Overview
8790

Sources/SwiftTrader/Model/Kucoin/Responses/KucoinAccount.swift

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// KucoinAccounts.swift
3+
//
4+
//
5+
// Created by Fernando Fernandes on 16.07.22.
6+
//
7+
8+
import Foundation
9+
10+
/// Kucoin "List Acounts" REST API response.
11+
///
12+
/// https://docs.kucoin.com/#list-accounts
13+
public struct KucoinSpotListAccounts: Codable {
14+
let code: String
15+
let data: [KucoinFuturesAccount]
16+
}
17+
18+
/// Encapsulates Kucoin futures account data.
19+
public struct KucoinFuturesAccount: Codable {
20+
let id, currency, balance: String
21+
let type: KucoinAccountType
22+
let available, holds: String
23+
}
24+
25+
/// Kucoin "Get Acount" REST API response.
26+
///
27+
/// https://docs.kucoin.com/#get-an-account
28+
public struct KucoinSpotGetAccount: Codable {
29+
let code: String
30+
let data: KucoinSpotAccount
31+
}
32+
33+
/// Encapsulates Kucoin spot account data.
34+
public struct KucoinSpotAccount: Codable {
35+
let currency, balance: String
36+
let available, holds: String
37+
}
38+
39+
/// There are three types of accounts: 1) main account 2) trade account 3) margin account.
40+
public enum KucoinAccountType: String, Codable {
41+
case main
42+
case trade
43+
case margin
44+
}

Sources/SwiftTrader/Model/SwiftTraderOperation.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public enum SwiftTraderOperation {
2121

2222
// MARK: Spot
2323

24-
case kucoinAccounts
24+
case kucoinSpotListAccounts
25+
case kucoinSpotGetAccount
2526

2627
// MARK: Futures
2728

Sources/SwiftTrader/Network/Kucoin/API/Kucoin/KucoinAPI+Spot.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,22 @@ public extension KucoinAPI {
1212
struct Spot {
1313

1414
public struct Path {
15-
static let accounts = "/api/v1/accounts"
15+
16+
/// Returns the path for getting accounts within Kucoin spot.
17+
///
18+
/// `pathComponent` required as `URL.appendingPathComponent(_:)` will be deprecated and
19+
/// `URL.appending(path:directoryHint:)` is only available in macOS 13 and later (so not in Linux / Heroku).
20+
///
21+
/// - Parameter pathComponent: use this parameter to append extra info to the returned
22+
/// `String`, e.g.: "/api/v1/accounts/5bd6e9286d99522a52e458de"
23+
/// - Returns: `/api/v1/accounts` or `/api/v1/accounts/pathComponent`
24+
static func accounts(pathComponent: String? = nil) -> String {
25+
if let path = pathComponent {
26+
return "/api/v1/accounts" + "/" + path
27+
} else {
28+
return "/api/v1/accounts"
29+
}
30+
}
1631
}
1732

1833
/// Returns the base `URL` based on an Xcode environment variable.

Sources/SwiftTrader/Network/Kucoin/API/Kucoin/KucoinAPI.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public struct KucoinAPI {
1818
}
1919

2020
public struct QueryParam {
21+
public static let accountId = "accountId"
2122
public static let currency = "currency"
2223
public static let orderStatus = "status"
2324
public static let symbol = "symbol"
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//
2+
// KucoinGetAccountRequest.swift
3+
//
4+
//
5+
// Created by Fernando Fernandes on 30.07.22.
6+
//
7+
8+
import Foundation
9+
10+
#if canImport(FoundationNetworking)
11+
import FoundationNetworking
12+
#endif
13+
import Logging
14+
15+
/// A **request** for getting a Kucoin account.
16+
///
17+
/// https://docs.kucoin.com/#get-an-account
18+
public struct KucoinGetAccountRequest: NetworkRequest {
19+
20+
// MARK: - Properties
21+
22+
public typealias DecodableModel = KucoinSpotGetAccount
23+
24+
public var logger: Logger {
25+
NetworkRequestLogger().default
26+
}
27+
28+
public var session: URLSession
29+
30+
public var request: URLRequest {
31+
get throws {
32+
let accountResource = KucoinGetAccountResource(accountID: accountID)
33+
var urlRequest = URLRequest(url: try accountResource.url)
34+
urlRequest.httpMethod = HTTPMethod.GET.rawValue
35+
try KucoinAPI.setRequestHeaderFields(request: &urlRequest, kucoinAuth: kucoinAuth.spot)
36+
return urlRequest
37+
}
38+
}
39+
40+
public var settings: NetworkRequestSettings
41+
42+
// MARK: Private
43+
44+
private let accountID: String
45+
46+
private let kucoinAuth: KucoinAuth
47+
48+
// MARK: - Lifecycle
49+
50+
/// Creates a new `KucoinListAccountsRequest` instance.
51+
///
52+
/// - Parameters:
53+
/// - accountID: `String`, the ID of the account.
54+
/// - kucoinAuth: Kucoin authentication data.
55+
/// - session: `URLSession`, default is `.shared`.
56+
/// - settings: `NetworkRequestSettings`.
57+
public init(accountID: String,
58+
kucoinAuth: KucoinAuth,
59+
session: URLSession = .shared,
60+
settings: NetworkRequestSettings) {
61+
self.accountID = accountID
62+
self.kucoinAuth = kucoinAuth
63+
self.session = session
64+
self.settings = settings
65+
}
66+
}
67+
68+
// MARK: - Network Request Protocol
69+
70+
public extension KucoinGetAccountRequest {
71+
72+
func decode(_ data: Data) throws -> DecodableModel {
73+
try JSONDecoder().decode(KucoinSpotGetAccount.self, from: data)
74+
}
75+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// KucoinGetAccountResource.swift
3+
//
4+
//
5+
// Created by Fernando Fernandes on 16.07.22.
6+
//
7+
8+
import Foundation
9+
10+
/// The **resource** for getting a Kucoin account.
11+
///
12+
/// https://docs.kucoin.com/#get-an-account
13+
public struct KucoinGetAccountResource: NetworkResource {
14+
15+
// MARK: - Properties
16+
17+
public var url: URL {
18+
get throws {
19+
let baseURLString = try KucoinAPI.Spot.baseURL()
20+
guard var urlComponents = URLComponents(string: baseURLString) else {
21+
throw NetworkRequestError.invalidURLString(urlString: baseURLString)
22+
}
23+
urlComponents.path = KucoinAPI.Spot.Path.accounts(pathComponent: accountID)
24+
guard let url = urlComponents.url else {
25+
throw NetworkRequestError.invalidURLString(urlString: baseURLString)
26+
}
27+
return url
28+
}
29+
}
30+
31+
// MARK: Private
32+
33+
private let accountID: String
34+
35+
// MARK: - Lifecycle
36+
37+
/// Creates a new `KucoinGetAccountResource` instance.
38+
///
39+
/// - Parameter accountID: `String`.
40+
init(accountID: String) {
41+
self.accountID = accountID
42+
}
43+
}

Sources/SwiftTrader/Network/Kucoin/Accounts/KucoinAccountsRequest.swift renamed to Sources/SwiftTrader/Network/Kucoin/Accounts/KucoinListAccountsRequest.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// KucoinAccountsRequest.swift
2+
// KucoinListAccountsRequest.swift
33
//
44
//
55
// Created by Fernando Fernandes on 16.07.22.
@@ -15,11 +15,11 @@ import Logging
1515
/// A **request** for listing Kucoin accounts.
1616
///
1717
/// https://docs.kucoin.com/#list-accounts
18-
public struct KucoinAccountsRequest: NetworkRequest {
18+
public struct KucoinListAccountsRequest: NetworkRequest {
1919

2020
// MARK: - Properties
2121

22-
public typealias DecodableModel = KucoinAccounts
22+
public typealias DecodableModel = KucoinSpotListAccounts
2323

2424
public var logger: Logger {
2525
NetworkRequestLogger().default
@@ -29,7 +29,7 @@ public struct KucoinAccountsRequest: NetworkRequest {
2929

3030
public var request: URLRequest {
3131
get throws {
32-
let accountsResource = KucoinAccountsResource(currencySymbol: currencySymbol)
32+
let accountsResource = KucoinListAccountsResource(currencySymbol: currencySymbol)
3333
var urlRequest = URLRequest(url: try accountsResource.url)
3434
urlRequest.httpMethod = HTTPMethod.GET.rawValue
3535
try KucoinAPI.setRequestHeaderFields(request: &urlRequest, kucoinAuth: kucoinAuth.spot)
@@ -47,7 +47,7 @@ public struct KucoinAccountsRequest: NetworkRequest {
4747

4848
// MARK: - Lifecycle
4949

50-
/// Creates a new `KucoinAccountsRequest` instance.
50+
/// Creates a new `KucoinListAccountsRequest` instance.
5151
///
5252
/// - Parameters:
5353
/// - currencySymbol: `CurrencySymbol`, default is `.USDT`.
@@ -67,9 +67,9 @@ public struct KucoinAccountsRequest: NetworkRequest {
6767

6868
// MARK: - Network Request Protocol
6969

70-
public extension KucoinAccountsRequest {
70+
public extension KucoinListAccountsRequest {
7171

7272
func decode(_ data: Data) throws -> DecodableModel {
73-
try JSONDecoder().decode(KucoinAccounts.self, from: data)
73+
try JSONDecoder().decode(KucoinSpotListAccounts.self, from: data)
7474
}
7575
}

0 commit comments

Comments
 (0)