SWapiCSharp 1.2.0

dotnet add package SWapiCSharp --version 1.2.0                
NuGet\Install-Package SWapiCSharp -Version 1.2.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<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                
#r "nuget: SWapiCSharp, 1.2.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SWapiCSharp as a Cake Addin
#addin nuget:?package=SWapiCSharp&version=1.2.0

// Install SWapiCSharp as a Cake Tool
#tool nuget:?package=SWapiCSharp&version=1.2.0                

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<string> Species
    • ICollection<string> Starships
    • ICollection<string> Vehicles
    • ICollection<string> Characters
    • ICollection<string> Planets
  • Pesron

    • string Name
    • string BirthYear
    • string EyeColor
    • string Gender
    • string HairColo
    • string Height
    • string Mass
    • string SkinColor
    • string Homeworld
    • ICollection<string> Films
    • ICollection<string> Species
    • ICollection<string> Starships
    • ICollection<string> Vehicles
  • Planet

    • string Name
    • string Diameter
    • string RotationPeriod
    • string OrbitalPeriod
    • string Gravity
    • string Climate
    • string Terrain
    • string SurfaceWater
    • ICollection<string> Residents
    • ICollection<string> Films
  • Specie

    • string Name
    • string Classification
    • string Designation
    • string AverageHeight
    • string AverageLifespan
    • string EyeColors
    • string HairColors
    • string SkinColors
    • string Language
    • string Homeworld
    • ICollection<string> People
    • ICollection<string> 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<string> Films
    • ICollection<string> 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<string> Films
    • ICollection<string> 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<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:

  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

Product Compatible and additional computed target framework versions.
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.0 1,371 5/25/2020
1.0.0.1 2,644 9/21/2016