示例#1
0
        private async Task <MalRecResults <IEnumerable <IRecommendation> > > GetMalRecommendationsAsync(Operation <GetMalRecsRequest> operation, TimeSpan timeout, CancellationToken cancellationToken)
        {
            GetMalRecsResponse response = await DoOperationWithResponseBodyAsync <GetMalRecsResponse>(
                operation, timeout, cancellationToken, descriptionForErrors : "getting recommendations").ConfigureAwait(false);

            // This should be set as if we were running against an in-process rec source.
            // So it should be an IEnumerable<AverageScoreRecommendation> if getting recs from an AverageScore rec source, etc.
            // MalRecResultsExtensions.cs contains extension methods for "casting" MalRecResults to a strongly-typed MalRecResults.
            IEnumerable <IRecommendation> results;

            Type responseType = response.GetType();
            // check if m_responseToRecs implements IResponseToRecsConverter<responseType>
            Type converterInterfaceOpenType   = typeof(IResponseToRecsConverter <>);
            Type converterInterfaceClosedType = converterInterfaceOpenType.MakeGenericType(responseType);

            if (converterInterfaceClosedType.IsInstanceOfType(m_responseToRecs))
            {
                // Good, we have a handler.
                MethodInfo converterMethod     = converterInterfaceClosedType.GetMethod(ResponseToRecsConverter.ConvertMethodName);
                object     conversionResultObj = converterMethod.Invoke(m_responseToRecs, new object[] { response });
                results = (IEnumerable <IRecommendation>)conversionResultObj;
            }
            else
            {
                // Fallback. This will throw an exception if the recommendation DTO type is registered in the DTO assembly
                // but no handler is registered in this assembly.
                GetMalRecsResponse <DTO.Recommendation> specificResponse = (GetMalRecsResponse <DTO.Recommendation>)response;
                List <IRecommendation> recommendations = new List <IRecommendation>();
                foreach (DTO.Recommendation dtoRec in specificResponse.Recommendations)
                {
                    recommendations.Add(new BasicRecommendation(dtoRec.MalAnimeId));
                }

                results = recommendations;
            }

            Dictionary <int, RecEngine.MAL.MalAnime> animes = new Dictionary <int, RecEngine.MAL.MalAnime>();

            foreach (DTO.MalAnime dtoAnime in response.Animes)
            {
                animes[dtoAnime.MalAnimeId] = new RecEngine.MAL.MalAnime(dtoAnime.MalAnimeId, dtoAnime.MalAnimeType, dtoAnime.Title);
            }

            MalRecResults <IEnumerable <IRecommendation> > resultsWithAnimes = new MalRecResults <IEnumerable <IRecommendation> >(results, animes, response.RecommendationType);

            return(resultsWithAnimes);
        }
示例#2
0
 public static MalRecResults <IEnumerable <AnimeRecs.RecEngine.RatingPredictionRecommendation> > AsRatingPredictionResults(this MalRecResults <IEnumerable <IRecommendation> > basicResults)
 {
     if (basicResults.RecommendationType.Equals(RecommendationTypes.RatingPrediction, StringComparison.OrdinalIgnoreCase))
     {
         IEnumerable <AnimeRecs.RecEngine.RatingPredictionRecommendation> results = (IEnumerable <AnimeRecs.RecEngine.RatingPredictionRecommendation>)basicResults.Results;
         return(new MalRecResults <IEnumerable <RecEngine.RatingPredictionRecommendation> >(results, basicResults.AnimeInfo, basicResults.RecommendationType));
     }
     else
     {
         return(null);
     }
 }
示例#3
0
 public static MalRecResults <RecEngine.MAL.MalAnimeRecsResults> AsAnimeRecsResults(this MalRecResults <IEnumerable <IRecommendation> > basicResults)
 {
     if (basicResults.RecommendationType.Equals(RecommendationTypes.AnimeRecs, StringComparison.OrdinalIgnoreCase))
     {
         RecEngine.MAL.MalAnimeRecsResults results = (RecEngine.MAL.MalAnimeRecsResults)basicResults.Results;
         return(new MalRecResults <RecEngine.MAL.MalAnimeRecsResults>(results, basicResults.AnimeInfo, basicResults.RecommendationType));
     }
     else
     {
         return(null);
     }
 }
示例#4
0
 public static MalRecResults <T> CastRecs <T>(this MalRecResults <IEnumerable <IRecommendation> > basicResults)
     where T : IEnumerable <IRecommendation>
 {
     return(new MalRecResults <T>((T)basicResults.Results, basicResults.AnimeInfo, basicResults.RecommendationType));
 }