-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement support for async activities export #43
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
54fef9d
initial version of async activities export
faaaa3d
run gofmt and update readme
af543b7
Update endpoint and response json
d79a7f6
update test
c1ff321
Changes based on feedback
f093c24
Typo in comment
7e7a927
Run gofmt on file
1cbde4f
Rename activitiesExportId => activitiesExportID
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,52 @@ | ||
package chartmogul | ||
|
||
// MetricsActivitiesExport represents Metrics API activity export in ChartMogul. | ||
type MetricsActivitiesExport struct { | ||
ID string `json:"id"` | ||
Status string `json:"status"` | ||
FileURL string `json:"file_url"` | ||
Params Params `json:"params"` | ||
ExpiresAt string `json:"expires_at"` | ||
CreatedAt string `json:"created_at"` | ||
} | ||
|
||
// Params provides information on the requested export. | ||
type Params struct { | ||
hassansin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Kind string `json:"kind"` | ||
Params NestedParams `json:"params,omitempty"` | ||
} | ||
|
||
// NestedParams represents the params of the requested type of export. | ||
type NestedParams struct { | ||
hassansin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ActivityType string `json:"activity_type,omitempty"` | ||
StartDate string `json:"start_date,omitempty"` | ||
EndDate string `json:"end_date,omitempty"` | ||
} | ||
|
||
// CreateMetricsActivitiesExportParam to create a MetricsActivitiesExport. | ||
type CreateMetricsActivitiesExportParam struct { | ||
Type string `json:"type,omitempty"` | ||
StartDate string `json:"start-date,omitempty"` | ||
hassansin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
EndDate string `json:"end-date,omitempty"` | ||
} | ||
|
||
const ( | ||
metricsActivitiesExportEndpoint = "activities_export" | ||
singleMetricsActivitiesExportEndpoint = "activities_export/:uuid" | ||
) | ||
|
||
// MetricsCreateActivitiesExport requests creation of an activities export in Chartmogul. | ||
// | ||
// See https://dev.chartmogul.com/v1.0/reference#activities_export | ||
func (api API) MetricsCreateActivitiesExport(CreateMetricsActivitiesExportParam *CreateMetricsActivitiesExportParam) (*MetricsActivitiesExport, error) { | ||
result := &MetricsActivitiesExport{} | ||
return result, api.create(metricsActivitiesExportEndpoint, CreateMetricsActivitiesExportParam, result) | ||
} | ||
|
||
// MetricsRetrieveActivitiesExport returns one activities export as in API. | ||
// | ||
// See https://dev.chartmogul.com/v1.0/reference#activities_export | ||
func (api API) MetricsRetrieveActivitiesExport(activitiesExportUUID string) (*MetricsActivitiesExport, error) { | ||
result := &MetricsActivitiesExport{} | ||
return result, api.retrieve(singleMetricsActivitiesExportEndpoint, activitiesExportUUID, result) | ||
} |
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,111 @@ | ||
package chartmogul | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/davecgh/go-spew/spew" | ||
) | ||
|
||
// Tests creation on an activity export. | ||
func TestCreateActivitiesExport(t *testing.T) { | ||
server := httptest.NewServer( | ||
http.HandlerFunc( | ||
func(w http.ResponseWriter, r *http.Request) { | ||
if r.Method != "POST" { | ||
t.Errorf("Unexpected method %v", r.Method) | ||
} | ||
if r.RequestURI != "/v/activities_export" { | ||
t.Errorf("Unexpected URI %v", r.RequestURI) | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
//nolint | ||
w.Write([]byte(`{ | ||
"id": "7f554dba-4a41-4cb2-9790-2045e4c3a5b1", | ||
"status": "pending", | ||
"file_url": null, | ||
"params": { | ||
"kind": "activities", | ||
"params": { | ||
"activity_type": "contraction", | ||
"start_date": "2020-01-01", | ||
"end_date": "2020-12-31" | ||
} | ||
}, | ||
"expires_at": null, | ||
"created_at": "2021-07-12T14:46:56+00:00" | ||
}`)) | ||
})) | ||
defer server.Close() | ||
SetURL(server.URL + "/v/%v") | ||
|
||
var tested IApi = &API{ | ||
AccountToken: "token", | ||
AccessKey: "key", | ||
} | ||
activitiesExport, err := tested.MetricsCreateActivitiesExport(&CreateMetricsActivitiesExportParam{ | ||
StartDate: "2020-01-01", | ||
EndDate: "2020-12-31", | ||
Type: "contraction", | ||
}) | ||
|
||
if err != nil { | ||
spew.Dump(err) | ||
t.Fatal("Not expected to fail") | ||
} | ||
if activitiesExport.ID != "7f554dba-4a41-4cb2-9790-2045e4c3a5b1" || activitiesExport.Status != "pending" { | ||
spew.Dump(activitiesExport) | ||
t.Fatal("Unexpected result") | ||
} | ||
} | ||
|
||
// Tests retrieval of an activity export | ||
func TestRetrieveActivitiesExport(t *testing.T) { | ||
server := httptest.NewServer( | ||
http.HandlerFunc( | ||
func(w http.ResponseWriter, r *http.Request) { | ||
if r.Method != "GET" { | ||
t.Errorf("Unexpected method %v", r.Method) | ||
} | ||
if r.RequestURI != "/v/activities_export/7f554dba-4a41-4cb2-9790-2045e4c3a5b1" { | ||
t.Errorf("Unexpected URI %v", r.RequestURI) | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
//nolint | ||
w.Write([]byte(`{ | ||
"id": "7f554dba-4a41-4cb2-9790-2045e4c3a5b1", | ||
"status": "succeeded", | ||
"file_url": "https://chartmogul-customer-export.s3.eu-west-1.amazonaws.com/activities-acme-corp-91e1ca88-d747-4e25-83d9-2b752033bdba.zip", | ||
"params": { | ||
"kind": "activities", | ||
"params": { | ||
"activity_type": "contraction", | ||
"start_date": "2020-01-01", | ||
"end_date": "2020-12-31" | ||
} | ||
}, | ||
"expires_at": "2021-07-19T14:46:58+00:00", | ||
"created_at": "2021-07-12T14:46:56+00:00" | ||
}`)) | ||
})) | ||
defer server.Close() | ||
SetURL(server.URL + "/v/%v") | ||
|
||
var tested IApi = &API{ | ||
AccountToken: "token", | ||
AccessKey: "key", | ||
} | ||
|
||
var activitiesExportID = "7f554dba-4a41-4cb2-9790-2045e4c3a5b1" | ||
activitiesExport, err := tested.MetricsRetrieveActivitiesExport(activitiesExportID) | ||
|
||
if err != nil { | ||
spew.Dump(err) | ||
t.Fatal("Not expected to fail") | ||
} | ||
if activitiesExport.ID != activitiesExportID || activitiesExport.Status != "succeeded" { | ||
spew.Dump(activitiesExport) | ||
t.Fatal("Unexpected result") | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be some whitespace issue. Try formatting with gofmt.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup, done!