public async Task <PlanetResidents> Run(
            [OrchestrationTrigger] DurableOrchestrationContextBase context,
            ILogger log)
        {
            var planetName = context.GetInput <string>();

            var result = new PlanetResidents();

            var planetResult = await context.CallActivityAsync <Planet>(
                nameof(SearchPlanetActivity),
                planetName);

            if (planetResult != null)
            {
                result.PlanetName = planetResult.Name;

                var tasks = new List <Task <string> >();
                foreach (var residentUrl in planetResult.ResidentUrls)
                {
                    tasks.Add(
                        context.CallActivityAsync <string>(
                            nameof(GetCharacterActivity),
                            residentUrl)
                        );
                }

                await Task.WhenAll(tasks);

                result.Residents = tasks.Select(task => task.Result).ToArray();
            }

            return(result);
        }
Ejemplo n.º 2
0
        public static PlanetResidents GetPlanetResidents(string planetName)
        {
            var apiResponse = MakeAPICall($"{EndPoint}/planets/?search={planetName}").Result;

            if (apiResponse != null)
            {
                var allPlanets = JsonConvert.DeserializeObject <AllPlanets>(apiResponse);
                while (allPlanets.Next != null)
                {
                    apiResponse = MakeAPICall($"{allPlanets.Next}").Result;
                    allPlanets.Conjoin(JsonConvert.DeserializeObject <AllPlanets>(apiResponse));
                }

                var planet = allPlanets.Results.FirstOrDefault(p => p.Name == planetName);
                if (planet != null)
                {
                    var planetResidents = new PlanetResidents();
                    var allResidents    = new List <SingleResident>();
                    planet.Residents.ForEach(r =>
                    {
                        apiResponse = MakeAPICall(r).Result;
                        if (apiResponse != null)
                        {
                            allResidents.Add(JsonConvert.DeserializeObject <SingleResident>(apiResponse));
                        }
                    });

                    planetResidents.Residents.AddRange(allResidents);
                    return(planetResidents);
                }
                ;
            }
            return(null);
        }