Пример #1
0
        /// <summary>
        /// Returns a result for a single ID from the GW2 API for the specified resource.
        /// </summary>
        /// <typeparam name="R">The result type that query results from the GW2 API are to be filtered into.</typeparam>
        /// <typeparam name="E">The entity type that is to be used to save the result data to the SQL database.</typeparam>
        /// <param name="client">The HTTP client used to interact with the GW2 API.</param>
        /// <param name="resource">The type of resource to get data for.</param>
        /// <param name="id">The ID of the result to be retrieved.</param>
        /// <returns>A result for a single ID from the GW2 API for the specified resource.</returns>
        private Entity GetSingleResult <R, E>(APIResource resource, int id)
            where R : APIDataResult <E>
            where E : Entity
        {
            var baseURL         = Settings.Default.APIBaseURL;
            var listURL         = $"{baseURL}/{resource.GetPath()}";
            var singleResultURL = $"{listURL}/{id}";

            var retryCount = 0;

            while (retryCount < this._maxRetryCount)
            {
                var singleResultResponse = this._httpClient.GetAsync(singleResultURL).Result;

                if (singleResultResponse.IsSuccessStatusCode)
                {
                    Debug.WriteLine($"Resource : {resource} | ID : {id} | Retry Count : {retryCount}");
                    return(singleResultResponse.Content.ReadAsAsync <R>().Result.ToEntity());
                }

                if (singleResultResponse.ReasonPhrase == "Not Found")
                {
                    return(null);
                }

                retryCount += 1;
            }

            return(null);
        }
Пример #2
0
        /// <summary>
        /// Returns a complete list of all possible IDs for the specified resource.
        /// </summary>
        /// <typeparam name="E">The entity type that is to be used to save the result data to the SQL database.</typeparam>
        /// <param name="client">The HTTP client used to interact with the GW2 API.</param>
        /// <param name="resource">The type of resource to get data for.</param>
        /// <param name="targetDataSet">The dataset containing entities to be populated from results from the GW2 API.</param>
        /// <returns>A complete list of all possible IDs for the specified resource.</returns>
        private IEnumerable <int> GetIds <E>(APIResource resource, DbSet <E> targetDataSet)
            where E : Entity
        {
            var baseURL  = Settings.Default.APIBaseURL;
            var listURL  = $"{baseURL}/{resource.GetPath()}";
            var response = this._httpClient.GetAsync(listURL).Result;

            if (!response.IsSuccessStatusCode)
            {
                return(null);
            }

            var queryableDbSet = (IQueryable <Entity>)targetDataSet;
            var existingIds    = queryableDbSet.Select(row => row.APIID)
                                 .OrderBy(id => id)
                                 .ToList();
            var newIds = response.Content.ReadAsAsync <List <int> >().Result;

            return(newIds.Except(existingIds));
        }