diff options
| author | Shulhan <ms@kilabit.info> | 2023-01-31 18:45:59 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2023-01-31 18:45:59 +0700 |
| commit | 31bac8aba1b26f9dd482b0784711b7695f43dbb0 (patch) | |
| tree | 25e8c2428ce011e769d23bc31dfa4a6e26ae3c2d | |
| parent | 6a62797b1aa9b313c4c9fd0d41a00c83064c5bdf (diff) | |
| download | duitku-31bac8aba1b26f9dd482b0784711b7695f43dbb0.tar.xz | |
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...
| -rw-r--r-- | .gitignore | 5 | ||||
| -rw-r--r-- | client.conf.example | 5 | ||||
| -rw-r--r-- | client_options.go | 40 | ||||
| -rw-r--r-- | duitku_test.go | 17 |
4 files changed, 48 insertions, 19 deletions
@@ -1,5 +1,6 @@ ## SPDX-FileCopyrightText: 2022 M. Shulhan <ms@kilabit.info> ## 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) } |
