// Usable based on return value of the Class from the URL query (api/classes/{id}). public DndClass GetClassById(int classId) { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(CLASS_ENDPOINT + "/" + classId); request.ContentType = "application/json"; request.Method = "GET"; using (WebResponse response = request.GetResponse()) { Stream data = response.GetResponseStream(); StreamReader reader = new StreamReader(data); JObject parsedJson = JObject.Parse(reader.ReadToEnd()); return(new DndClass() { Name = HtmlUtil.Encode(HtmlUtil.Decode(parsedJson["name"].ToString())), HitDie = int.Parse(parsedJson["hit_die"].ToString()), Spellcaster = parsedJson["spellcasting"] != null }); } } catch (WebException e) { throw new DndApiException($"Web error when getting data from Dnd5e API. Status: {e.Status}. Message: + {e.Message}"); } catch (Exception e) when(e is InvalidCastException || e is FormatException || e is ArgumentException || e is JsonReaderException) { throw new DndApiException($"Unable to retrieve specified character class from Dnd5e API: {e.Message}"); } }
public Dictionary <string, int> GetRaceOrClassesNameIdList(bool isRaceRequest) { string endpoint = isRaceRequest ? RACE_ENDPOINT : CLASS_ENDPOINT; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint); request.ContentType = "application/json"; request.Method = "GET"; Dictionary <string, int> nameUrlList = new Dictionary <string, int>(); using (WebResponse response = request.GetResponse()) { Stream data = response.GetResponseStream(); StreamReader reader = new StreamReader(data); foreach (JObject item in JObject.Parse(reader.ReadToEnd())["results"]) { string tempName = HtmlUtil.Encode(HtmlUtil.Decode(item["name"].ToString())); string tempUrl = item["url"].ToString(); // Protect against server spoofing of URL. Replaces expected endpoint from returned URL, interacts with "ids" instead. // StringComparison.Ordinal to ensure correct response when system is using a different locale. if (tempUrl.StartsWith(endpoint, StringComparison.Ordinal)) { tempUrl = tempUrl.Replace(endpoint, ""); nameUrlList.Add(tempName, int.Parse(tempUrl)); } else { throw new DndApiException("URL returned from dnd5e API using the wrong format. Server may have been spoofed."); } } return(nameUrlList); } } catch (WebException e) { throw new DndApiException($"Web error when getting data from Dnd5e API. Status: {e.Status}. Message: + {e.Message}"); } catch (Exception e) when(e is IOException || e is InvalidCastException || e is DndApiException || e is FormatException || e is ArgumentException || e is JsonReaderException) { throw new DndApiException($"Unable to retrieve class/race list from Dnd5e API: {e.Message}"); } }
// Usable based on return value of the Race from the URL query (api/races/{id}). public DndRace GetRaceById(int raceId) { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(RACE_ENDPOINT + "/" + raceId); request.ContentType = "application/json"; request.Method = "GET"; using (WebResponse response = request.GetResponse()) { Stream data = response.GetResponseStream(); StreamReader reader = new StreamReader(data); string jsonString = reader.ReadToEnd(); if (jsonString != "null") { JObject parsedJson = JObject.Parse(jsonString); return(new DndRace() { Name = HtmlUtil.Encode(HtmlUtil.Decode(parsedJson["name"].ToString())), RacialBonuses = parsedJson["ability_bonuses"].Select(x => (int)x).ToArray() }); } throw new DndApiException("Dnd5e api returned null. Requested race does not exist."); } } catch (WebException e) { throw new DndApiException($"Web error when getting data from Dnd5e API. Status: {e.Status}. Message: + {e.Message}"); } catch (Exception e) when(e is InvalidCastException || e is FormatException || e is ArgumentException || e is JsonReaderException) { throw new DndApiException($"Unable to specified character race from Dnd5e API: {e.Message}"); } }