Skip to content

Commit

Permalink
[Feature] Ability to define multiple statuses in get order request (#183
Browse files Browse the repository at this point in the history
)
  • Loading branch information
dougdellolio authored Dec 12, 2018
1 parent 349b25d commit 0a74a70
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 49 deletions.
137 changes: 94 additions & 43 deletions CoinbasePro.Specs/Services/Orders/OrdersServiceSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,50 +294,101 @@ class when_getting_all_orders

class when_getting_all_orders_and_specifying_order_status
{
Establish context = () =>
The<IHttpClient>().WhenToldTo(p => p.ReadAsStringAsync(Param.IsAny<HttpResponseMessage>()))
.Return(Task.FromResult(OrderResponseFixture.CreateLimitOrderMany(OrderStatus.Active)));

Because of = () =>
order_many_response_result = Subject.GetAllOrdersAsync(OrderStatus.Active).Result;

It should_have_correct_number_of_orders = () =>
order_many_response_result.First().Count.ShouldEqual(2);

It should_have_correct_orders = () =>
class with_single_status
{
order_many_response_result.First().First().Id.ShouldEqual(new Guid("d0c5340b-6d6c-49d9-b567-48c4bfca13d2"));
order_many_response_result.First().First().Price.ShouldEqual(0.10000000M);
order_many_response_result.First().First().Size.ShouldEqual(0.01000000M);
order_many_response_result.First().First().ProductId.ShouldEqual(ProductType.BtcUsd);
order_many_response_result.First().First().Side.ShouldEqual(OrderSide.Buy);
order_many_response_result.First().First().Stp.ShouldEqual("dc");
order_many_response_result.First().First().OrderType.ShouldEqual(OrderType.Limit);
order_many_response_result.First().First().TimeInForce.ShouldEqual(TimeInForce.Gtc);
order_many_response_result.First().First().PostOnly.ShouldBeFalse();
order_many_response_result.First().First().CreatedAt.ShouldEqual(new DateTime(2016, 12, 9));
order_many_response_result.First().First().FillFees.ShouldEqual(0.0000000000000000M);
order_many_response_result.First().First().FilledSize.ShouldEqual(0.00000000M);
order_many_response_result.First().First().ExecutedValue.ShouldEqual(0.0000000000000000M);
order_many_response_result.First().First().Status.ShouldEqual(OrderStatus.Active);
order_many_response_result.First().First().Settled.ShouldBeFalse();

order_many_response_result.First().Skip(1).First().Id.ShouldEqual(new Guid("8b99b139-58f2-4ab2-8e7a-c11c846e3022"));
order_many_response_result.First().Skip(1).First().Price.ShouldEqual(0.10000000M);
order_many_response_result.First().Skip(1).First().Size.ShouldEqual(0.01000000M);
order_many_response_result.First().Skip(1).First().ProductId.ShouldEqual(ProductType.EthUsd);
order_many_response_result.First().Skip(1).First().Side.ShouldEqual(OrderSide.Buy);
order_many_response_result.First().Skip(1).First().Stp.ShouldEqual("dc");
order_many_response_result.First().Skip(1).First().OrderType.ShouldEqual(OrderType.Limit);
order_many_response_result.First().Skip(1).First().TimeInForce.ShouldEqual(TimeInForce.Gtc);
order_many_response_result.First().Skip(1).First().PostOnly.ShouldBeFalse();
order_many_response_result.First().Skip(1).First().CreatedAt.ShouldEqual(new DateTime(2016, 12, 9));
order_many_response_result.First().Skip(1).First().FillFees.ShouldEqual(0.0000000000000000M);
order_many_response_result.First().Skip(1).First().FilledSize.ShouldEqual(0.00000000M);
order_many_response_result.First().Skip(1).First().ExecutedValue.ShouldEqual(0.0000000000000000M);
order_many_response_result.First().Skip(1).First().Status.ShouldEqual(OrderStatus.Active);
order_many_response_result.First().Skip(1).First().Settled.ShouldBeFalse();
};
Establish context = () =>
The<IHttpClient>().WhenToldTo(p => p.ReadAsStringAsync(Param.IsAny<HttpResponseMessage>()))
.Return(Task.FromResult(OrderResponseFixture.CreateLimitOrderMany(OrderStatus.Active)));

Because of = () =>
order_many_response_result = Subject.GetAllOrdersAsync(OrderStatus.Active).Result;

It should_have_correct_number_of_orders = () =>
order_many_response_result.First().Count.ShouldEqual(2);

It should_have_correct_orders = () =>
{
order_many_response_result.First().First().Id.ShouldEqual(new Guid("d0c5340b-6d6c-49d9-b567-48c4bfca13d2"));
order_many_response_result.First().First().Price.ShouldEqual(0.10000000M);
order_many_response_result.First().First().Size.ShouldEqual(0.01000000M);
order_many_response_result.First().First().ProductId.ShouldEqual(ProductType.BtcUsd);
order_many_response_result.First().First().Side.ShouldEqual(OrderSide.Buy);
order_many_response_result.First().First().Stp.ShouldEqual("dc");
order_many_response_result.First().First().OrderType.ShouldEqual(OrderType.Limit);
order_many_response_result.First().First().TimeInForce.ShouldEqual(TimeInForce.Gtc);
order_many_response_result.First().First().PostOnly.ShouldBeFalse();
order_many_response_result.First().First().CreatedAt.ShouldEqual(new DateTime(2016, 12, 9));
order_many_response_result.First().First().FillFees.ShouldEqual(0.0000000000000000M);
order_many_response_result.First().First().FilledSize.ShouldEqual(0.00000000M);
order_many_response_result.First().First().ExecutedValue.ShouldEqual(0.0000000000000000M);
order_many_response_result.First().First().Status.ShouldEqual(OrderStatus.Active);
order_many_response_result.First().First().Settled.ShouldBeFalse();

order_many_response_result.First().Skip(1).First().Id.ShouldEqual(new Guid("8b99b139-58f2-4ab2-8e7a-c11c846e3022"));
order_many_response_result.First().Skip(1).First().Price.ShouldEqual(0.10000000M);
order_many_response_result.First().Skip(1).First().Size.ShouldEqual(0.01000000M);
order_many_response_result.First().Skip(1).First().ProductId.ShouldEqual(ProductType.EthUsd);
order_many_response_result.First().Skip(1).First().Side.ShouldEqual(OrderSide.Buy);
order_many_response_result.First().Skip(1).First().Stp.ShouldEqual("dc");
order_many_response_result.First().Skip(1).First().OrderType.ShouldEqual(OrderType.Limit);
order_many_response_result.First().Skip(1).First().TimeInForce.ShouldEqual(TimeInForce.Gtc);
order_many_response_result.First().Skip(1).First().PostOnly.ShouldBeFalse();
order_many_response_result.First().Skip(1).First().CreatedAt.ShouldEqual(new DateTime(2016, 12, 9));
order_many_response_result.First().Skip(1).First().FillFees.ShouldEqual(0.0000000000000000M);
order_many_response_result.First().Skip(1).First().FilledSize.ShouldEqual(0.00000000M);
order_many_response_result.First().Skip(1).First().ExecutedValue.ShouldEqual(0.0000000000000000M);
order_many_response_result.First().Skip(1).First().Status.ShouldEqual(OrderStatus.Active);
order_many_response_result.First().Skip(1).First().Settled.ShouldBeFalse();
};
}

