From 31bac8aba1b26f9dd482b0784711b7695f43dbb0 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Tue, 31 Jan 2023 18:45:59 +0700 Subject: all: add function to load ClientOptions from file The LoadClientOptions load the ClientOptions from configuration file. The file configuration is using INI, for example [duitku] server_url = https://sandbox.duitku.com disburse_user_id = 3551 disburse_email = test@chakratechnology.com disburse_api_key = de5... --- .gitignore | 5 +++-- client.conf.example | 5 +++++ client_options.go | 40 ++++++++++++++++++++++++++++++++-------- duitku_test.go | 17 ++++++++--------- 4 files changed, 48 insertions(+), 19 deletions(-) create mode 100644 client.conf.example diff --git a/.gitignore b/.gitignore index 9d97b82..21b0a79 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ ## SPDX-FileCopyrightText: 2022 M. Shulhan ## SPDX-License-Identifier: GPL-3.0-or-later *.json -cover.html -cover.out +/client.conf +/cover.html +/cover.out diff --git a/client.conf.example b/client.conf.example new file mode 100644 index 0000000..8f6141f --- /dev/null +++ b/client.conf.example @@ -0,0 +1,5 @@ +[duitku] +server_url = https://sandbox.duitku.com +disburse_user_id = 3551 +disburse_email = test@chakratechnology.com +disburse_api_key = de56f832487bc1ce1de5ff2cfacf8d9486c61da69df6fd61d5537b6b7d6d354d diff --git a/client_options.go b/client_options.go index 293b7c3..d4d6500 100644 --- a/client_options.go +++ b/client_options.go @@ -6,11 +6,14 @@ package duitku import ( "fmt" "net/url" + "os" + + "github.com/shuLhan/share/lib/ini" ) // ClientOptions configuration for HTTP client. type ClientOptions struct { - ServerUrl string + ServerUrl string `ini:"duitku::server_url"` // The hostname extracted from ServerUrl. host string @@ -23,23 +26,44 @@ type ClientOptions struct { // [merchant portal]. // // [merchant portal]: https://passport.duitku.com/merchant/Project - MerchantCode string + MerchantCode string `ini:"duitku::merchant_code"` // MerchantApiKey The API key for signing merchant related request. - MerchantApiKey string + MerchantApiKey string `ini:"duitku::merchant_api_key"` // Merchant code and API key for payment through Indomaret. - IndomaretMerchantCode string - IndomaretApiKey string + IndomaretMerchantCode string `ini:"duitku::indomaret_merchant_code"` + IndomaretApiKey string `ini:"duitku::indomaret_api_key"` // DisburseApiKey API key for signing disbursement request. - DisburseApiKey string + DisburseApiKey string `ini:"duitku::disburse_api_key"` // DisburseEmail The email registered for disbursement in Duitku. - DisburseEmail string + DisburseEmail string `ini:"duitku::disburse_email"` // DisburseUserID user ID for disbursement request. - DisburseUserID int64 + DisburseUserID int64 `ini:"duitku::disburse_user_id"` +} + +// LoadClientOptions load ClientOptions from file. +// See the client.conf.example for an example for client configuration. +func LoadClientOptions(file string) (opts *ClientOptions, err error) { + var ( + logp = `LoadClientOptions` + content []byte + ) + content, err = os.ReadFile(file) + if err != nil { + return nil, fmt.Errorf(`%s: %w`, logp, err) + } + + opts = &ClientOptions{} + err = ini.Unmarshal(content, opts) + if err != nil { + return nil, fmt.Errorf(`%s: %w`, logp, err) + } + + return opts, nil } // validate each field values. diff --git a/duitku_test.go b/duitku_test.go index 37d1ba6..9f158c5 100644 --- a/duitku_test.go +++ b/duitku_test.go @@ -15,17 +15,16 @@ var ( func TestMain(m *testing.M) { var ( - clOpts = ClientOptions{ - ServerUrl: ServerUrlSandbox, - DisburseUserID: 3551, - DisburseEmail: `test@chakratechnology.com`, - DisburseApiKey: `de56f832487bc1ce1de5ff2cfacf8d9486c61da69df6fd61d5537b6b7d6d354d`, - } - - err error + opts *ClientOptions + err error ) - testClient, err = NewClient(clOpts) + opts, err = LoadClientOptions(`client.conf.example`) + if err != nil { + log.Fatal(err) + } + + testClient, err = NewClient(*opts) if err != nil { log.Fatal(err) } -- cgit v1.3