-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e5f5e4
commit 33d4d75
Showing
6 changed files
with
109 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
examples/nasa/testdata/TestImagesLister_ListNASAImages/0.req.http
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
examples/nasa/testdata/TestImagesLister_ListNASAImages/0.resp.http
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters