/// <summary> /// A static method to create a client. /// </summary> /// <param name="connection"> /// The connection ip or host. /// </param> /// <param name="port"> /// The port the client rpc is listening on. /// </param> /// <param name="user"> /// The user name. /// </param> /// <param name="encPass"> /// The encrypted password. /// </param> /// <param name="secure"> /// Indicator to use ssl (https). /// </param> /// <returns> /// A new instance of <see cref="BitcoinClient"/>. /// </returns> public static IBitcoinClient Create(string connection, int port, string user, string encPass, bool secure) { // Set cache key name var cacheKey = "{0}:{1}:{2}:{3}".StringFormat(connection, port, user, secure); // Get storage source from cache if available var client = (IBitcoinClient)Cache.Get(cacheKey); if (client == null) { // Assuming no storage source in cache, attempt to create it lock (CacheLock) { client = (IBitcoinClient)Cache.Get(cacheKey); if (client == null) { // Add to cache client = BitcoinClient.Create(connection, port, user, encPass, secure); Cache.Add(cacheKey, client, new CacheItemPolicy { SlidingExpiration = new TimeSpan(0, 60, 0) }); } } } // Return storage source return(client); }
/// <summary> /// Make a call to crypto API. /// </summary> /// <param name="method"> /// The s Method. /// </param> /// <param name="parameters"> /// The parameters. /// </param> /// <returns> /// The <see cref="Task"/>. /// </returns> private async Task Call(string method, params object[] parameters) { var rpcReq = new JsonRpcRequest(1, method, parameters); var s = JsonConvert.SerializeObject(rpcReq); // serialize json for the request var byteArray = Encoding.UTF8.GetBytes(s); using (var request = new StreamContent(new MemoryStream(byteArray))) { request.Headers.ContentType = new MediaTypeHeaderValue("application/json-rpc"); var response = await BitcoinClient.Send(this.Client, this.Url, request); await this.CheckResponseOk <string>(response); } }