public async Task <List <FipeMake> > GetMakes(FipeType type) { try { List <FipeMake> makes = new List <FipeMake>(); string url = /*await*/ UrlBuilder(_getMakesUrl, new string[] { _refTable, ((int)type).ToString() }); string jsonList = await WebFile.HttpRequestAsync(url); if (String.IsNullOrEmpty(jsonList)) { throw new WebException(JsonConvert.SerializeObject(new Relatório(2, "", "Sem acesso à internet"))); } else { if (jsonList.StartsWith("["))//tests if string is a json list (starts with '[')... "{\"codigo\":\"3\",\"erro\":\"Código fipe inválido\"}" , "{\"codigo\":\"0\",\"erro\":\"Nada encontrado\"}") { makes = JsonConvert.DeserializeObject <List <FipeMake> >(jsonList); } else { throw new WebException(jsonList); } } return(makes); } catch (Exception ex) { Relatório.ExHandler(ex); return(new List <FipeMake>()); } }
public async Task <List <FipeModel> > QueryYear_modelsByFipeCode(string code)//QueryByFipeCode(Tipo t, string code) { try { int numcode = -1; if (code == "" || !Int32.TryParse(code.Replace("-", ""), out numcode) || numcode == 0 || numcode > 9999999) { throw new WebException(JsonConvert.SerializeObject(new Relatório(3))); } code = numcode.ToString("000000-0"); List <FipeModel> models = await GetModels(code); if (models.Count == 0) { return(models); } List <Task <string> > requests = new List <Task <string> >(); foreach (FipeModel model in models) { string url = /*await*/ UrlBuilder(_queryByFipeCodeUrl, new string[] { _refTable, ((int)model.TipoVeiculo).ToString(), model.AnoModelo, model.TipoCombustivel, _vehicleTypes[(int)model.TipoVeiculo], code, _queryTypes[0] }); requests.Add(WebFile.HttpRequestAsync(url)); } string[] responses = await TaskEx.WhenAll(requests); if (responses.Length == 0 || String.IsNullOrEmpty(responses[0])) { throw new WebException(JsonConvert.SerializeObject(new Relatório(4))); } foreach (string json in responses) { if (Array.IndexOf(responses, json) == 0) //insert mockup in position 0 { FipeModel mockup = JsonConvert.DeserializeObject <FipeModel>(json); //mockup: "Selecione o ano/modelo:" mockup.Label = "Selecione o ano/modelo:"; mockup.Value = "0"; mockup.Valor = null; models.Insert(0, mockup); } FipeModel model = JsonConvert.DeserializeObject <FipeModel>(json); model.Label = models.ElementAt(Array.IndexOf(responses, json) + 1).Label; model.Value = models.ElementAt(Array.IndexOf(responses, json) + 1).Value; models[Array.IndexOf(responses, json) + 1] = model; } return(models); } catch (Exception ex) { Relatório.ExHandler(ex); return(new List <FipeModel>()); } }
private async Task <List <FipeModel> > GetModels(string code) { try { string url = /*await*/ UrlBuilder(_getModelsUrl, new string[] { _refTable, "1", _vehicleTypes[1], code, _queryTypes[0] }); //if (_refTable == "0") // throw new WebException(JsonConvert.SerializeObject(new Relatório(0, "Fipe indisponível"))); List <Task <string> > requests = new List <Task <string> >(); foreach (int type in Enum.GetValues(typeof(FipeType)).OfType <object>().Where(o => (int)o > 0))//Autodetection FipeType { url = /*await*/ UrlBuilder(_getModelsUrl, new string[] { _refTable, type.ToString(), _vehicleTypes[type], code, _queryTypes[0] }); requests.Add(WebFile.HttpRequestAsync(url)); } string[] responses = await TaskEx.WhenAll(requests); if (responses.Length == 0 || String.IsNullOrEmpty(responses[0]) || String.IsNullOrEmpty(responses[1]) || String.IsNullOrEmpty(responses[2])) { throw new WebException(JsonConvert.SerializeObject(new Relatório(2, "", "Sem acesso à internet"))); } List <FipeModel> models = new List <FipeModel>(); foreach (string jsonList in responses) { if (jsonList.StartsWith("["))//tests if string is a json list (stars with '[')... "{\"codigo\":\"3\",\"erro\":\"Código fipe inválido\"}" , "{\"codigo\":\"0\",\"erro\":\"Nada encontrado\"}") { models = JsonConvert.DeserializeObject <List <FipeModel> >(jsonList); foreach (FipeModel model in models) { model.TipoVeiculo = (FipeType)(Array.IndexOf(responses, jsonList) + 1); } } else { Relatório report = JsonConvert.DeserializeObject <Relatório>(jsonList); if (report.Codigo != 0) { throw new WebException(jsonList); } } } if (models.Count == 0) { throw new WebException(JsonConvert.SerializeObject(new Relatório(3))); } else { return(models); } } catch (Exception ex) { Relatório.ExHandler(ex); return(new List <FipeModel>()); } }