private static void SetAPISpecs() { if (_apiSpecs.Items.Count == 0) { string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "/Content/SpecList.json"; if (File.Exists(path)) { if (new FileInfo(path).Length != 0) { Console.WriteLine("Reading SpecList"); using (var reader = new StreamReader(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "/Content/SpecList.json")) { var serializer = new JsonSerializer() { ContractResolver = DefaultJsonContractResolver }; var specList = (List <GW2APISpec>)serializer.Deserialize(reader, typeof(List <GW2APISpec>)); _apiSpecs = new APISpecs(specList); reader.Close(); } } } } return; }
public static List <int> WriteAPISpecsToFile() { FileStream fcreate = File.Open(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "/Content/SpecList.json", FileMode.Create); fcreate.Close(); Console.WriteLine("Getting API"); //Get list from API GetAPIClient(); var specList = new List <GW2APISpec>(); HttpResponseMessage response = APIClient.GetAsync(new Uri("/v2/specializations", UriKind.Relative)).Result; int[] idArray; var failedList = new List <int>(); if (response.IsSuccessStatusCode) { // Get Skill ID list idArray = JsonConvert.DeserializeObject <int[]>(response.Content.ReadAsStringAsync().Result); foreach (int id in idArray) { GW2APISpec curSpec = GetGW2APISpec("/v2/specializations/" + id); if (curSpec != null) { specList.Add(curSpec); } else { Console.WriteLine("Failed to get response");//fail to retrieve failedList.Add(id); } } var writer = new StreamWriter(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "/Content/SpecList.json"); var serializer = new JsonSerializer { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.None, ContractResolver = DefaultJsonContractResolver }; serializer.Serialize(writer, specList); writer.Close(); } _apiSpecs = new APISpecs(specList); return(failedList); }