Vehicle Methods - adampresley/swapi-go GitHub Wiki

The Vehicle methods return information about the different types of vehicles found in the Star Wars universe. A vehicle is defined as single transport craft that does not have hyperdrive capability. Below are links to more information about the RESTful API, as well as documentation for the structures and each method.

Structures

type Vehicle struct {
	errors.Error

	CargoCapacity        string   `json:"cargo_capacity"`
	Consumables          string   `json:"consumables"`
	CostInCredits        string   `json:"cost_in_credits"`
	Created              string   `json:"created"`
	Crew                 string   `json:"crew"`
	Edited               string   `json:"edited"`
	Length               string   `json:"length"`
	Manufacturer         string   `json:"manufacturer"`
	MaxAtmospheringSpeed string   `json:"max_atmostphering_speed"`
	Model                string   `json:"model"`
	Name                 string   `json:"name"`
	Passengers           string   `json:"passengers"`
	Films                []string `json:"films"`
	Pilots               []string `json:"pilots"`
	VehicleClass         string   `json:"vehicle_class"`
	Url                  string   `json:"url"`
}

type VehicleCollection struct {
	errors.Error

	collection.SWAPIResultCollection
	Results []Vehicle `json:"results"`
}

Methods

GetAllVehicles

Description: Returns a collection of Vehicle structures in the form of a VehicleCollection structure.

Returns: VehicleCollection structure, HTTP status code, error

Example:

package main

import (
	"log"

	"github.com/adampresley/swapi-go/swapi"
)

func main() {
	client := swapi.NewClient()
	vehicles, status, err := client.GetAllVehicles()

	if err != nil {
		log.Fatalf("Cannot get vehicles: %s", err.Error())
	}

	if status != 200 {
		log.Println("HTTP error with message", result.ErrorMessage)
	}

	/*
	 * Iterate over the results and do something useful
	 */
	for _, vehicle := range vehicles.Results {
		// useful
	}
}

GetVehicleById

Description: Returns a single Vehicle structure by ID.

Returns: Vehicle structure, HTTP status code, error

Example:

package main

import (
	"log"

	"github.com/adampresley/swapi-go/swapi"
)

func main() {
	client := swapi.NewClient()
	vehicle, status, err := client.GetVehicleById(1)

	if err != nil {
		log.Fatalf("Cannot get vehicle: %s", err.Error())
	}

	if status != 200 {
		log.Println("HTTP error with message", result.ErrorMessage)
	}

	// Do something useful with this vehicle
}
⚠️ **GitHub.com Fallback** ⚠️