public RiotRestAPI.MatchDTO LoadMatch(SeedData.MatchListing listing, string matchId)
        {
            RiotRestAPI.MatchDTO match = null;

            if (DoesRawCachedCopyOfMatchExist(listing, matchId))
            {
                match = LoadMatchFromRawFile(listing, matchId);
            }
            else
            {
                string rawResponse = "";
                match = LoadMatchFromAPI(listing, matchId, ref rawResponse);

                // Save the raw response file to speed up future queries
                string prettyJSON = JsonPrettyPrinterPlus.PrettyPrinterExtensions.PrettyPrintJson(rawResponse);

                string[]   rawfilePathParts = new string[] { _rawMatchDataDirectory, listing.region + "-" + matchId + ".json" };
                string     filePath         = System.IO.Path.Combine(rawfilePathParts);
                FileStream fstream          = new FileStream(filePath, FileMode.Create);
                byte[]     data             = Encoding.ASCII.GetBytes(prettyJSON);
                fstream.Write(data, 0, data.Length);
                fstream.Close();
            }

            return(match);
        }
        protected RiotRestAPI.MatchDTO LoadMatchFromAPI(SeedData.MatchListing listing, string matchId, ref string rawResponse)
        {
            RiotRestAPI.MatchDTO match = null;

            bool rateLimitHit = true;

            while (rateLimitHit)
            {
                string resource = "/" + listing.region + "/v2.2/match/" + matchId;

                Dictionary <string, string> queryParams = new Dictionary <string, string>();
                queryParams["includeTimeline"] = "true";
                match = _apiConnection.Get <RiotRestAPI.MatchDTO>(resource, queryParams,
                                                                  ref rateLimitHit, ref rawResponse);
                if (match != null)
                {
                    LogProgress("Loaded match " + listing.region + "-" + matchId + " from the API.");
                }
                else if (rateLimitHit)
                {
                    LogProgress("Hit rate limit. Waiting to retry.");
                    System.Threading.Thread.Sleep(RATE_LIMIT_WAIT_IN_MS);
                }
                else
                {
                    LogProgress("Unable to load match: " + listing.region + " - " + matchId);
                }
            }

            return(match);
        }
        public bool CacheMatchesFromAPI(List <SeedData.MatchListing> listings, CancellationToken cancelationToken)
        {
            foreach (SeedData.MatchListing listing in listings)
            {
                foreach (string matchId in listing.MatchIds)
                {
                    if (cancelationToken.IsCancellationRequested)
                    {
                        return(true);
                    }

                    RiotRestAPI.MatchDTO match = LoadMatch(listing, matchId);
                }
            }
            return(false);
        }
        protected RiotRestAPI.MatchDTO LoadMatchFromRawFile(SeedData.MatchListing matchListing, string matchId)
        {
            RiotRestAPI.MatchDTO match = null;

            string[]   rawfilePathParts = new string[] { _rawMatchDataDirectory, BuildMatchFileName(matchListing, matchId) };
            string     filePath         = System.IO.Path.Combine(rawfilePathParts);
            FileStream fstream          = new FileStream(filePath, FileMode.Open);
            DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(RiotRestAPI.MatchDTO));
            object objResponse = null;

            try
            {
                objResponse = jsonSerializer.ReadObject(fstream);
            }
            catch (System.Xml.XmlException ex)
            {
                LogProgress("XML Exception while parsing match response: " + matchListing.region + "-" + matchId + " - " + ex.Message);
            }
            catch (Exception ex)
            {
                LogProgress("Generic Exception while parsing match response: " + matchListing.region + "-" + matchId + " - " + ex.Message);
            }

            fstream.Close();

            if (objResponse == null)
            {
                LogProgress("Failed to load match " + matchListing.region + "-" + matchId + " from cached data. Deleting file.");
                File.Delete(filePath);
            }
            else
            {
                match = (RiotRestAPI.MatchDTO)Convert.ChangeType(objResponse, typeof(RiotRestAPI.MatchDTO));
                LogProgress("Loaded match " + matchListing.region + "-" + matchId + " from cached data.");
            }

            return(match);
        }