From 6746c6dd7add9f3c4b49ac63823f0935aec961ff Mon Sep 17 00:00:00 2001 From: Guillermo Gutierrez Almazor Date: Fri, 26 Jan 2024 16:11:46 +0100 Subject: [PATCH] Change: update code and fixtures to reflect a change in the payload schema --- dnsimple/dns_analytics.go | 10 ++--- dnsimple/dns_analytics_test.go | 10 ++--- fixtures.http/api/dnsAnalytics/success.http | 2 +- main/main.go | 45 +++++++++++++++++++++ 4 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 main/main.go diff --git a/dnsimple/dns_analytics.go b/dnsimple/dns_analytics.go index ebbdd54..1337c4e 100644 --- a/dnsimple/dns_analytics.go +++ b/dnsimple/dns_analytics.go @@ -15,9 +15,9 @@ type DnsAnalyticsService struct { // DnsAnalytics represents DNS Analytics data. type DnsAnalytics struct { - Volume int64 - Zone string - Date string + Volume int64 + ZoneName string + Date string } type DnsAnalyticsQueryParameters struct { @@ -53,8 +53,8 @@ func (r *DnsAnalyticsResponse) marshalData() { switch header { case "volume": dataEntry.Volume = int64(row[j].(float64)) - case "zone": - dataEntry.Zone = row[j].(string) + case "zone_name": + dataEntry.ZoneName = row[j].(string) case "date": dataEntry.Date = row[j].(string) } diff --git a/dnsimple/dns_analytics_test.go b/dnsimple/dns_analytics_test.go index 12c0af9..1d2e12d 100644 --- a/dnsimple/dns_analytics_test.go +++ b/dnsimple/dns_analytics_test.go @@ -33,7 +33,7 @@ func TestDnsAnalyticsService_Query(t *testing.T) { assert.Len(t, data, 12) assert.Equal(t, int64(1200), data[0].Volume) assert.Equal(t, "2023-12-08", data[0].Date) - assert.Equal(t, "bar.com", data[0].Zone) + assert.Equal(t, "bar.com", data[0].ZoneName) } func TestDnsAnalyticsService_Query_SupportsFiltering(t *testing.T) { @@ -67,7 +67,7 @@ func TestDnsAnalyticsService_Query_SupportsSorting(t *testing.T) { testMethod(t, r, "GET") testHeaders(t, r) expectedQueryParameters := url.Values{} - expectedQueryParameters.Add("sort", "date:desc,zone:asc") + expectedQueryParameters.Add("sort", "date:desc,zone_name:asc") testQuery(t, r, expectedQueryParameters) w.WriteHeader(httpResponse.StatusCode) @@ -75,7 +75,7 @@ func TestDnsAnalyticsService_Query_SupportsSorting(t *testing.T) { }) options := DnsAnalyticsOptions{} - options.Sort = String("date:desc,zone:asc") + options.Sort = String("date:desc,zone_name:asc") _, _ = client.DnsAnalytics.Query(context.Background(), "1", &options) } @@ -113,12 +113,12 @@ func TestDnsAnalyticsService_Query_SupportsGrouping(t *testing.T) { testMethod(t, r, "GET") testHeaders(t, r) expectedQueryParameters := url.Values{} - expectedQueryParameters.Add("groupings", "zone,date") + expectedQueryParameters.Add("groupings", "zone_name,date") testQuery(t, r, expectedQueryParameters) w.WriteHeader(httpResponse.StatusCode) _, _ = io.Copy(w, httpResponse.Body) }) - _, _ = client.DnsAnalytics.Query(context.Background(), "1", &DnsAnalyticsOptions{Groupings: String("zone,date")}) + _, _ = client.DnsAnalytics.Query(context.Background(), "1", &DnsAnalyticsOptions{Groupings: String("zone_name,date")}) } diff --git a/fixtures.http/api/dnsAnalytics/success.http b/fixtures.http/api/dnsAnalytics/success.http index fa06540..d142db6 100644 --- a/fixtures.http/api/dnsAnalytics/success.http +++ b/fixtures.http/api/dnsAnalytics/success.http @@ -17,4 +17,4 @@ X-Permitted-Cross-Domain-Policies: none X-XSS-Protection: 1; mode=block Strict-Transport-Security: max-age=31536000 -{"data": {"headers": ["zone", "date", "volume"], "rows": [["bar.com", "2023-12-08", 1200], ["bar.com", "2023-12-09", 1200], ["bar.com", "2024-01-07", 1200], ["bar.com", "2024-01-08", 1200], ["example.com", "2023-12-08", 1200], ["example.com", "2023-12-09", 1200], ["example.com", "2024-01-07", 1200], ["example.com", "2024-01-08", 1200], ["foo.com", "2023-12-08", 1200], ["foo.com", "2023-12-09", 1200], ["foo.com", "2024-01-07", 1200], ["foo.com", "2024-01-08", 1200]]}, "query": {"account_id": 1, "start_date": "2023-12-08", "end_date": "2024-01-08", "sort": "zone:asc,date:asc", "page": 0, "per_page": 100, "groupings": "zone,date"}, "pagination": {"current_page": 0, "per_page": 100, "total_entries": 93, "total_pages": 1}} +{"data": {"headers": ["zone_name", "date", "volume"], "rows": [["bar.com", "2023-12-08", 1200], ["bar.com", "2023-12-09", 1200], ["bar.com", "2024-01-07", 1200], ["bar.com", "2024-01-08", 1200], ["example.com", "2023-12-08", 1200], ["example.com", "2023-12-09", 1200], ["example.com", "2024-01-07", 1200], ["example.com", "2024-01-08", 1200], ["foo.com", "2023-12-08", 1200], ["foo.com", "2023-12-09", 1200], ["foo.com", "2024-01-07", 1200], ["foo.com", "2024-01-08", 1200]]}, "query": {"account_id": 1, "start_date": "2023-12-08", "end_date": "2024-01-08", "sort": "zone_name:asc,date:asc", "page": 0, "per_page": 100, "groupings": "zone_name,date"}, "pagination": {"current_page": 0, "per_page": 100, "total_entries": 93, "total_pages": 1}} diff --git a/main/main.go b/main/main.go new file mode 100644 index 0000000..060b011 --- /dev/null +++ b/main/main.go @@ -0,0 +1,45 @@ +package main + +import ( + "context" + "fmt" + "github.com/dnsimple/dnsimple-go/dnsimple" + "os" + "strconv" +) + +func main() { + tc := dnsimple.StaticTokenHTTPClient(context.Background(), "dnsimpletest_a_fYkucvRnKzn86cVlODVnX0L6LaUbrnUa") + + // new client + client := dnsimple.NewClient(tc) + client.BaseURL = "http://api.dnsimple.localhost:3000" + + // get the current authenticated account (if you don't know who you are) + whoamiResponse, err := client.Identity.Whoami(context.Background()) + if err != nil { + fmt.Printf("Whoami() returned error: %v\n", err) + os.Exit(1) + } + + fmt.Println(whoamiResponse.Data.Account) + + // either assign the account ID or fetch it from the response + // if you are authenticated with an account token + accountID := strconv.FormatInt(whoamiResponse.Data.Account.ID, 10) + + // get the list of domains + dnsAnalyticsResponse, err := client.DnsAnalytics.Query(context.Background(), accountID, nil) + if err != nil { + fmt.Printf("DnsAnalytics.Query() returned error: %v\n", err) + os.Exit(1) + } + + // iterate over all the domains in the + // paginated response. + for _, entry := range dnsAnalyticsResponse.Data { + fmt.Println(entry) + } + + fmt.Println(dnsAnalyticsResponse) +}