Skip to content

Commit d50062c

Browse files
committed
tojson nil check; example allows apikey
1 parent 6f33840 commit d50062c

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

example/main.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,29 @@ package main
22

33
import (
44
"fmt"
5+
"os"
56
"strconv"
67

78
listennotes "github.com/ListenNotes/podcast-api-go"
89
)
910

1011
func main() {
11-
client := listennotes.NewClient("")
12+
apiKey := os.Getenv("LISTEN_API_KEY")
13+
14+
client := listennotes.NewClient(apiKey)
1215

1316
// The test data will return the same page each time, but this is an example of getting the next_offset out fo the resulting payload
1417
nextOffset := fetchAndOutputPage(client, 0)
1518
fetchAndOutputPage(client, nextOffset)
1619

1720
// You can get the output json easily as well:
1821
fmt.Printf("\nRegions:\n")
19-
regions, _ := client.FetchPodcastRegions(nil)
20-
fmt.Println(regions.ToJSON())
22+
regions, err := client.FetchPodcastRegions(nil)
23+
if err != nil {
24+
fmt.Printf("Failed reading regions: %s\n", err)
25+
} else {
26+
fmt.Println(regions.ToJSON())
27+
}
2128
}
2229

2330
func fetchAndOutputPage(client listennotes.HTTPClient, offset int) int {
@@ -48,8 +55,8 @@ func fetchAndOutputPage(client listennotes.HTTPClient, offset int) int {
4855
return nextOffset
4956
}
5057

51-
// searchResults, _ := client.Search(map[string]string {"q": "star wars"});
58+
// searchResults, err := client.Search(map[string]string {"q": "star wars"});
5259
// fmt.Println(searchResults.ToJSON())
5360

54-
// typeaheadResults, _ := client.Typeahead(map[string]string {"q": "star wars"});
61+
// typeaheadResults, err := client.Typeahead(map[string]string {"q": "star wars"});
5562
// fmt.Println(typeaheadResults.ToJSON())

execute.go

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ type Response struct {
2020
// Note: JSON marshal errors are swallowed here on purpose. This is for ease of use.
2121
// Considering this data marshalled from JSON, the risk here is low. On failure, "" will be returned.
2222
func (r *Response) ToJSON() string {
23+
if r == nil {
24+
return ""
25+
}
2326
jsonResult, err := json.Marshal(r.Data)
2427
if err != nil {
2528
log.Printf("failed to marshal response data to json: %s", err)

execute_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,13 @@ func TestResponseJSON(t *testing.T) {
189189
t.Errorf("ToJSON had unexpected result: '%s'", j)
190190
}
191191
}
192+
func TestNilResponseJSON(t *testing.T) {
193+
var resp *Response
194+
j := resp.ToJSON()
195+
if j != "" {
196+
t.Errorf("ToJSON had unexpected respons: '%s'", j)
197+
}
198+
}
192199

193200
func TestResponseJSONParseFailure(t *testing.T) {
194201
resp := Response{

0 commit comments

Comments
 (0)