Skip to content

Commit aaa6781

Browse files
committed
Add support for DELETE via application/json and application/x-www-form-urlencoded
1 parent 90fc3d1 commit aaa6781

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

official-http/csharp/BitMEXAPI.cs

+23-3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ private string BuildQueryData(Dictionary<string, string> param)
4747
catch (Exception) { return ""; }
4848
}
4949

50+
private string BuildJSON(Dictionary<string, string> param)
51+
{
52+
if (param == null)
53+
return "";
54+
55+
var entries = new List<string>();
56+
foreach (var item in param)
57+
entries.Add(string.Format("\"{0}\":\"{1}\"", item.Key, item.Value));
58+
59+
return "{" + string.Join(",", entries) + "}";
60+
}
61+
5062
public static string ByteArrayToString(byte[] ba)
5163
{
5264
StringBuilder hex = new StringBuilder(ba.Length * 2);
@@ -61,9 +73,9 @@ private long GetNonce()
6173
return DateTime.UtcNow.Ticks - yearBegin.Ticks;
6274
}
6375

64-
private string Query(string method, string function, Dictionary<string, string> param = null, bool auth = false)
76+
private string Query(string method, string function, Dictionary<string, string> param = null, bool auth = false, bool json = false)
6577
{
66-
string paramData = BuildQueryData(param);
78+
string paramData = json ? BuildJSON(param) : BuildQueryData(param);
6779
string url = "/api/v1" + function + ((method == "GET" && paramData != "") ? "?" + paramData : "");
6880
string postData = (method != "GET") ? paramData : "";
6981

@@ -86,7 +98,7 @@ private string Query(string method, string function, Dictionary<string, string>
8698
{
8799
if (postData != "")
88100
{
89-
webRequest.ContentType = "application/x-www-form-urlencoded";
101+
webRequest.ContentType = json ? "application/json" : "application/x-www-form-urlencoded";
90102
var data = Encoding.UTF8.GetBytes(postData);
91103
using (var stream = webRequest.GetRequestStream())
92104
{
@@ -152,6 +164,14 @@ public string PostOrders()
152164
return Query("POST", "/order", param, true);
153165
}
154166

167+
public string DeleteOrders()
168+
{
169+
var param = new Dictionary<string, string>();
170+
param["orderID"] = "de709f12-2f24-9a36-b047-ab0ff090f0bb";
171+
param["text"] = "cancel order by ID";
172+
return Query("DELETE", "/order", param, true, true);
173+
}
174+
155175
private byte[] hmacsha256(byte[] keyByte, byte[] messageBytes)
156176
{
157177
using (var hash = new HMACSHA256(keyByte))

official-http/csharp/Program.cs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ private void Run(string[] args)
1919
BitMEXApi bitmex = new BitMEXApi(bitmexKey, bitmexSecret);
2020
// var orderBook = bitmex.GetOrderBook("XBTUSD", 3);
2121
var orders = bitmex.GetOrders();
22+
// var orders = bitmex.DeleteOrders();
2223
Console.WriteLine(orders);
2324
}
2425
}

0 commit comments

Comments
 (0)