public async Task <DbStatsResponseModel> GetDbStats( CancellationToken cancellationToken ) { var values = new Dictionary <string, object>(); values.Add("format", ApiResponseFormat); values.Add("action", GetDbStatsActionName); var response = await CallApi( GetDbStatsActionName, values, cancellationToken ); if (!response.IsSuccessStatusCode) { throw new YourlsException(); } var responseText = await response.Content.ReadAsStringAsync(); var resultDict = JsonDeserializer.DeserializeToDictionary(responseText); return(ReadDbStatsFromDictionary(DbStatsPropertyName, resultDict)); }
public async Task <StatsResponseModel> GetStats( StatsFilterMode mode, int limit, CancellationToken cancellationToken ) { if (limit <= 0) { throw new InvalidOperationException("limit was zero or less than zero."); } var values = new Dictionary <string, object>(); string filter; switch (mode) { case StatsFilterMode.Top: filter = "top"; break; case StatsFilterMode.Bottom: filter = "bottom"; break; case StatsFilterMode.Random: filter = "rand"; break; case StatsFilterMode.Last: filter = "last"; break; default: throw new ArgumentOutOfRangeException(nameof(mode), mode, null); } values.Add("filter", filter); values.Add("limit", limit); values.Add("format", ApiResponseFormat); values.Add("action", GetStatsActionName); var response = await CallApi( GetStatsActionName, values, cancellationToken ); if (!response.IsSuccessStatusCode) { throw new YourlsException(); } var responseText = await response.Content.ReadAsStringAsync(); var resultModel = JsonDeserializer.DeserializeToDictionary(responseText); if (resultModel is null || !resultModel.TryGetValue(MessagePropertyName, out var message) || !(message is string strMsg) || strMsg?.ToLower() != "success" || !resultModel.TryGetValue(StatsLinksPropertyName, out var linksObj) || !(linksObj is IDictionary <string, object> linksDictionary)) { throw new YourlsException( $"The result of {nameof(JsonDeserializer.DeserializeObject)} was null or it didn't contain a Message value."); } var dbStats = ReadDbStatsFromDictionary(GetStatsDbStatsPropertyName, resultModel); var links = ReadUrlStatsFromDictionary(linksDictionary); return(new StatsResponseModel { Stats = dbStats, Links = links }); }