github.com/wtfutil/wtf@v0.43.0/modules/spacex/client.go (about)

     1  package spacex
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/wtfutil/wtf/utils"
     7  )
     8  
     9  const (
    10  	spacexLaunchAPI = "https://api.spacexdata.com/v3/launches/next"
    11  )
    12  
    13  type Launch struct {
    14  	FlightNumber int        `json:"flight_number"`
    15  	MissionName  string     `json:"mission_name"`
    16  	LaunchDate   int64      `json:"launch_date_unix"`
    17  	IsTentative  bool       `json:"tentative"`
    18  	Rocket       Rocket     `json:"rocket"`
    19  	LaunchSite   LaunchSite `json:"launch_site"`
    20  	Links        Links      `json:"links"`
    21  	Details      string     `json:"details"`
    22  }
    23  
    24  type LaunchSite struct {
    25  	Name string `json:"site_name_long"`
    26  }
    27  
    28  type Rocket struct {
    29  	Name string `json:"rocket_name"`
    30  }
    31  
    32  type Links struct {
    33  	RedditLink  string `json:"reddit_campaign"`
    34  	YouTubeLink string `json:"video_link"`
    35  }
    36  
    37  func NextLaunch() (*Launch, error) {
    38  	resp, err := http.Get(spacexLaunchAPI)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  	defer func() { _ = resp.Body.Close() }()
    43  
    44  	var data Launch
    45  	err = utils.ParseJSON(&data, resp.Body)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	return &data, nil
    51  }