Skip to content

Commit

Permalink
Merge pull request #125 from zombozo12/master
Browse files Browse the repository at this point in the history
feat: added report api
  • Loading branch information
adityarx authored May 29, 2023
2 parents 55ed3b0 + 6d09ef2 commit c093978
Show file tree
Hide file tree
Showing 10 changed files with 453 additions and 0 deletions.
3 changes: 3 additions & 0 deletions client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/xendit/xendit-go/payout"
"github.com/xendit/xendit-go/qrcode"
"github.com/xendit/xendit-go/recurringpayment"
"github.com/xendit/xendit-go/report"
"github.com/xendit/xendit-go/retailoutlet"
"github.com/xendit/xendit-go/transaction"
"github.com/xendit/xendit-go/virtualaccount"
Expand All @@ -37,6 +38,7 @@ type API struct {
QRCode *qrcode.Client
Customer *customer.Client
Transaction *transaction.Client
Report *report.Client
}

func (a *API) init() {
Expand All @@ -53,6 +55,7 @@ func (a *API) init() {
a.QRCode = &qrcode.Client{Opt: &a.opt, APIRequester: a.apiRequester}
a.Customer = &customer.Client{Opt: &a.opt, APIRequester: a.apiRequester}
a.Transaction = &transaction.Client{Opt: &a.opt, APIRequester: a.apiRequester}
a.Report = &report.Client{Opt: &a.opt, APIRequester: a.apiRequester}
}

// New creates a new Xendit API client
Expand Down
44 changes: 44 additions & 0 deletions example/report/main.go
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)
}
1 change: 1 addition & 0 deletions integration_test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var testFunctions = []func(){
transactionTest,
accountTest,
paymentmethodTest,
reportTest,
}

func main() {
Expand Down
28 changes: 28 additions & 0 deletions integration_test/report.go
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!")
}
21 changes: 21 additions & 0 deletions report.go
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"`
}
82 changes: 82 additions & 0 deletions report/client.go
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
}
40 changes: 40 additions & 0 deletions report/example_test.go
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)
}
20 changes: 20 additions & 0 deletions report/params.go
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"`
}
43 changes: 43 additions & 0 deletions report/report.go
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
}
Loading

0 comments on commit c093978

Please sign in to comment.