44 "encoding/json"
55 "fmt"
66 "net/http"
7+ "os"
78 "time"
89
910 "github.com/rs/zerolog/log"
@@ -18,22 +19,27 @@ type NASAClient struct {
1819
1920// Response structures for NASA API
2021type APOD struct {
21- Title string `json:"title"`
22- Date string `json:"date"`
23- Explanation string `json:"explanation"`
24- URL string `json:"url"`
25- MediaType string `json:"media_type"`
22+ Title string `json:"title"`
23+ Date string `json:"date"`
24+ Explanation string `json:"explanation"`
25+ URL string `json:"url"`
26+ MediaType string `json:"media_type"`
2627 ServiceVersion string `json:"service_version"`
2728}
2829
29- // NewNASAClient creates a new NASA API client
30+ // NewNASAClient creates a new NASA API client.
31+ // Uses NASA_API_KEY env if set; otherwise falls back to DEMO_KEY (strict rate limits).
3032func NewNASAClient () * NASAClient {
33+ apiKey := os .Getenv ("NASA_API_KEY" )
34+ if apiKey == "" {
35+ apiKey = "DEMO_KEY"
36+ }
3137 return & NASAClient {
3238 baseURL : "https://api.nasa.gov" ,
3339 httpClient : & http.Client {
3440 Timeout : time .Second * 10 ,
3541 },
36- apiKey : "DEMO_KEY" , // Using demo key for simplicity
42+ apiKey : apiKey ,
3743 }
3844}
3945
@@ -93,12 +99,12 @@ func (c *NASAClient) GetAPOD() (*APOD, error) {
9399 return nil , fmt .Errorf ("NASA API rate limit exceeded" )
94100 }
95101 }
96-
102+
97103 // Also check for 429 status code (Too Many Requests)
98104 if resp .StatusCode == http .StatusTooManyRequests {
99105 return nil , fmt .Errorf ("NASA API rate limit exceeded (429)" )
100106 }
101-
107+
102108 // Check for other non-success status codes
103109 if resp .StatusCode != http .StatusOK {
104110 return nil , fmt .Errorf ("NASA API error: HTTP %d" , resp .StatusCode )
@@ -109,4 +115,4 @@ func (c *NASAClient) GetAPOD() (*APOD, error) {
109115 return nil , err
110116 }
111117 return & apod , nil
112- }
118+ }
0 commit comments