コード例 #1
0
        public async Task <CMCResponse> Fetch(string endpoint, string symbol, string fiat)
        {
            var api = "https://pro-api.coinmarketcap.com/v1/cryptocurrency";

            var queryString = HttpUtility.ParseQueryString(string.Empty);

            queryString["symbol"]  = symbol;
            queryString["convert"] = fiat;

            var url     = $"{api}/{endpoint}?{queryString}"; // "https://www.pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?symbol=btc&convert=eur"
            var request = new HttpRequestMessage()
            {
                RequestUri = new Uri(url),
                Method     = HttpMethod.Get,
            };

            request.Headers.Add("X-CMC_PRO_API_KEY", "7c001994-3cf3-47da-b74d-ef8e4692d7ed");

            CMCResponse result = new CMCResponse();

            using (var httpClient = new HttpClient())
            {
                using (var httpResponse = await httpClient.SendAsync(request))
                {
                    try
                    {
                        string response = await httpResponse.Content.ReadAsStringAsync();

                        result = JsonConvert.DeserializeObject <CMCResponse>(response);
                    }
                    catch (Exception e)
                    {
                        return(null);
                    }
                }
            }

            return(result);
        }
コード例 #2
0
        public CryptoResponse CreateCryptoResponse(CMCResponse CMCResponse)
        {
            CryptoResponse response = new CryptoResponse();

            if (CMCResponse != null)
            {
                response.successful = true;
                response.message    = $"Listing for {CMCResponse.Data.BTC.Name}";
                response.data       = new CryptoData()
                {
                    lastUpdated = CMCResponse.Data.BTC.Quote.EUR.LastUpdated,
                    name        = CMCResponse.Data.BTC.Name,
                    price       = CMCResponse.Data.BTC.Quote.EUR.Price,
                    raw         = CMCResponse,
                    symbol      = CMCResponse.Data.BTC.Symbol,
                };
            }
            else
            {
                response.successful = false;
                response.message    = "Could not get listing.";
            }
            return(response);
        }