SWapiCSharp 1.2.0

An package that works with https://swapi.dev/

Install-Package SWapiCSharp -Version 1.2.0
dotnet add package SWapiCSharp --version 1.2.0
<PackageReference Include="SWapiCSharp" Version="1.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SWapiCSharp --version 1.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

SWapi-CSharp

Helper library for consuming data from https://swapi.dev/

Build status
Coverage Status

Install

  • Install from Nuget using the command in package manager console:

    PM> Install-Package SWapiCSharp

Entities

  • Film

    • string Title
    • string EpisodeId
    • string OpeningCrawl
    • string Director
    • string Producer
    • string ReleaseDate
    • ICollection&lt;string&gt; Species
    • ICollection&lt;string&gt; Starships
    • ICollection&lt;string&gt; Vehicles
    • ICollection&lt;string&gt; Characters
    • ICollection&lt;string&gt; Planets
  • Pesron

    • string Name
    • string BirthYear
    • string EyeColor
    • string Gender
    • string HairColo
    • string Height
    • string Mass
    • string SkinColor
    • string Homeworld
    • ICollection&lt;string&gt; Films
    • ICollection&lt;string&gt; Species
    • ICollection&lt;string&gt; Starships
    • ICollection&lt;string&gt; Vehicles
  • Planet

    • string Name
    • string Diameter
    • string RotationPeriod
    • string OrbitalPeriod
    • string Gravity
    • string Climate
    • string Terrain
    • string SurfaceWater
    • ICollection&lt;string&gt; Residents
    • ICollection&lt;string&gt; Films
  • Specie

    • string Name
    • string Classification
    • string Designation
    • string AverageHeight
    • string AverageLifespan
    • string EyeColors
    • string HairColors
    • string SkinColors
    • string Language
    • string Homeworld
    • ICollection&lt;string&gt; People
    • ICollection&lt;string&gt; Films
  • Starship

    • string Name
    • string Model
    • string StarshipClass
    • string Manufacturer
    • string CostInCredits
    • string Length
    • string Crew
    • string Passengers
    • string MaxAtmospheringSpeed
    • string HyperdriveRating
    • string MegaLights
    • string CargoCapacity
    • string Consumables
    • ICollection&lt;string&gt; Films
    • ICollection&lt;string&gt; Pilots
  • Vehicle

    • string Name
    • string Model
    • string VehicleClass
    • string Manufacturer
    • string Length
    • string CostInCredits
    • string Crew
    • string Passengers
    • string MaxAtmospheringSpeed
    • string CargoCapacity
    • string Consumables
    • ICollection&lt;string&gt; Films
    • ICollection&lt;string&gt; Pilots

All of entities inherits BaseEntity class which has following properties:

  • string Url
  • DateTime Created
  • DateTime Edited

So after you have an instance of one entity type, you can get his base properties:

Console.WriteLine(person.Created);

<a href="./Structure.png">
<img src="./Structure.png" width="750" />
</a>

Examples

Using Repository&lt;T&gt; 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:

  1. GetById(int id)

    If the entity with passed id doesn'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 Vehicle has collection of Films, 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 unknown or n/a as 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));
    }
    
  2. 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 GetEntities without 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);    
    
  3. 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

SWapi-CSharp

Helper library for consuming data from https://swapi.dev/

Build status
Coverage Status

Install

  • Install from Nuget using the command in package manager console:

    PM> Install-Package SWapiCSharp

Entities

  • Film

    • string Title
    • string EpisodeId
    • string OpeningCrawl
    • string Director
    • string Producer
    • string ReleaseDate
    • ICollection&lt;string&gt; Species
    • ICollection&lt;string&gt; Starships
    • ICollection&lt;string&gt; Vehicles
    • ICollection&lt;string&gt; Characters
    • ICollection&lt;string&gt; Planets
  • Pesron

    • string Name
    • string BirthYear
    • string EyeColor
    • string Gender
    • string HairColo
    • string Height
    • string Mass
    • string SkinColor
    • string Homeworld
    • ICollection&lt;string&gt; Films
    • ICollection&lt;string&gt; Species
    • ICollection&lt;string&gt; Starships
    • ICollection&lt;string&gt; Vehicles
  • Planet

    • string Name
    • string Diameter
    • string RotationPeriod
    • string OrbitalPeriod
    • string Gravity
    • string Climate
    • string Terrain
    • string SurfaceWater
    • ICollection&lt;string&gt; Residents
    • ICollection&lt;string&gt; Films
  • Specie

    • string Name
    • string Classification
    • string Designation
    • string AverageHeight
    • string AverageLifespan
    • string EyeColors
    • string HairColors
    • string SkinColors
    • string Language
    • string Homeworld
    • ICollection&lt;string&gt; People
    • ICollection&lt;string&gt; Films
  • Starship

    • string Name
    • string Model
    • string StarshipClass
    • string Manufacturer
    • string CostInCredits
    • string Length
    • string Crew
    • string Passengers
    • string MaxAtmospheringSpeed
    • string HyperdriveRating
    • string MegaLights
    • string CargoCapacity
    • string Consumables
    • ICollection&lt;string&gt; Films
    • ICollection&lt;string&gt; Pilots
  • Vehicle

    • string Name
    • string Model
    • string VehicleClass
    • string Manufacturer
    • string Length
    • string CostInCredits
    • string Crew
    • string Passengers
    • string MaxAtmospheringSpeed
    • string CargoCapacity
    • string Consumables
    • ICollection&lt;string&gt; Films
    • ICollection&lt;string&gt; Pilots

All of entities inherits BaseEntity class which has following properties:

  • string Url
  • DateTime Created
  • DateTime Edited

So after you have an instance of one entity type, you can get his base properties:

Console.WriteLine(person.Created);

<a href="./Structure.png">
<img src="./Structure.png" width="750" />
</a>

Examples

Using Repository&lt;T&gt; 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:

  1. GetById(int id)

    If the entity with passed id doesn'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 Vehicle has collection of Films, 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 unknown or n/a as 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));
    }
    
  2. 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 GetEntities without 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);    
    
  3. 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

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.2.0 121 5/25/2020
1.0.0.1 683 9/21/2016