class with_multiple_statuses
{
Establish context = () =>
The<IHttpClient>().WhenToldTo(p => p.ReadAsStringAsync(Param.IsAny<HttpResponseMessage>()))
.Return(Task.FromResult(OrderResponseFixture.CreateLimitOrderMany(OrderStatus.Active)));

Because of = () =>
order_many_response_result = Subject.GetAllOrdersAsync(new [] { OrderStatus.Active, OrderStatus.Done }).Result;

It should_have_correct_number_of_orders = () =>
order_many_response_result.First().Count.ShouldEqual(2);

It should_have_correct_orders = () =>
{
order_many_response_result.First().First().Id.ShouldEqual(new Guid("d0c5340b-6d6c-49d9-b567-48c4bfca13d2"));
order_many_response_result.First().First().Price.ShouldEqual(0.10000000M);
order_many_response_result.First().First().Size.ShouldEqual(0.01000000M);
order_many_response_result.First().First().ProductId.ShouldEqual(ProductType.BtcUsd);
order_many_response_result.First().First().Side.ShouldEqual(OrderSide.Buy);
order_many_response_result.First().First().Stp.ShouldEqual("dc");
order_many_response_result.First().First().OrderType.ShouldEqual(OrderType.Limit);
order_many_response_result.First().First().TimeInForce.ShouldEqual(TimeInForce.Gtc);
order_many_response_result.First().First().PostOnly.ShouldBeFalse();
order_many_response_result.First().First().CreatedAt.ShouldEqual(new DateTime(2016, 12, 9));
order_many_response_result.First().First().FillFees.ShouldEqual(0.0000000000000000M);
order_many_response_result.First().First().FilledSize.ShouldEqual(0.00000000M);
order_many_response_result.First().First().ExecutedValue.ShouldEqual(0.0000000000000000M);
order_many_response_result.First().First().Status.ShouldEqual(OrderStatus.Active);
order_many_response_result.First().First().Settled.ShouldBeFalse();

order_many_response_result.First().Skip(1).First().Id.ShouldEqual(new Guid("8b99b139-58f2-4ab2-8e7a-c11c846e3022"));
order_many_response_result.First().Skip(1).First().Price.ShouldEqual(0.10000000M);
order_many_response_result.First().Skip(1).First().Size.ShouldEqual(0.01000000M);
order_many_response_result.First().Skip(1).First().ProductId.ShouldEqual(ProductType.EthUsd);
order_many_response_result.First().Skip(1).First().Side.ShouldEqual(OrderSide.Buy);
order_many_response_result.First().Skip(1).First().Stp.ShouldEqual("dc");
order_many_response_result.First().Skip(1).First().OrderType.ShouldEqual(OrderType.Limit);
order_many_response_result.First().Skip(1).First().TimeInForce.ShouldEqual(TimeInForce.Gtc);
order_many_response_result.First().Skip(1).First().PostOnly.ShouldBeFalse();
order_many_response_result.First().Skip(1).First().CreatedAt.ShouldEqual(new DateTime(2016, 12, 9));
order_many_response_result.First().Skip(1).First().FillFees.ShouldEqual(0.0000000000000000M);
order_many_response_result.First().Skip(1).First().FilledSize.ShouldEqual(0.00000000M);
order_many_response_result.First().Skip(1).First().ExecutedValue.ShouldEqual(0.0000000000000000M);
order_many_response_result.First().Skip(1).First().Status.ShouldEqual(OrderStatus.Active);
order_many_response_result.First().Skip(1).First().Settled.ShouldBeFalse();
};
}
}

