Skip to content

Commit

Permalink
Refactor whilst I still have copilot access
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Birchall committed Dec 9, 2024
1 parent 6572d9f commit 25550de
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 7 deletions.
8 changes: 7 additions & 1 deletion downloadImage.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ import (
"os"
)

type ImageDownloader interface {
DownloadImage(url, dir, filename string) error
}

type RealImageDownloader struct{}

// DownloadImage downloads an image from a URL and saves it to a file in a subdirectory
func DownloadImage(url string, subdirectory string, filename string) error {
func (d RealImageDownloader) DownloadImage(url string, subdirectory string, filename string) error {
// Create the subdirectory if it doesn't exist
if _, err := os.Stat(subdirectory); os.IsNotExist(err) {
os.Mkdir(subdirectory, 0755)
Expand Down
9 changes: 7 additions & 2 deletions fetchAPOD.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ package main

import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
)

// FetchAPOD fetches the Astronomy Picture of the Day (APOD) from the NASA API
func FetchAPOD() (Response, error) {
func FetchAPOD(apiURL string) (Response, error) {
apiKey := os.Getenv("NASA_KEY")
url := "https://api.nasa.gov/planetary/apod?api_key=" + apiKey
if apiKey == "" {
return Response{}, fmt.Errorf("NASA_KEY environment variable not set")
}

url := apiURL + "?api_key=" + apiKey

resp, err := http.Get(url)
if err != nil {
Expand Down
40 changes: 40 additions & 0 deletions fetchAPOD_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)

func MockResponse(statusCode int, body string) *http.Response {
return &http.Response{
StatusCode: statusCode,
Body: io.NopCloser(strings.NewReader(body)),
}
}

func TestFetchAPOD(t *testing.T) {
os.Unsetenv("NASA_KEY")
_, err := FetchAPOD("https://api.nasa.gov/planetary/apod")
if err == nil || err.Error() != "NASA_KEY environment variable not set" {
t.Errorf("expected error 'NASA_KEY environment variable not set', got %v", err)
}

os.Setenv("NASA_KEY", "DEMO_KEY")
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"date":"2023-10-01","explanation":"Test explanation","media_type":"image","service_version":"v1","title":"Test Title","url":"https://example.com/test.jpg"}`))
}))
defer server.Close()

resp, err := FetchAPOD(server.URL)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if resp.Title != "Test Title" {
t.Errorf("expected title 'Test Title', got %v", resp.Title)
}
}
13 changes: 9 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,22 @@ type Response struct {
}

func main() {
downloader := RealImageDownloader{}
if err := run(downloader); err != nil {
panic(err)
}
}

result, err := FetchAPOD()
func run(downloader ImageDownloader) error {
result, err := FetchAPOD("https://api.nasa.gov/planetary/apod")
if err != nil {
panic(err)
return err
}

fmt.Println("Downloading image: " + result.Title)

filename := result.Title + ".jpg"
noSpaceFilename := strings.ReplaceAll(filename, " ", "_")

DownloadImage(result.Hdurl, "images", noSpaceFilename)

return downloader.DownloadImage(result.Hdurl, "images", noSpaceFilename)
}
36 changes: 36 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"net/http"
"net/http/httptest"
"os"
"testing"
)

type MockImageDownloader struct{}

func (d MockImageDownloader) DownloadImage(url, dir, filename string) error {
return nil
}

func TestRun(t *testing.T) {
downloader := MockImageDownloader{}

os.Unsetenv("NASA_KEY")
err := run(downloader)
if err == nil || err.Error() != "NASA_KEY environment variable not set" {
t.Errorf("expected error 'NASA_KEY environment variable not set', got %v", err)
}

os.Setenv("NASA_KEY", "DEMO_KEY")
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"date":"2023-10-01","explanation":"Test explanation","media_type":"image","service_version":"v1","title":"Test Title","url":"https://example.com/test.jpg"}`))
}))
defer server.Close()

err = run(downloader)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
}

0 comments on commit 25550de

Please # to comment.