/// <summary> /// synchroniously updates <see cref="CoinListing"/> repository with data from server. /// </summary> public void UpdateRepository() { try { CoinListing[] coinListingArray = CMCRequestHandler.RequestCoinListings(); // update id-to-CoinListing, name-to-CoinListing, symbol-to-CoinListing dictionaries for (int i = 0; i < coinListingArray.Length; i++) { CoinListing curCoinListing = coinListingArray[i]; // get CoinListing fields in lowercase int id = curCoinListing.Id; string lowercaseName = curCoinListing.Name.ToLower(); string lowercaseSymbol = curCoinListing.Symbol.ToLower(); // update dictionaries coinIdToCoinListing[id] = curCoinListing; coinNameToCoinListing[lowercaseName] = curCoinListing; coinSymbolToCoinListing[lowercaseSymbol] = curCoinListing; } } catch (DataRequestException dataRequestException) // request unsuccessful { throw new RepositoryUpdateException(dataRequestException); } }
/// <summary> /// returns a new <see cref="PropertyRow"/> corresponding to <paramref name="coinListing"/>. /// </summary> /// <param name="coinListing"></param> /// <returns> /// <see cref="PropertyRow"/> corresponding to <paramref name="coinListing"/> /// </returns> private PropertyRow getPropertyRow(CoinListing coinListing) { // convert coinListing to a CoinData to get a CoinData type object CoinData coinData = new CoinData(coinListing); return(new PropertyRow(coinData, PROPERTIES)); }
/// <summary> /// fills <paramref name="coinListingArray"/> with <see cref="CoinListing"/>s fetched from /// <paramref name="coinListingArrayJToken"/>. /// </summary> /// <param name="coinListingArray"></param> /// <param name="CoinListingArrayJToken"></param> /// <exception cref="ArgumentNullException"> /// <seealso cref="JsonUtils.AssertExist(JToken, object[])"/> /// </exception> /// <exception cref="JsonPropertyParseException"> /// <seealso cref="JsonUtils.AssertExist(JToken, object[])"/> /// <seealso cref="JsonUtils.GetPropertyValue{T}(JToken, string)"/> /// in addition, thrown if listing array length specified in <paramref name="coinListingArrayJToken"/> /// does not match length of <paramref name="coinListingArray"/> /// </exception> private static void fillCoinListingArray( CoinListing[] coinListingArray, JToken coinListingArrayJToken) { try { for (int i = 0; i < coinListingArray.Length; i++) { AssertExist(coinListingArrayJToken, i); JToken currentCoinListing = coinListingArrayJToken[i]; AssertExist(currentCoinListing, "id", "name", "symbol"); int id = GetPropertyValue <int>(currentCoinListing, "id"); string name = GetPropertyValue <string>(currentCoinListing, "name"); string symbol = GetPropertyValue <string>(currentCoinListing, "symbol"); long unixTimestamp = Utils.DateTimeUtils.GetUnixTimestamp(); coinListingArray[i] = new CoinListing(id, name, symbol, unixTimestamp); } } // listing array size specified in coinListingArrayJToken does not match coinListingArray length catch (ArgumentOutOfRangeException) { throw new JsonPropertyParseException("metadata.num_cryptocurrencies"); } }
/// <summary> /// parses a <see cref="CoinListing"/> array from <paramref name="ListingJsonString"/>. /// </summary> /// <seealso cref="JsonConvert.DeserializeObject(string)"/> /// <seealso cref="JsonUtils.AssertExist(JToken, object[])"/> /// <seealso cref="JToken.Value{T}(object)"/> /// <param name="ListingJSONString"></param> /// <returns> /// <see cref="CoinListing"/> array parsed from <paramref name="ListingJsonString"/> /// </returns> /// <exception cref="CoinListingJsonParseException"> /// thrown if <paramref name="ListingJsonString"/> is invalid. /// </exception> internal static CoinListing[] ParseCoinListingArray(string ListingJsonString) { try { CoinListing[] coinListingArray; // deserialize json string to get a JToken JToken coinListingJToken = (JToken)JsonConvert.DeserializeObject(ListingJsonString); // handle matadata JsonUtils.AssertExist(coinListingJToken, "metadata", "data"); JToken coinListingMetadataJToken = coinListingJToken["metadata"]; // get length of coin listing array JsonUtils.AssertExist(coinListingMetadataJToken, "num_cryptocurrencies"); int coinListingArrayLength = JsonUtils.GetPropertyValue <int>( coinListingMetadataJToken, "num_cryptocurrencies"); if (coinListingArrayLength <= 0) // invalid coin listing array length { throw new JsonPropertyParseException("data.num_cryptocurrencies"); } // init coin listing array coinListingArray = new CoinListing[coinListingArrayLength]; // get coin listing array JToken JToken CoinListingsArrayJToken = coinListingJToken["data"]; // fill coin listing array with data from JToken fillCoinListingArray(coinListingArray, CoinListingsArrayJToken); return(coinListingArray); } catch (Exception exception) { if ( exception is JsonReaderException || exception is JsonPropertyParseException || exception is InvalidCastException) { throw new CoinListingJsonParseException(exception); } else // unhandled exception { throw exception; } } }
/// <summary> /// returns <see cref="CoinData"/> section of <see cref="PortfolioEntryPropertyRow"/> /// <see cref="object"/> array. /// </summary> /// <seealso cref="CollectionUtils.DuplicateToArray{T}(T, int)"/> /// <param name="portfolioEntry"></param> /// <returns> /// <see cref="CoinData"/> section of <see cref="PortfolioEntryPropertyRow"/> /// <see cref="object"/> array. /// </returns> private static CoinData[] constructCoinDataArray(PortfolioEntry portfolioEntry) { // get coin listing corresponding to PortfolioEntry coin id CoinListing coinListing = CoinListingManager.Instance.GetCoinListing(portfolioEntry.CoinId); // create a corresponding CoinData object CoinData coinData = new CoinData(coinListing); // construct CoinData array CoinData[] coinDataArray = CollectionUtils.DuplicateToArray( coinData, COIN_DATA_PROPERTIES.Length); return(coinDataArray); }
/// <summary> /// return a <see cref="StaticCoinData"/>array containing data of all coins in database, /// each element encapsulating static server data corresponding to a specific coin. /// </summary> /// <exception cref="DataRequestFailedException"><see cref="sendDataRequest(string)"/></exception> /// <exception cref="DataRequestUnsuccessfulStatusCodeException"><see cref="sendDataRequest(string)"/> /// </exception> /// <exception cref="InvalidServerResponse">thrown if a <see cref="StaticCoinData"/>object could not /// be parsed from server response string.</exception> /// <returns> /// <see cref="StaticCoinData"/>array containing data of all coins in database, /// each element encapsulating static server data corresponding to a specific coin. /// </returns> public static CoinListing[] RequestCoinListings() { CoinListing[] coinListingsArray; string serverResponse = sendDataRequest(LISTINGS_REQUEST_URL); try { coinListingsArray = CoinListing.ParseCoinListingArray(serverResponse); return(coinListingsArray); } catch (CoinListingJsonParseException coinListingJsonParseException) { throw new ServerResponseParseException(coinListingJsonParseException); } }
/// <summary> /// returns display string of <see cref="CoinListingTable"/> with /// rows corresponding to <paramref name="coinIds"/>. /// </summary> /// <param name="coinIds"></param> /// <returns> /// display string of <see cref="CoinListingTable"/> with /// rows corresponding to <paramref name="coinIds"/>. /// </returns> /// <exception cref="ManagerNotInitializedException"> /// <seealso cref="assertManagerInitialized(string)"/> /// </exception> /// <exception cref="CoinIdDoesNotExistException"> /// <seealso cref="GetCoinListing(long)"/> /// </exception> public string GetCoinListingTableDisplayString(params long[] coinIds) { assertManagerInitialized("GetCoinListingTableDisplayString"); // init coin listing table CoinListingTable coinListingTable = new CoinListingTable(); foreach (long coinId in coinIds) { // add row corresponding to each coin listing associated with specified id CoinListing coinListing = GetCoinListing(coinId); coinListingTable.AddRow(coinListing); } // return table display string string coinListingTableString = coinListingTable.GetTableDisplayString(); return(coinListingTableString); }
/// <summary> /// initializes a new <see cref="CoinData"/> object using <paramref name="coinListing"/>. /// </summary> /// <param name="coinListing"></param> public CoinData(CoinListing coinListing) : this(coinListing.id, coinListing.name, coinListing.symbol, coinListing.unixTimestamp) { }
/// <summary> /// <para> /// removes row corresponding to <paramref name="coinListing"/> from table. /// </para> /// <para> /// returns whether row existed in table prior to being removed. /// </para> /// </summary> /// <param name="coinListing"></param> /// <returns> /// true if row corresponding to <paramref name="coinListing"/> existed in table prior to being removed, /// else false /// </returns> public bool RemoveRow(CoinListing coinListing) { PropertyRow propertyRow = getPropertyRow(coinListing); return(propertyTable.RemoveRow(propertyRow)); }
/// <summary> /// adds row corresponding to <paramref name="coinListing"/> to table. /// </summary> /// <seealso cref="PropertyTable.AddRow(PropertyRow)"/> /// <param name="coinListing"></param> public void AddRow(CoinListing coinListing) { PropertyRow propertyRow = getPropertyRow(coinListing); propertyTable.AddRow(propertyRow); }