Skip to content

Commit 7fa8e32

Browse files
author
Fernando Fernandes
committed
Fix place order response
- Read environment from Xcode variable.
1 parent 49f176c commit 7fa8e32

File tree

9 files changed

+60
-15
lines changed

9 files changed

+60
-15
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// Environment.swift
3+
//
4+
//
5+
// Created by Fernando Fernandes on 09.02.22.
6+
//
7+
8+
import Foundation
9+
10+
/// Defines the target exchange environment, such as `production` or `sandbox`.
11+
public enum Environment: String {
12+
case production
13+
case sandbox
14+
}
15+
16+
// MARK: - Interface
17+
18+
public extension Environment {
19+
20+
/// An `ENVIRONMENT` variable can be set in the run schema in Xcode to easily switch between them.
21+
/// This functions returns it or `nil` if it's absent.
22+
static func environmentFromXcode() -> Environment? {
23+
#if os(macOS) || os(iOS)
24+
guard let environmentValue = ProcessInfo.processInfo.environment["ENVIRONMENT"],
25+
let environment = Environment(rawValue: environmentValue) else {
26+
return nil
27+
}
28+
return environment
29+
#else
30+
return nil
31+
#endif
32+
}
33+
}

Sources/SwiftTrader/Model/Kucoin/Order/KucoinFuturesPlaceOrder.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ import Foundation
1212
/// https://docs.kucoin.com/futures/#place-an-order
1313
public struct KucoinFuturesPlaceOrder: Codable {
1414
public let code: String
15-
public let data: KucoinFuturesOrderPlaced
16-
}
17-
18-
public struct KucoinFuturesOrderPlaced: Codable {
19-
public let orderId: String
15+
public let msg: String?
16+
public let orderId: String?
2017
}

Sources/SwiftTrader/Network/Kucoin/AccountOverview/KucoinFuturesAccountOverviewResource.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public struct KucoinFuturesAccountOverviewResource: NetworkResource {
1616

1717
public var url: URL {
1818
get throws {
19-
let baseURLString = KucoinAPI.Futures.URL.base
19+
let baseURLString = KucoinAPI.Futures.baseURL()
2020
guard var urlComponents = URLComponents(string: baseURLString) else {
2121
throw NetworkRequestError.invalidURLString(urlString: baseURLString)
2222
}

Sources/SwiftTrader/Network/Kucoin/KucoinAPI.swift

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,30 @@ import Foundation
1111
public struct KucoinAPI {
1212

1313
public struct Futures {
14-
14+
1515
public struct Path {
1616
static let accountOverview = "/api/v1/account-overview"
1717
static let orders = "/api/v1/orders"
1818
static let positions = "/api/v1/positions"
1919
}
2020

21-
public struct URL {
22-
static let base = "https://api-futures.kucoin.com"
23-
static let sandbox = "https://api-sandbox-futures.kucoin.com"
21+
/// Returns the base `URL` based on an Xcode environment variable.
22+
///
23+
/// In case the above fails/is absent, returns the `production` base `URL`.
24+
public static func baseURL() -> String {
25+
let production = "https://api-futures.kucoin.com"
26+
let sandbox = "https://api-sandbox-futures.kucoin.com"
27+
if let environment = Environment.environmentFromXcode() {
28+
switch environment {
29+
case .production:
30+
return production
31+
case .sandbox:
32+
return sandbox
33+
}
34+
} else {
35+
// Fallback to production.
36+
return production
37+
}
2438
}
2539
}
2640

Sources/SwiftTrader/Network/Kucoin/Orders/KucoinFuturesOrderListResource.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public struct KucoinFuturesOrderListResource: NetworkResource {
1616

1717
public var url: URL {
1818
get throws {
19-
let baseURLString = KucoinAPI.Futures.URL.base
19+
let baseURLString = KucoinAPI.Futures.baseURL()
2020
guard var urlComponents = URLComponents(string: baseURLString) else {
2121
throw NetworkRequestError.invalidURLString(urlString: baseURLString)
2222
}

Sources/SwiftTrader/Network/Kucoin/Orders/KucoinFuturesPlaceOrderRequest.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public struct KucoinFuturesPlaceOrdersRequest: NetworkRequest {
3434

3535
#warning("TODO: This has to be parameterized")
3636
var json = [String:Any]()
37-
json["symbol"] = "XBTUSDM"
37+
json["clientOid"] = UUID().uuidString
38+
json["symbol"] = "XBTUSDTM"
3839
json["price"] = "42000"
3940
json["closeOrder"] = true
4041

Sources/SwiftTrader/Network/Kucoin/Orders/KucoinFuturesPlaceOrderResource.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public struct KucoinFuturesPlaceOrderResource: NetworkResource {
1616

1717
public var url: URL {
1818
get throws {
19-
let baseURLString = KucoinAPI.Futures.URL.base
19+
let baseURLString = KucoinAPI.Futures.baseURL()
2020
guard var urlComponents = URLComponents(string: baseURLString) else {
2121
throw NetworkRequestError.invalidURLString(urlString: baseURLString)
2222
}

Sources/SwiftTrader/Network/Kucoin/Positions/KucoinFuturesPositionListResource.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public struct KucoinFuturesPositionListResource: NetworkResource {
1616

1717
public var url: URL {
1818
get throws {
19-
let baseURLString = KucoinAPI.Futures.URL.base
19+
let baseURLString = KucoinAPI.Futures.baseURL()
2020
guard var urlComponents = URLComponents(string: baseURLString) else {
2121
throw NetworkRequestError.invalidURLString(urlString: baseURLString)
2222
}

Sources/SwiftTrader/Network/NetworkRequest/NetworkRequestError.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public enum NetworkRequestError: Error {
2525
/// Could not instantiate a `URLComponents` struct using the given `String` URL.
2626
case invalidURLString(urlString: String)
2727

28-
/// The request failed; the `Error` parameter indicates why.
28+
/// The request has failed; the `Error` parameter indicates why.
2929
case requestFailed(error: Error)
3030

3131
/// The response status code is something other than `200`.

0 commit comments

Comments
 (0)