Planet Methods - adampresley/swapi-go GitHub Wiki

The Planet methods return information about planets found in the Star Wars universe. Below are links to more information about the RESTful API, as well as documentation for the structures and each method.

Structures

type Planet struct {
	errors.Error

	Climate        string   `json:"climate"`
	Created        string   `json:"created"`
	Diameter       string   `json:"diameter"`
	Edited         string   `json:"edited"`
	Films          []string `json:"films"`
	Gravity        string   `json:"gravity"`
	Name           string   `json:"name"`
	OrbitalPeriod  string   `json:"orbital_period"`
	Population     string   `json:"population"`
	Residents      []string `json:"residents"`
	RotationPeriod string   `json:"rotation_period"`
	SurfaceWater   string   `json:"surface_water"`
	Terrain        string   `json:"terrain"`
	Url            string   `json:"url"`
}

type PlanetCollection struct {
	errors.Error

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

Methods

GetAllPlanets

Description: Returns a collection of Planet structures in the form of a PlanetCollection structure.

Returns: PlanetCollection structure, HTTP status code, error

Example:

package main

import (
	"log"

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

func main() {
	client := swapi.NewClient()
	planets, status, err := client.GetAllPlanets()

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

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

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

GetPlanetById

Description: Returns a single Planet structure by ID.

Returns: Planet structure, HTTP status code, error

Example:

package main

import (
	"log"

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

func main() {
	client := swapi.NewClient()
	planet, status, err := client.GetPlanetById(1)

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

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

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