Build DLLs that call a REST API to return
the latest price of bitcoin in US dollars and Euros.
There are four programs that are part of the broader solution
MyApp4Lib. Input and build this sample code.
Two DLLs are created:
-
MyApp4Lib.dll
-
RestSharp.dll
Procedure
-
Compile the code that is used to test the methods.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyApp4Lib;
namespace MyApp4Test
{
class Program // Test program for the classes
{
static void Main(string[] args)
{
VisibleBotMethods vbm = new VisibleBotMethods();
String UsdPrice = vbm.GetPriceOfBitcoin("usd");
Console.WriteLine("\nPrice of Bitcoin in USD: " + UsdPrice);
Console.ReadKey();
}
}
}
-
Compile the code that orchestrates the REST call and retrieves the bitcoin
price.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyApp4Lib
{
public class VisibleBotMethods
{
public String GetPriceOfBitcoin(String Currency)
{
// Make GET call -- the Currency is the variable:
// usd for $Dollars, eur for Euros
String URL = "https://api.cryptonator.com/api/ticker/btc-" + Currency;
// Instantiate objects
RestUtils ru = new RestUtils();
JsonUtils ju = new JsonUtils();
// Make the REST call and return the JSON response
String JsonResp = ru.CallRestGETNoAuth(URL);
// Retrieve just the price from the JSON as a String
String BCPrice = ju.GetDataFromJsonResponse(JsonResp);
return BCPrice;
}
}
}
-
Compile the methods to make REST calls.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace MyApp4Lib
{
public class RestUtils
{
public String CallRestGETNoAuth(String URL)
{
// Setting up a web request on the URL that we pass as a parameter,
// specifying a GET request for a JSON response
System.Net.HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(URL);
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "GET";
// Submitting the request, getting the response, turning it into a string,
// and returning the response
try
{
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new System.IO.StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
return result;
}
}
catch (System.Net.WebException e) // Catch error if URL is not valid
{
return "Error:" + e.Message;
}
}
}
}
-
Compile the code that processes the JSON response and returns the bitcoin
price.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyApp4Lib
{
class JsonUtils // Includes methods to process JSON
{
public String GetDataFromJsonResponse(String JsonResp)
{
RestSharp.RestResponse response = new RestSharp.RestResponse();
response.Content = JsonResp;
// Turning JSON structure into object / class structure
JsonDeserializer deserial = new JsonDeserializer();
StdJsonResponseForCurrCheck x = deserial.Deserialize<StdJsonResponseForCurrCheck>(response);
String Resp = "";
// Identifying price within the class structure, and then return it
Resp = x.ticker.price;
return Resp;
}
}
// Taking JSON response and turning it into classes
public class Ticker
{
public string @base { get; set; }
public string target { get; set; }
public string price { get; set; }
public string volume { get; set; }
public string change { get; set; }
}
public class StdJsonResponseForCurrCheck
{
public Ticker ticker { get; set; }
public int timestamp { get; set; }
public bool success { get; set; }
public string error { get; set; }
}
}
Next steps
Add the DLLs created in this task to a MetaBot in Create an advanced MetaBot.