Skip to content

Commit

Permalink
docs: setup nasa api example
Browse files Browse the repository at this point in the history
  • Loading branch information
areknoster committed Mar 11, 2024
1 parent 2e5f5e4 commit 33d4d75
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 2 deletions.
3 changes: 2 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"testing"
)

const RecordModeEnv = "HTTTTEST_RECORD_MODE"
const RecordModeEnv = "HTTTEST_RECORD_MODE"

type config struct {
isRecordMode bool
Expand Down Expand Up @@ -52,6 +52,7 @@ func callerDir() string {
// NewDefaultTestClient returns a new http.Client that will either dump requests to the given dir or read them from it.
// It can be used to record and replay http requests.
func NewDefaultTestClient(t *testing.T, opts ...Option) *http.Client {
t.Helper()
scheme, err := NewSequentialNamingScheme(
path.Join(callerDir(), "testdata", t.Name()),
)
Expand Down
61 changes: 61 additions & 0 deletions examples/nasa/nasa_images.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package nasa

import (
"context"
"encoding/json"
"net/http"
"net/url"
"time"
)

type ImagesLister struct {
client *http.Client
apiKey string
}

func NewImagesLister(c *http.Client, apiKey string) *ImagesLister {
return &ImagesLister{
client: c,
apiKey: apiKey,
}
}

type listImagesResponse []struct {
URL string `json:"url"`
}

func (il *ImagesLister) ListNASAImages(ctx context.Context, from, to time.Time) ([]string, error) {
q := url.Values{
"api_key": {il.apiKey},
"date": {from.Format("2006-01-02")},
"end_date": {to.Format("2006-01-02")},
}
u := &url.URL{
Scheme: "https",
Host: "api.nasa.gov",
Path: "/planetary/apod",
RawQuery: q.Encode(),
}

req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
}

resp, err := il.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

var images listImagesResponse
if err := json.NewDecoder(resp.Body).Decode(&images); err != nil {
return nil, err
}

var urls []string
for _, img := range images {
urls = append(urls, img.URL)
}
return urls, nil
}
22 changes: 22 additions & 0 deletions examples/nasa/nasa_images_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package nasa

import (
"context"
"github.com/areknoster/htttest"
"testing"
"time"
)

func TestImagesLister_ListNASAImages(t *testing.T) {
httpClient := htttest.NewDefaultTestClient(t)
lister := NewImagesLister(httpClient, "DEMO_KEY")
d1 := time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC)
d2 := d1.Add(4 * 24 * time.Hour)
images, err := lister.ListNASAImages(context.Background(), d1, d2)
if err != nil {
t.Fatal(err)
}
if len(images) != 4 {
t.Fatal("expected at least one image")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
GET https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY&date=2021-01-01&end_date=2021-01-05 HTTP/1.1
Host: api.nasa.gov
User-Agent: Go-http-client/1.1

19 changes: 19 additions & 0 deletions examples/nasa/testdata/TestImagesLister_ListNASAImages/0.resp.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
HTTP/2.0 400 Bad Request
Content-Length: 208
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: X-RateLimit-Limit, X-RateLimit-Remaining
Age: 0
Content-Type: application/json
Date: Sun, 10 Mar 2024 21:53:05 GMT
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
Via: https/1.1 api-umbrella (ApacheTrafficServer [cMsSf ])
X-Api-Umbrella-Request-Id: ce59demoqkssuqvt6g70
X-Cache: MISS
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-Ratelimit-Limit: 40
X-Ratelimit-Remaining: 37
X-Vcap-Request-Id: ab8d26db-4ad1-4e45-5b01-4943c2930553
X-Xss-Protection: 1; mode=block

{"code":400,"msg":"Bad Request: invalid field combination passed. Allowed request fields for apod method are 'concept_tags', 'date', 'hd', 'count', 'start_date', 'end_date', 'thumbs'","service_version":"v1"}
2 changes: 1 addition & 1 deletion naming_scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type SequentialNamingScheme struct {
}

func NewSequentialNamingScheme(dir string) (*SequentialNamingScheme, error) {
err := os.MkdirAll(dir, 0660)
err := os.MkdirAll(dir, 0760)
if err != nil {
return nil, fmt.Errorf("error creating directory: %w", err)
}
Expand Down

0 comments on commit 33d4d75

Please sign in to comment.