private async Task <IList <Weapon> > GetWeaponsAsync(Task <string> contentProvider, WeaponType weaponType) { Stopwatch sw = Instrumentation.BeginNetworkMeasure(); string content = await contentProvider; Instrumentation.EndNetworkMeasure(sw); sw = Instrumentation.BeginProcessingMeasure(); IList <Weapon> result = ProcessContent(content, weaponType); Instrumentation.EndProcessingMeasure(sw); return(result); }
public async Task <IList <Weapon> > ProduceWeaponsAsync() { var httpClient = new HttpClient() { Timeout = TimeSpan.FromSeconds(3.0) }; var result = new List <Weapon>(); var tasks = new Task <IList <Weapon> >[] { CreateWeapons(httpClient, "great-sword"), CreateWeapons(httpClient, "long-sword"), CreateWeapons(httpClient, "sword-and-shield"), CreateWeapons(httpClient, "dual-blades"), CreateWeapons(httpClient, "hammer"), CreateWeapons(httpClient, "hunting-horn"), CreateWeapons(httpClient, "lance"), CreateWeapons(httpClient, "gunlance"), CreateWeapons(httpClient, "switch-axe"), CreateWeapons(httpClient, "charge-blade"), CreateWeapons(httpClient, "insect-glaive"), }; Stopwatch sw = Instrumentation.BeginProcessingMeasure(); try { await Task.WhenAll(tasks); foreach (Task <IList <Weapon> > task in tasks) { result.AddRange(task.Result); } } catch (TaskCanceledException) { throw new TaskCanceledException($"{Name} took too long to answer."); } Instrumentation.EndProcessingMeasure(sw); return(result); }
private async Task <IList <Weapon> > CreateWeapons(HttpClient httpClient, string weaponType) { Stopwatch sw = Instrumentation.BeginNetworkMeasure(); string content = await httpClient.GetStringAsync($"https://mhw-db.com/weapons?q={{%22type%22:%22{weaponType}%22}}&p={{%22crafting%22:false,%22assets%22:false}}"); Instrumentation.EndNetworkMeasure(sw); sw = Instrumentation.BeginProcessingMeasure(); var inputWeapons = (JArray)JsonConvert.DeserializeObject(content); WeaponType typedWeaponType = ConvertWeaponType(weaponType); var outputWeapons = new List <Weapon>(); for (int i = 0; i < inputWeapons.Count; i++) { outputWeapons.Add(CreateWeapon((JObject)inputWeapons[i], typedWeaponType)); } Instrumentation.EndProcessingMeasure(sw); return(outputWeapons); }