Skip to content

Commit 208a487

Browse files
Johnny A. dos Santosvslee
authored andcommitted
Add buy/sell/cancel to BL3P (#475)
* Starting to handle requests * Start add/remove orders implementation * Implementation of proxies (unix like) to help debugging * Add commands to buy/sell in console app * Finished buy/sell implementation * Fixes wait option * Undo use of system.range * Add call to cancel order and console option * Fix json parsing * Small fixes from PR review * Fix json error and access modifiers * Fix ws-orderbook for bl3p * Add same proxy to WS * Fix BL3P tests
1 parent ffb57e7 commit 208a487

39 files changed

+1066
-169
lines changed

ExchangeSharp/API/Common/APIRequestMaker.cs

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
MIT LICENSE
33
44
Copyright 2017 Digital Ruby, LLC - http://www.digitalruby.com
@@ -14,7 +14,6 @@ The above copyright notice and this permission notice shall be included in all c
1414
using System.Collections.Generic;
1515
using System.IO;
1616
using System.Net;
17-
using System.Text;
1817
using System.Threading.Tasks;
1918

2019
namespace ExchangeSharp
@@ -27,79 +26,93 @@ public sealed class APIRequestMaker : IAPIRequestMaker
2726
{
2827
private readonly IAPIRequestHandler api;
2928

30-
private class InternalHttpWebRequest : IHttpWebRequest
29+
internal class InternalHttpWebRequest : IHttpWebRequest
3130
{
32-
internal readonly HttpWebRequest request;
31+
internal readonly HttpWebRequest Request;
32+
internal static WebProxy Proxy;
33+
34+
static InternalHttpWebRequest()
35+
{
36+
var httpProxy = Environment.GetEnvironmentVariable("http_proxy");
37+
httpProxy ??= Environment.GetEnvironmentVariable("HTTP_PROXY");
38+
39+
if (string.IsNullOrEmpty(httpProxy))
40+
return;
41+
42+
var uri = new Uri(httpProxy);
43+
Proxy = new WebProxy(uri);
44+
}
3345

3446
public InternalHttpWebRequest(Uri fullUri)
3547
{
36-
request = (HttpWebRequest.Create(fullUri) as HttpWebRequest ?? throw new NullReferenceException("Failed to create HttpWebRequest"));
37-
request.KeepAlive = false;
48+
Request = (HttpWebRequest.Create(fullUri) as HttpWebRequest ?? throw new NullReferenceException("Failed to create HttpWebRequest"));
49+
Request.Proxy = Proxy;
50+
Request.KeepAlive = false;
3851
}
3952

4053
public void AddHeader(string header, string value)
4154
{
4255
switch (header.ToStringLowerInvariant())
4356
{
4457
case "content-type":
45-
request.ContentType = value;
58+
Request.ContentType = value;
4659
break;
4760

4861
case "content-length":
49-
request.ContentLength = value.ConvertInvariant<long>();
62+
Request.ContentLength = value.ConvertInvariant<long>();
5063
break;
5164

5265
case "user-agent":
53-
request.UserAgent = value;
66+
Request.UserAgent = value;
5467
break;
5568

5669
case "accept":
57-
request.Accept = value;
70+
Request.Accept = value;
5871
break;
5972

6073
case "connection":
61-
request.Connection = value;
74+
Request.Connection = value;
6275
break;
6376

6477
default:
65-
request.Headers[header] = value;
78+
Request.Headers[header] = value;
6679
break;
6780
}
6881
}
6982

7083
public Uri RequestUri
7184
{
72-
get { return request.RequestUri; }
85+
get { return Request.RequestUri; }
7386
}
7487

7588
public string Method
7689
{
77-
get { return request.Method; }
78-
set { request.Method = value; }
90+
get { return Request.Method; }
91+
set { Request.Method = value; }
7992
}
8093

8194
public int Timeout
8295
{
83-
get { return request.Timeout; }
84-
set { request.Timeout = value; }
96+
get { return Request.Timeout; }
97+
set { Request.Timeout = value; }
8598
}
8699

87100
public int ReadWriteTimeout
88101
{
89-
get { return request.ReadWriteTimeout; }
90-
set { request.ReadWriteTimeout = value; }
102+
get { return Request.ReadWriteTimeout; }
103+
set { Request.ReadWriteTimeout = value; }
91104
}
92105

93106
public async Task WriteAllAsync(byte[] data, int index, int length)
94107
{
95-
using (Stream stream = await request.GetRequestStreamAsync())
108+
using (Stream stream = await Request.GetRequestStreamAsync())
96109
{
97110
await stream.WriteAsync(data, 0, data.Length);
98111
}
99112
}
100113
}
101114

102-
private class InternalHttpWebResponse : IHttpWebResponse
115+
internal class InternalHttpWebResponse : IHttpWebResponse
103116
{
104117
private readonly HttpWebResponse response;
105118

@@ -175,7 +188,7 @@ public async Task<string> MakeRequestAsync(string url, string? baseUrl = null, D
175188
try
176189
{
177190
RequestStateChanged?.Invoke(this, RequestMakerState.Begin, uri.AbsoluteUri);// when start make a request we send the uri, this helps developers to track the http requests.
178-
response = await request.request.GetResponseAsync() as HttpWebResponse;
191+
response = await request.Request.GetResponseAsync() as HttpWebResponse;
179192
if (response == null)
180193
{
181194
throw new APIException("Unknown response from server");
@@ -191,16 +204,19 @@ public async Task<string> MakeRequestAsync(string url, string? baseUrl = null, D
191204
}
192205
using (Stream responseStream = response.GetResponseStream())
193206
using (StreamReader responseStreamReader = new StreamReader(responseStream))
194-
responseString = responseStreamReader.ReadToEnd();
207+
responseString = responseStreamReader.ReadToEnd();
208+
195209
if (response.StatusCode != HttpStatusCode.OK)
196210
{
197-
// 404 maybe return empty responseString
198-
if (string.IsNullOrWhiteSpace(responseString))
199-
{
211+
// 404 maybe return empty responseString
212+
if (string.IsNullOrWhiteSpace(responseString))
213+
{
200214
throw new APIException(string.Format("{0} - {1}", response.StatusCode.ConvertInvariant<int>(), response.StatusCode));
201-
}
202-
throw new APIException(responseString);
215+
}
216+
217+
throw new APIException(responseString);
203218
}
219+
204220
api.ProcessResponse(new InternalHttpWebResponse(response));
205221
RequestStateChanged?.Invoke(this, RequestMakerState.Finished, responseString);
206222
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Runtime.Serialization;
3+
using ExchangeSharp.API.Exchanges.BL3P.Models;
4+
5+
namespace ExchangeSharp.API.Exchanges.BL3P
6+
{
7+
[Serializable]
8+
internal class BL3PException : Exception
9+
{
10+
public string ErrorCode { get; }
11+
12+
internal BL3PException(BL3PResponsePayloadError error)
13+
: this(error?.Message)
14+
{
15+
if (error == null)
16+
throw new ArgumentNullException(nameof(error));
17+
ErrorCode = error.ErrorCode;
18+
}
19+
20+
public BL3PException(string message)
21+
: base(message)
22+
{
23+
}
24+
25+
public BL3PException(string message, Exception inner)
26+
: base(message, inner)
27+
{
28+
}
29+
30+
protected BL3PException(
31+
SerializationInfo info,
32+
StreamingContext context
33+
) : base(info, context)
34+
{
35+
}
36+
}
37+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using ExchangeSharp.API.Exchanges.BL3P.Models;
2+
using ExchangeSharp.Dependencies.Converters;
3+
using Newtonsoft.Json;
4+
5+
namespace ExchangeSharp.API.Exchanges.BL3P.Converters
6+
{
7+
internal class BL3PResponseConverter<TSuccess> : JsonComplexObjectConverter<BL3PResponsePayload>
8+
where TSuccess : BL3PResponsePayload, new()
9+
{
10+
protected override BL3PResponsePayload Create(JsonReader reader)
11+
{
12+
while (reader.Read())
13+
{
14+
if (reader.TokenType != JsonToken.PropertyName)
15+
{
16+
continue;
17+
}
18+
19+
var prop = (string) reader.Value;
20+
21+
switch (prop)
22+
{
23+
// this is the first prop on an error object
24+
case "code":
25+
return new BL3PResponsePayloadError();
26+
default:
27+
return new TSuccess();
28+
}
29+
}
30+
31+
throw new JsonException("Could not locate key property in json.");
32+
}
33+
}
34+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace ExchangeSharp.API.Exchanges.BL3P.Enums
2+
{
3+
public enum BL3PCurrencyFee : byte
4+
{
5+
BTC = 0,
6+
EUR = 1
7+
}
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace ExchangeSharp.API.Exchanges.BL3P.Enums
2+
{
3+
internal enum BL3POrderStatus
4+
{
5+
Pending = 0,
6+
Open,
7+
Closed,
8+
Cancelled,
9+
Placed
10+
}
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace ExchangeSharp.API.Exchanges.BL3P.Enums
2+
{
3+
internal enum BL3POrderType
4+
{
5+
Bid,
6+
Ask
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace ExchangeSharp.API.Exchanges.BL3P.Enums
2+
{
3+
internal enum BL3PResponseType
4+
{
5+
Unknown = 0,
6+
Error,
7+
Success
8+
}
9+
}

0 commit comments

Comments
 (0)