public async Task <StatisticResponse> ExcangeHistory(string Dates, string Currency) { var statistic = new StatisticResponse(); using (var httpClient = new HttpClient()) { try { httpClient.BaseAddress = new Uri(_configuration["BaseAddress"]); var dates = DateSpliter(Dates); var currencies = CurrencySpliter(Currency); foreach (string date in dates) { var rawObject = await GetExchangeRateModel(date, httpClient, currencies); foreach (var rate in rawObject.Rates) { var rateVal = rate.Value; if (rateVal < statistic.MinimumRate) { statistic.MinimumRate = rateVal; statistic.MinimumRateDate = rawObject.Date; } if (rateVal > statistic.MaximuRate) { statistic.MaximuRate = rateVal; statistic.MaximumRateDate = rawObject.Date; } statistic.RateSum += rateVal; statistic.RateCount++; } } } catch { } } return(statistic); }
public IEnumerator Share(Dictionary <string, string> values, string id = "", string name = "", Action <bool> callback = null) { bool update = false; if (this._apiAccessToken == null || !this._apiAccessToken.IsValid()) { yield return(this.GetAccessToken()); } // If no id is supplied but we have one stored, reuse it. if (id == "" && this.StatisticId != "") { id = this.StatisticId; } string url = "https://api.globalstats.io/v1/statistics"; if (id != "") { url = "https://api.globalstats.io/v1/statistics/" + id; update = true; } else { if (name == "") { name = "anonymous"; } } string jsonPayload; if (update == false) { jsonPayload = "{\"name\":\"" + name + "\", \"values\":{"; } else { jsonPayload = "{\"values\":{"; } bool semicolon = false; foreach (KeyValuePair <string, string> value in values) { if (semicolon) { jsonPayload += ","; } jsonPayload += "\"" + value.Key + "\":\"" + value.Value + "\""; semicolon = true; } jsonPayload += "}}"; byte[] pData = Encoding.UTF8.GetBytes(jsonPayload); StatisticResponse statistic = null; using (UnityWebRequest www = new UnityWebRequest(url)) { if (update == false) { www.method = "POST"; } else { www.method = "PUT"; } www.uploadHandler = new UploadHandlerRaw(pData); www.downloadHandler = new DownloadHandlerBuffer(); www.SetRequestHeader("Authorization", "Bearer " + this._apiAccessToken.access_token); www.SetRequestHeader("Content-Type", "application/json"); yield return(www.SendWebRequest()); string responseBody = www.downloadHandler.text; if (www.isNetworkError || www.isHttpError) { Debug.LogWarning("Error submitting statistic: " + www.error); Debug.Log("GlobalstatsIO API Response: " + responseBody); callback?.Invoke(false); } else { statistic = JsonUtility.FromJson <StatisticResponse>(responseBody); } }; // ID is available only on create, not on update, so do not overwrite it if (statistic._id != null && statistic._id != "") { this.StatisticId = statistic._id; } this.UserName = statistic.name; //Store the returned data statically foreach (StatisticValues value in statistic.values) { bool updatedExisting = false; for (int i = 0; i < this._statisticValues.Count; i++) { if (this._statisticValues[i].key == value.key) { this._statisticValues[i] = value; updatedExisting = true; break; } } if (!updatedExisting) { this._statisticValues.Add(value); } } callback?.Invoke(true); }