|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | +) |
| 9 | + |
| 10 | +type AstronautAPIResponse struct { |
| 11 | + Count int `json:"count"` |
| 12 | + Next string `json:"next"` |
| 13 | + Prev string `json:"previous"` |
| 14 | + Results []Astronaut `json:"results"` |
| 15 | +} |
| 16 | + |
| 17 | +type Nationality struct { |
| 18 | + ID int `json:"id"` |
| 19 | + Name string `json:"name"` |
| 20 | + Alpha2Code string `json:"alpha_2_code"` |
| 21 | + Alpha3Code string `json:"alpha_3_code"` |
| 22 | + NationalityName string `json:"nationality_name"` |
| 23 | + NationalityNameComposed string `json:"nationality_name_composed"` |
| 24 | +} |
| 25 | + |
| 26 | +type AgencyDetail struct { |
| 27 | + ResponseMode string `json:"response_mode"` |
| 28 | + ID int `json:"id"` |
| 29 | + URL string `json:"url"` |
| 30 | + Name string `json:"name"` |
| 31 | + Abbrev string `json:"abbrev"` |
| 32 | + Type NamedObject `json:"type"` |
| 33 | +} |
| 34 | + |
| 35 | +type Astronaut struct { |
| 36 | + ID int `json:"id"` |
| 37 | + URL string `json:"url"` |
| 38 | + Name string `json:"name"` |
| 39 | + Status NamedObject `json:"status"` |
| 40 | + Agency AgencyDetail `json:"agency"` |
| 41 | + Type NamedObject `json:"type"` |
| 42 | + InSpace bool `json:"in_space"` |
| 43 | + TimeInSpace string `json:"time_in_space"` |
| 44 | + EvaTime string `json:"eva_time"` |
| 45 | + Age *int `json:"age"` |
| 46 | + DateOfBirth *string `json:"date_of_birth"` |
| 47 | + DateOfDeath *string `json:"date_of_death"` |
| 48 | + Nationality []Nationality `json:"nationality"` |
| 49 | + Bio string `json:"bio"` |
| 50 | + Wiki *string `json:"wiki"` |
| 51 | + LastFlight *string `json:"last_flight"` |
| 52 | + FirstFlight *string `json:"first_flight"` |
| 53 | + FlightsCount int `json:"flights_count"` |
| 54 | + LandingsCount int `json:"landings_count"` |
| 55 | + SpacewalksCount int `json:"spacewalks_count"` |
| 56 | +} |
| 57 | + |
| 58 | +type NamedObject struct { |
| 59 | + ID int `json:"id"` |
| 60 | + Name string `json:"name"` |
| 61 | +} |
| 62 | + |
| 63 | +func main() { |
| 64 | + astronauts, err := GetRecentAstronauts() |
| 65 | + if err != nil { |
| 66 | + fmt.Println("❌ Error:", err) |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + fmt.Println("🧑🚀 Most Recently Born Astronauts (Newest)") |
| 71 | + for _, a := range astronauts { |
| 72 | + fmt.Println("═══════════════════════════════════════════") |
| 73 | + fmt.Printf("👤 Name: %s\n", a.Name) |
| 74 | + |
| 75 | + // Handle nationality array |
| 76 | + nationalityStr := "Unknown" |
| 77 | + if len(a.Nationality) > 0 { |
| 78 | + nationalities := make([]string, len(a.Nationality)) |
| 79 | + for i, nat := range a.Nationality { |
| 80 | + nationalities[i] = nat.NationalityName |
| 81 | + } |
| 82 | + nationalityStr = fmt.Sprintf("%v", nationalities) |
| 83 | + if len(a.Nationality) == 1 { |
| 84 | + nationalityStr = a.Nationality[0].NationalityName |
| 85 | + } |
| 86 | + } |
| 87 | + fmt.Printf("🗺️ Nationality: %s\n", nationalityStr) |
| 88 | + |
| 89 | + // Handle nullable fields |
| 90 | + dob := "Unknown" |
| 91 | + if a.DateOfBirth != nil { |
| 92 | + dob = *a.DateOfBirth |
| 93 | + } |
| 94 | + fmt.Printf("🎂 DOB: %s\n", dob) |
| 95 | + |
| 96 | + dod := "N/A" |
| 97 | + if a.DateOfDeath != nil && *a.DateOfDeath != "" { |
| 98 | + dod = *a.DateOfDeath |
| 99 | + } |
| 100 | + fmt.Printf("☠️ DOD: %s\n", dod) |
| 101 | + |
| 102 | + age := "Unknown" |
| 103 | + if a.Age != nil { |
| 104 | + age = fmt.Sprintf("%d", *a.Age) |
| 105 | + } |
| 106 | + fmt.Printf("🎂 Age: %s\n", age) |
| 107 | + |
| 108 | + fmt.Printf("🧠 Status: %s | Type: %s | In Space Now: %t\n", a.Status.Name, a.Type.Name, a.InSpace) |
| 109 | + fmt.Printf("🏢 Agency: %s (%s)\n", a.Agency.Name, a.Agency.Abbrev) |
| 110 | + fmt.Printf("🚀 Flights: %d | Landings: %d | Spacewalks: %d\n", a.FlightsCount, a.LandingsCount, a.SpacewalksCount) |
| 111 | + fmt.Printf("🕒 Time in Space: %s | EVA Time: %s\n", a.TimeInSpace, a.EvaTime) |
| 112 | + |
| 113 | + firstFlight := "N/A" |
| 114 | + if a.FirstFlight != nil && *a.FirstFlight != "" { |
| 115 | + firstFlight = *a.FirstFlight |
| 116 | + } |
| 117 | + lastFlight := "N/A" |
| 118 | + if a.LastFlight != nil && *a.LastFlight != "" { |
| 119 | + lastFlight = *a.LastFlight |
| 120 | + } |
| 121 | + fmt.Printf("🛰️ First Flight: %s | Last Flight: %s\n", firstFlight, lastFlight) |
| 122 | + |
| 123 | + if a.Bio != "" { |
| 124 | + bioPreview := a.Bio |
| 125 | + if len(bioPreview) > 300 { |
| 126 | + bioPreview = bioPreview[:300] + "..." |
| 127 | + } |
| 128 | + fmt.Printf("📜 Bio: %s\n", bioPreview) |
| 129 | + } |
| 130 | + |
| 131 | + wiki := "N/A" |
| 132 | + if a.Wiki != nil && *a.Wiki != "" { |
| 133 | + wiki = *a.Wiki |
| 134 | + } |
| 135 | + fmt.Printf("🌐 Wiki: %s\n", wiki) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +func GetRecentAstronauts() ([]Astronaut, error) { |
| 140 | + url := "https://ll.thespacedevs.com/2.3.0/astronauts/?limit=3&ordering=-date_of_birth&mode=detailed&format=json" |
| 141 | + |
| 142 | + resp, err := http.Get(url) |
| 143 | + if err != nil { |
| 144 | + return nil, fmt.Errorf("failed to fetch: %w", err) |
| 145 | + } |
| 146 | + defer resp.Body.Close() |
| 147 | + |
| 148 | + if resp.StatusCode != http.StatusOK { |
| 149 | + body, _ := io.ReadAll(resp.Body) |
| 150 | + return nil, fmt.Errorf("API returned %d: %s", resp.StatusCode, string(body)) |
| 151 | + } |
| 152 | + |
| 153 | + var parsed AstronautAPIResponse |
| 154 | + if err := json.NewDecoder(resp.Body).Decode(&parsed); err != nil { |
| 155 | + return nil, fmt.Errorf("error decoding: %w", err) |
| 156 | + } |
| 157 | + |
| 158 | + return parsed.Results, nil |
| 159 | +} |
0 commit comments