-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #125 from zombozo12/master
feat: added report api
- Loading branch information
Showing
10 changed files
with
453 additions
and
0 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,44 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/joho/godotenv" | ||
"github.com/xendit/xendit-go" | ||
"github.com/xendit/xendit-go/report" | ||
"log" | ||
"os" | ||
) | ||
|
||
func main() { | ||
godotenvErr := godotenv.Load() | ||
if godotenvErr != nil { | ||
log.Fatal(godotenvErr) | ||
} | ||
|
||
xendit.Opt.SecretKey = os.Getenv("SECRET_KEY") | ||
|
||
generateReportData := report.GenerateReportParams{ | ||
Type: "BALANCE_HISTORY", // "BALANCE_HISTORY", "TRANSACTIONS", "UPCOMING_TRANSACTIONS" | ||
Filter: report.Filter{ | ||
From: "2020-01-01T00:00:00.000Z", | ||
To: "2020-12-31T23:59:59.999Z", | ||
}, | ||
} | ||
|
||
resp, err := report.GenerateReport(&generateReportData) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fmt.Printf("generated report: %+v\n", resp) | ||
|
||
getReportData := report.GetReportParams{ | ||
ReportID: resp.ID, | ||
} | ||
|
||
resp, err = report.GetReport(&getReportData) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Printf("retrieved report: %+v\n", resp) | ||
} |
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,28 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/xendit/xendit-go/report" | ||
"log" | ||
) | ||
|
||
func reportTest() { | ||
respReport, err := report.GenerateReport(&report.GenerateReportParams{ | ||
Type: "BALANCE_HISTORY", // "BALANCE_HISTORY", "TRANSACTIONS", "UPCOMING_TRANSACTIONS" | ||
Filter: report.Filter{ | ||
From: "2020-01-01T00:00:00.000Z", | ||
To: "2020-12-31T23:59:59.999Z", | ||
}, | ||
}) | ||
if err != nil { | ||
log.Panic(err) | ||
} | ||
|
||
_, err = report.GetReport(&report.GetReportParams{ | ||
ReportID: respReport.ID, | ||
}) | ||
if err != nil { | ||
log.Panic(err) | ||
} | ||
fmt.Println("report integration tests done!") | ||
} |
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,21 @@ | ||
package xendit | ||
|
||
import "time" | ||
|
||
type Report struct { | ||
ID string `json:"id"` | ||
Type string `json:"type"` | ||
Filter Filter `json:"filter"` | ||
Format string `json:"format"` | ||
Status string `json:"status"` | ||
Url string `json:"url,omitempty"` | ||
Currency string `json:"currency"` | ||
BusinessID string `json:"business_id"` | ||
Created time.Time `json:"created"` | ||
Updated time.Time `json:"updated"` | ||
} | ||
|
||
type Filter struct { | ||
From string `json:"from"` | ||
To string `json:"to"` | ||
} |
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,82 @@ | ||
package report | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/xendit/xendit-go" | ||
"github.com/xendit/xendit-go/utils/validator" | ||
"net/http" | ||
) | ||
|
||
type Client struct { | ||
Opt *xendit.Option | ||
APIRequester xendit.APIRequester | ||
} | ||
|
||
// GenerateReport creates a report | ||
func (c *Client) GenerateReport(data *GenerateReportParams) (*xendit.Report, *xendit.Error) { | ||
return c.GenerateReportWithContext(context.Background(), data) | ||
} | ||
|
||
// GenerateReportWithContext creates a report with context | ||
func (c *Client) GenerateReportWithContext(ctx context.Context, data *GenerateReportParams) (*xendit.Report, *xendit.Error) { | ||
if err := validator.ValidateRequired(ctx, data); err != nil { | ||
return nil, validator.APIValidatorErr(err) | ||
} | ||
|
||
response := &xendit.Report{} | ||
|
||
header := http.Header{} | ||
if data.ForUserID != "" { | ||
header.Add("for-user-id", data.ForUserID) | ||
} | ||
|
||
err := c.APIRequester.Call( | ||
ctx, | ||
"POST", | ||
fmt.Sprintf("%s/reports", c.Opt.XenditURL), | ||
c.Opt.SecretKey, | ||
header, | ||
data, | ||
response, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return response, nil | ||
} | ||
|
||
// GetReport gets a report | ||
func (c *Client) GetReport(data *GetReportParams) (*xendit.Report, *xendit.Error) { | ||
return c.GetReportWithContext(context.Background(), data) | ||
} | ||
|
||
// GetReportWithContext gets a report with context | ||
func (c *Client) GetReportWithContext(ctx context.Context, data *GetReportParams) (*xendit.Report, *xendit.Error) { | ||
if err := validator.ValidateRequired(ctx, data); err != nil { | ||
return nil, validator.APIValidatorErr(err) | ||
} | ||
|
||
response := &xendit.Report{} | ||
|
||
header := http.Header{} | ||
if data.ForUserID != "" { | ||
header.Add("for-user-id", data.ForUserID) | ||
} | ||
|
||
err := c.APIRequester.Call( | ||
ctx, | ||
"GET", | ||
fmt.Sprintf("%s/reports/%s", c.Opt.XenditURL, data.ReportID), | ||
c.Opt.SecretKey, | ||
header, | ||
nil, | ||
response, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return response, 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,40 @@ | ||
package report | ||
|
||
import ( | ||
"github.com/xendit/xendit-go" | ||
"log" | ||
) | ||
|
||
func ExampleGenerateReport() { | ||
xendit.Opt.SecretKey = "examplesecretkey" | ||
|
||
generateReport := GenerateReportParams{ | ||
Type: "BALANCE_HISTORY", // "BALANCE_HISTORY", "TRANSACTIONS", "UPCOMING_TRANSACTIONS" | ||
Filter: Filter{ | ||
From: "2020-01-01T00:00:00.000Z", | ||
To: "2020-12-31T23:59:59.999Z", | ||
}, | ||
} | ||
|
||
resp, err := GenerateReport(&generateReport) | ||
if err != nil { | ||
log.Fatal(err.ErrorCode) | ||
} | ||
|
||
log.Printf("generated report: %+v\n", resp) | ||
} | ||
|
||
func ExampleGetReport() { | ||
xendit.Opt.SecretKey = "examplesecretkey" | ||
|
||
getReport := GetReportParams{ | ||
ReportID: "report_5c1b34a2-6ceb-4c24-aba9-c836bac82b28", | ||
} | ||
|
||
resp, err := GetReport(&getReport) | ||
if err != nil { | ||
log.Fatal(err.ErrorCode) | ||
} | ||
|
||
log.Printf("retrieved report: %+v\n", resp) | ||
} |
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,20 @@ | ||
package report | ||
|
||
type Filter struct { | ||
From string `json:"from"` | ||
To string `json:"to"` | ||
} | ||
|
||
type GenerateReportParams struct { | ||
ForUserID string `json:"-"` | ||
Type string `json:"type" validate:"required"` | ||
Filter Filter `json:"filter" validate:"required"` | ||
Format string `json:"format"` | ||
Currency string `json:"currency"` | ||
ReportVersion string `json:"report_version"` | ||
} | ||
|
||
type GetReportParams struct { | ||
ForUserID string `json:"-"` | ||
ReportID string `json:"report_id" validate:"required"` | ||
} |
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,43 @@ | ||
package report | ||
|
||
import ( | ||
"context" | ||
"github.com/xendit/xendit-go" | ||
) | ||
|
||
// GenerateReport generates a report | ||
func GenerateReport(data *GenerateReportParams) (*xendit.Report, *xendit.Error) { | ||
return GenerateReportWithContext(context.Background(), data) | ||
} | ||
|
||
// GenerateReportWithContext generates a report with context | ||
func GenerateReportWithContext(ctx context.Context, data *GenerateReportParams) (*xendit.Report, *xendit.Error) { | ||
client, err := getClient() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return client.GenerateReportWithContext(ctx, data) | ||
} | ||
|
||
// GetReport gets a report | ||
func GetReport(data *GetReportParams) (*xendit.Report, *xendit.Error) { | ||
return GetReportWithContext(context.Background(), data) | ||
} | ||
|
||
// GetReportWithContext gets a report with context | ||
func GetReportWithContext(ctx context.Context, data *GetReportParams) (*xendit.Report, *xendit.Error) { | ||
client, err := getClient() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return client.GetReportWithContext(ctx, data) | ||
} | ||
|
||
func getClient() (*Client, *xendit.Error) { | ||
return &Client{ | ||
Opt: &xendit.Opt, | ||
APIRequester: xendit.GetAPIRequester(), | ||
}, nil | ||
} |
Oops, something went wrong.