class when_getting_order_by_id
Expand Down
2 changes: 1 addition & 1 deletion CoinbasePro/CoinbaseProClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public CoinbaseProClient(

AccountsService = new AccountsService(httpClient, httpRequestMessageService);
CoinbaseAccountsService = new CoinbaseAccountsService(httpClient, httpRequestMessageService);
OrdersService = new OrdersService(httpClient, httpRequestMessageService);
OrdersService = new OrdersService(httpClient, httpRequestMessageService, queryBuilder);
PaymentsService = new PaymentsService(httpClient, httpRequestMessageService);
WithdrawalsService = new WithdrawalsService(httpClient, httpRequestMessageService);
DepositsService = new DepositsService(httpClient, httpRequestMessageService);
Expand Down
2 changes: 1 addition & 1 deletion CoinbasePro/Network/Authentication/Authenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public string ComputeSignature(
return HashString(prehash, convertedString);
}

public string HashString(string str, byte[] secret)
private string HashString(string str, byte[] secret)
{
var bytes = Encoding.UTF8.GetBytes(str);
using (var hmaccsha = new HMACSHA256(secret))
Expand Down
3 changes: 0 additions & 3 deletions CoinbasePro/Network/Authentication/IAuthenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,5 @@ string ComputeSignature(
double timestamp,
string requestUri,
string contentBody = "");

string HashString(string str, byte[] secret);

}
}
21 changes: 20 additions & 1 deletion CoinbasePro/Services/Orders/OrdersService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CoinbasePro.Shared.Utilities.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using CoinbasePro.Network.HttpClient;
Expand All @@ -10,16 +11,21 @@
using CoinbasePro.Services.Orders.Types;
using CoinbasePro.Shared.Types;
using CoinbasePro.Shared.Utilities;
using CoinbasePro.Shared.Utilities.Queries;

namespace CoinbasePro.Services.Orders
{
public class OrdersService : AbstractService
{
private readonly QueryBuilder queryBuilder;

public OrdersService(
IHttpClient httpClient,
IHttpRequestMessageService httpRequestMessageService)
IHttpRequestMessageService httpRequestMessageService,
QueryBuilder queryBuilder)
: base(httpClient, httpRequestMessageService)
{
this.queryBuilder = queryBuilder;
}

public async Task<OrderResponse> PlaceMarketOrderAsync(
Expand Down Expand Up @@ -171,6 +177,19 @@ public async Task<IList<IList<OrderResponse>>> GetAllOrdersAsync(
return httpResponseMessage;
}

public async Task<IList<IList<OrderResponse>>> GetAllOrdersAsync(
OrderStatus[] orderStatus,
int limit = 100,
int numberOfPages = 0)
{
var queryKeyValuePairs = orderStatus.Select(p => new KeyValuePair<string, string>("status", p.GetEnumMemberValue())).ToArray();
var query = queryBuilder.BuildQuery(queryKeyValuePairs);

var httpResponseMessage = await SendHttpRequestMessagePagedAsync<OrderResponse>(HttpMethod.Get, $"/orders{query}&limit={limit}", numberOfPages: numberOfPages);

return httpResponseMessage;
}

public async Task<OrderResponse> GetOrderByIdAsync(string id)
{
return await SendServiceCall<OrderResponse>(HttpMethod.Get, $"/orders/{id}").ConfigureAwait(false);
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ var allAccounts = await coinbaseProClient.AccountsService.GetAllAccountsAsync();
- CancelAllOrdersAsync() - cancel all orders
- CancelOrderByIdAsync(id) - cancel order by id
- GetAllOrdersAsync(orderStatus, limit, numberOfPages) - get all, active or pending orders (paged response)
- GetAllOrdersAsync(orderStatus[], limit, numberOfPages) - get orders by multiple statuses (paged response)
- GetOrderByIdAsync(id) - get order by id

###### Payments ######
Expand Down

0 comments on commit 0a74a70

Please sign in to comment.