bchavez / Coinbase

:moneybag: A .NET/C# implementation of the Coinbase API.

Home Page:https://developers.coinbase.com/api/v2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Async support

shravan2x opened this issue · comments

I was unable to find async versions of methods in the library, am I missing them or has this not been added yet?

Hi @shravan2x ,

That's correct. async and .NET Core are not available for the Coinbase .NET/API client at the moment.

Perhaps you can bug @barmstrong to donate me some Bitcoin/Ethereum (or even better, free market orders on GDAX 😸) and I can put more effort into improving the Coinbase .NET/API client and bringing it up to date.

Otherwise, as I mentioned before, you'll need to send a PR if you want new features. I'd be willing to accept a PR for async support; however, #25 should be done before #27.

Thanks,
Brian

💥 💫 "Crashing, hit a wall. Right now I need a miracle..."

At this time, I'm juggling class and work and other things and will not be able to do this. However, I made a little utility that offers async support for the core methods, in case it helps anyone:

public class CoinbaseAsyncApi : CoinbaseApi
{
    public CoinbaseAsyncApi(string apiKey, string apiSecret) : base(apiKey, apiSecret, false) { }

    public async Task<CoinbaseResponse<TResponse>> SendRequestAsync<TResponse>(string endpoint, object body, Method httpMethod = Method.POST)
    {
        RestClient client = CreateClient();

        IRestRequest req = CreateRequest(endpoint, httpMethod)
                .AddJsonBody(body);

        IRestResponse<CoinbaseResponse<TResponse>> resp = await client.ExecuteTaskAsync<CoinbaseResponse<TResponse>>(req);

        return resp.Data;
    }

    public async Task<CoinbaseResponse<TResponse>> SendGetRequestAsync<TResponse>(string endpoint, params KeyValuePair<string, string>[] queryParams)
    {
        RestClient client = CreateClient();

        IRestRequest req = CreateRequest(endpoint, Method.GET);
        if (queryParams != null)
        {
            foreach (KeyValuePair<string, string> kvp in queryParams)
                req.AddQueryParameter(kvp.Key, kvp.Value);
        }

        IRestResponse<CoinbaseResponse<TResponse>> resp = await client.ExecuteTaskAsync<CoinbaseResponse<TResponse>>(req);

        return resp.Data;
    }
}