Skip to content

Commit 00dbde5

Browse files
add api test and some fixes accross files
1 parent ffa22a9 commit 00dbde5

File tree

5 files changed

+43
-25
lines changed

5 files changed

+43
-25
lines changed

api.go

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,40 @@ type Response struct {
1313
}
1414

1515
func GetApiData(apiUrl string, bearerToken string) ([]Response, error) {
16-
client := &http.Client{}
17-
18-
req, err := http.NewRequest("GET", apiUrl, nil)
19-
if err != nil {
20-
return nil, err
21-
}
22-
23-
req.Header.Set("Authorization", "Bearer "+bearerToken)
24-
25-
resp, err := client.Do(req)
26-
if err != nil {
27-
return nil, err
28-
}
29-
defer resp.Body.Close()
30-
3116
var result []Response
32-
err = json.NewDecoder(resp.Body).Decode(&result)
33-
if err != nil {
34-
return nil, err
17+
18+
for apiUrl != "" {
19+
// Make API request
20+
client := &http.Client{}
21+
req, err := http.NewRequest("GET", apiUrl, nil)
22+
if err != nil {
23+
return nil, err
24+
}
25+
26+
req.Header.Set("Authorization", "Bearer "+bearerToken)
27+
28+
resp, err := client.Do(req)
29+
if err != nil {
30+
return nil, err
31+
}
32+
defer resp.Body.Close()
33+
34+
// Decode response body
35+
var response struct {
36+
Results []Response `json:"results"`
37+
NextUrl string `json:"nextUrl"`
38+
}
39+
40+
err = json.NewDecoder(resp.Body).Decode(&response)
41+
if err != nil {
42+
return nil, err
43+
}
44+
45+
// Append results to the overall result
46+
result = append(result, response.Results...)
47+
48+
// Update apiUrl for the next page
49+
apiUrl = response.NextUrl
3550
}
3651

3752
return result, nil

tests/api_test.go renamed to api_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"encoding/json"
5-
"github.com/mieh/api-to-csv/api"
65
"net/http"
76
"net/http/httptest"
87
"testing"
@@ -31,7 +30,6 @@ func TestGetApiData(t *testing.T) {
3130
defer server.Close()
3231

3332
// Make a request to the mock API server with a fake token
34-
token := "fake_token"
3533
response, err := http.Get(server.URL)
3634
if err != nil {
3735
t.Fatalf("Error making API request: %v", err)

go.mod

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ module github.com/mieh/api-to-csv
22

33
go 1.20
44

5-
require google.golang.org/api v0.110.0
5+
require (
6+
google.golang.org/api v0.110.0
7+
gopkg.in/yaml.v2 v2.4.0
8+
)
69

710
require (
811
cloud.google.com/go/compute v1.18.0 // indirect
@@ -21,5 +24,4 @@ require (
2124
google.golang.org/genproto v0.0.0-20230209215440-0dfe4f8abfcc // indirect
2225
google.golang.org/grpc v1.53.0 // indirect
2326
google.golang.org/protobuf v1.28.1 // indirect
24-
gopkg.in/yaml.v2 v2.4.0 // indirect
2527
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
2+
cloud.google.com/go v0.107.0 h1:qkj22L7bgkl6vIeZDlOY2po43Mx/TIa2Wsa7VR+PEww=
23
cloud.google.com/go/compute v1.18.0 h1:FEigFqoDbys2cvFkZ9Fjq4gnHBP55anJ0yQyau2f9oY=
34
cloud.google.com/go/compute v1.18.0/go.mod h1:1X7yHxec2Ga+Ss6jPyjxRxpu2uu7PLgsOVXvgU0yacs=
45
cloud.google.com/go/compute/metadata v0.2.3 h1:mg4jlk7mCAj6xXp9UJ4fjI9VUI5rubuGBW5aJ7UnBMY=
56
cloud.google.com/go/compute/metadata v0.2.3/go.mod h1:VAV5nSsACxMJvgaAuX6Pk2AawlZn8kiOGuCv6gTkwuA=
7+
cloud.google.com/go/longrunning v0.3.0 h1:NjljC+FYPV3uh5/OwWT6pVU+doBqMg2x/rZlE+CamDs=
68
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
79
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
810
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
@@ -37,6 +39,7 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
3739
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
3840
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
3941
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
42+
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
4043
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
4144
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
4245
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
@@ -123,6 +126,7 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
123126
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
124127
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
125128
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
129+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
126130
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
127131
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
128132
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"io/ioutil"
77
"log"
8-
"os"
98

109
"github.com/mieh/api-to-csv/output"
1110
"gopkg.in/yaml.v2"
@@ -60,15 +59,15 @@ func main() {
6059
}
6160

6261
// Convert response body to CSV string
63-
csvString, err := convertToCSV(responseBody)
62+
csvString, err := ConvertToCsv(responseBody)
6463
if err != nil {
6564
log.Fatalf("Error converting response body to CSV: %v", err)
6665
}
6766

6867
// Output the CSV string based on the outputType flag
6968
switch config.OutputType {
7069
case "toConsole":
71-
err = output.ToConsole()(csvString)
70+
err = output.ToConsole(csvString)
7271
if err != nil {
7372
log.Fatalf("Error outputting CSV to console: %v", err)
7473
}

0 commit comments

Comments
 (0)