bchavez / Coinbase

:moneybag: A .NET/C# implementation of the Coinbase API.
https://developers.coinbase.com/api/v2
MIT License
170 stars 93 forks source link

Parameter Ignored on Exchange-Rates Call #26

Closed DevonJSmith closed 7 years ago

DevonJSmith commented 7 years ago

According to the Coinbase API, the call for current exchange rates GET https://api.coinbase.com/v2/exchange-rates allows an optional parameter to specify the base currency. If this parameter is not provided, the default is USD.

However when using this library to make such a call, any supplied "currency" value is ignored.

Here is example code using this Coinbase library (based on a similar example found in the README):

var api = new CoinbaseApi(ApiKey, ApiSecretKey, useSandbox: false, proxy: null, useTimeApi: false);
var options = new
{
    currency = "BTC"
};

var response = api.SendRequest("exchange-rates", options, Method.GET);

This returns a JSON object (in the data field of the response object) in this format:

{{
  "currency": "USD",
  "rates": {
    "AED": "3.67",
    "AFN": "68.45",
    "ALL": "113.80",
     .....
  }
}}

As you can see, the currency value of this response specifies USD, even though BTC was passed as a parameter.

Contrasting this with a request made using HttpWebRequest:

string url = $"https://api.coinbase.com/v2/exchange-rates?currency=BTC";
var request = (HttpWebRequest) WebRequest.Create(url);

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
    var returnText = reader.ReadToEnd();         
}

The value of returnText:

"{\"data\":{\"currency\":\"BTC\",\"rates\":{\"AED\":\"14379.85\",\"AFN\":\"267981.75\....}}}"

As you can see, the currency value is BTC, as specified in the request URL and all exchange rates use BTC as a base.

Am I mistaken or using this library incorrectly? I would expect the first method using this library to produce the results the second method did.

bchavez commented 7 years ago

Hi @DevonJSmith ,

Per the XML parameter doc:

devenv_658

You are using options as the parameter for the body of a GET post. Hmm. I think what's happening is since GET requests don't have a body, the options object is ignored. You need to make your code look something more like:

var api = new CoinbaseApi(ApiKey, ApiSecretKey, useSandbox: false, proxy: null, useTimeApi: false);
var response = api.SendRequest("exchange-rates?currency=BTC", null, Method.GET);
var rates = response.Data;

Hope that helps. Feel free to reopen the issue if you can spot a problem or bug with the source code in the repository.

Thanks, Brian

:briefcase: :necktie: "Taking care of business every day... Taking care of business every way..."