Helper library for consuming data from https://swapi.dev/
-
Install from Nuget using the command in package manager console:
PM>
Install-Package SWapiCSharp
-
FilmstringTitlestringEpisodeIdstringOpeningCrawlstringDirectorstringProducerstringReleaseDateICollection<string>SpeciesICollection<string>StarshipsICollection<string>VehiclesICollection<string>CharactersICollection<string>Planets
-
PesronstringNamestringBirthYearstringEyeColorstringGenderstringHairColostringHeightstringMassstringSkinColorstringHomeworldICollection<string>FilmsICollection<string>SpeciesICollection<string>StarshipsICollection<string>Vehicles
-
PlanetstringNamestringDiameterstringRotationPeriodstringOrbitalPeriodstringGravitystringClimatestringTerrainstringSurfaceWaterICollection<string>ResidentsICollection<string>Films
-
SpeciestringNamestringClassificationstringDesignationstringAverageHeightstringAverageLifespanstringEyeColorsstringHairColorsstringSkinColorsstringLanguagestringHomeworldICollection<string>PeopleICollection<string>Films
-
StarshipstringNamestringModelstringStarshipClassstringManufacturerstringCostInCreditsstringLengthstringCrewstringPassengersstringMaxAtmospheringSpeedstringHyperdriveRatingstringMegaLightsstringCargoCapacitystringConsumablesICollection<string>FilmsICollection<string>Pilots
-
VehiclestringNamestringModelstringVehicleClassstringManufacturerstringLengthstringCostInCreditsstringCrewstringPassengersstringMaxAtmospheringSpeedstringCargoCapacitystringConsumablesICollection<string>FilmsICollection<string>Pilots
All of entities inherits BaseEntity class which has following properties:
stringUrlDateTimeCreatedDateTimeEdited
So after you have an instance of one entity type, you can get his base properties:
Console.WriteLine(person.Created);
Using Repository<T> class:
IRepository<Starship> starshipRepo = new Repository<Starship>();
// The Starship class can be replaced with each other entity class: Person, Specie, Film, Vehicle or Planet.The repository has two main methods for consume entities:
-
GetById(int id)If the entity with passed
iddoesn't exist, repository will return null.Starship starshipDetails = starshipRepo.GetById(3); // Allays check for null!!! if(starshipDetails != null) { Console.WriteLine(starship.Name); } // C# 6.0 syntax Console.WriteLine(starship?.Name);
The
Vehiclehas collection ofFilms, but it's only URLs. So to access them you should manually done this:Vehicle vehicle = vehicleRepository.GetById(2); if (vehicle != null && vehicle.Films.Count > 0) { Console.WriteLine("Vehicle {0} has {1} films:", vehicle.Name, vehicle.Films.Count); foreach (var film in vehicle.Films) { // GetFilmId is a helper method just for extracting the id from the url. Example: https://swapi.dev/api/films/2/ - will return only 2. int filmId = this.GetFilmId(film); // getting related items should be done manual Film relatedFilm = filmRepository.GetById(filmId); Console.WriteLine(relatedFilm.Title); } }
Sometimes a property from entity can return
unknownorn/aas a value. So be careful when parsing properties to other type. There are many ways to validate it:Specie specie = specieRepository.GetById(5); int SpecialSpan = 2; // Check for specie != null { ... } if (specie.AverageLifespan != "unknown") { int lifeSpan = int.Parse(specie.AverageLifespan); Console.WriteLine("Life span: " + (lifeSpan + SpecialSpan)); } int lifeSpanAverage = 0; if (int.TryParse(specie.AverageLifespan, out lifeSpanAverage)) { // If parse is success, lifeSpanAverage will have parsed value. Console.WriteLine("Life span: " + (lifeSpanAverage + SpecialSpan)); }
-
GetEntities(int page = 1, int size = 10)By default size is from https://swapi.dev/api is 10. You can pass the page and size for getting entities, but you should know that if you want size 20 and page 3 it will start from 3-rd page and will collect entities from next pages until size is reached or next page is
null. So all pages have 10 entities and with parameters above, the method will return entities on page 3 and 4.Example with first five entities from second page:
var plnetsRepository = new Repository<Planet>(); var planets = plnetsRepository.GetEntities(2, 5); if (planets == null) { Console.WriteLine("There are no planets on page {0}", page); return; } foreach (var planet in planets) { Console.WriteLine("Name: " + planet.Name); Console.WriteLine("Terrain: " + planet.Terrain); }
If you call
GetEntitieswithout passing values, the method will return first ten entities from type in first page.To get all entities from type:
var films = filmsRepo.GetEntities(size: int.MaxValue);
-
Extending Entities:
Make a class which inherits the entity type to extend for example person.
public class MyPerson : Person { public override string ToString() { return this.Name + Environment.NewLine + "Birth year: " + this.BirthYear + Environment.NewLine + "Has " + this.Starships.Count + " starships"; } }
After that you can make a repository from
MyPerson:IRepository<MyPerson> peopleRepo = new Repository<MyPerson>(); MyPerson kenobi = peopleRepo.GetById(10); Console.WriteLine(kenobi.ToString()); /***** Will print: Obi-Wan Kenobi Birth year: 57BBY Has 5 starships */
All of the examples can be seen in the repository Example project.
SWApi documentation here
If you find a bug or something doesn't working submit here