From 54a38df86941cd96d721ee08e542a17ebcb3f6d0 Mon Sep 17 00:00:00 2001 From: ipinak Date: Sat, 4 Feb 2023 22:30:12 +0200 Subject: [PATCH] split client to oauth and basic --- auth.go | 12 ++++++------ example/main.go | 9 +++++---- order_payments.go | 2 +- transactions.go | 4 ++-- viva_wallet.go => vivawallet.go | 27 +++++++++++++++++++++------ wallet.go | 4 ++-- 6 files changed, 37 insertions(+), 21 deletions(-) rename viva_wallet.go => vivawallet.go (65%) diff --git a/auth.go b/auth.go index 4ad3871..1a857b5 100644 --- a/auth.go +++ b/auth.go @@ -21,7 +21,7 @@ type TokenResponse struct { // Authenticate retrieves the access token to continue making requests to Viva's API. It // returns the full response of the API and stores the token and expiration time for // later use. -func (c Client) Authenticate() (*TokenResponse, error) { +func (c OAuthClient) Authenticate() (*TokenResponse, error) { uri := c.tokenEndpoint() auth := AuthBody(c.Config) @@ -56,7 +56,7 @@ func (c Client) Authenticate() (*TokenResponse, error) { } // AuthToken returns the token value -func (c Client) AuthToken() string { +func (c OAuthClient) AuthToken() string { c.lock.RLock() t := c.tokenValue.value @@ -66,7 +66,7 @@ func (c Client) AuthToken() string { } // SetToken sets the token value and the expiration time of the token. -func (c Client) SetToken(value string, expires time.Time) { +func (c OAuthClient) SetToken(value string, expires time.Time) { c.lock.Lock() c.tokenValue.value = value @@ -77,7 +77,7 @@ func (c Client) SetToken(value string, expires time.Time) { // HasAuthExpired returns true if the expiry time of the token has passed and false // otherwise. -func (c Client) HasAuthExpired() bool { +func (c OAuthClient) HasAuthExpired() bool { c.lock.RLock() expires := c.tokenValue.expires @@ -98,11 +98,11 @@ func BasicAuth(c Config) string { return base64.StdEncoding.EncodeToString([]byte(auth)) } -func (c Client) tokenEndpoint() string { +func (c OAuthClient) tokenEndpoint() string { return fmt.Sprintf("%s/%s", c.authUri(), "/connect/token") } -func (c Client) authUri() string { +func (c OAuthClient) authUri() string { if isDemo(c.Config) { return "https://demo-accounts.vivapayments.com" } diff --git a/example/main.go b/example/main.go index 5ba1621..21e66a5 100644 --- a/example/main.go +++ b/example/main.go @@ -11,9 +11,10 @@ func main() { clientSecret := "ODX4vwQVmeYo373814yYf2p6Vq85yR" merchantID := "393969b6-c18e-4770-ba9a-2838c2beafee" apiKey := "YZ}z>_" - client := vivawallet.New(clientID, clientSecret, merchantID, apiKey, true) + oauthClient := vivawallet.NewOAuth(clientID, clientSecret, true) + basicAuthClient := vivawallet.NewBasicAuth(merchantID, apiKey, true) - token, err := client.Authenticate() + token, err := oauthClient.Authenticate() if err != nil { fmt.Printf("Error: %s\n", err.Error()) return @@ -23,14 +24,14 @@ func main() { req := vivawallet.CheckoutOrder{ Amount: 1000, } - op, err2 := client.CreateOrderPayment(req) + op, err2 := oauthClient.CreateOrderPayment(req) if err2 != nil { fmt.Printf("err: %s\n", err2.Error()) } else { fmt.Printf("OrderPayment: %d\n", op.OrderCode) } - wallets, err3 := client.GetWallets() + wallets, err3 := basicAuthClient.GetWallets() if err3 != nil { fmt.Printf("err: %s\n", err3.Error()) } else { diff --git a/order_payments.go b/order_payments.go index cafbba7..a661a0d 100644 --- a/order_payments.go +++ b/order_payments.go @@ -37,7 +37,7 @@ type CheckoutOrderResponse struct { } // CreateOrderPayment creates a new order payment and returns the `orderCode`. -func (c Client) CreateOrderPayment(payload CheckoutOrder) (*CheckoutOrderResponse, error) { +func (c OAuthClient) CreateOrderPayment(payload CheckoutOrder) (*CheckoutOrderResponse, error) { uri := checkoutOrderUri(c.Config) data, err := json.Marshal(payload) if err != nil { diff --git a/transactions.go b/transactions.go index d4bef70..95e5cf3 100644 --- a/transactions.go +++ b/transactions.go @@ -31,7 +31,7 @@ type TransactionResponse struct { DigitalWalletID int `json:"digitalWalletId"` } -func (c Client) GetTransaction(trxID string) (*TransactionResponse, error) { +func (c OAuthClient) GetTransaction(trxID string) (*TransactionResponse, error) { uri := getTransactionUri(c.Config, trxID) // TODO: use RoundTripper to avoid rewriting this @@ -78,7 +78,7 @@ type CardTokenResponse struct { Token string `json:"token"` } -func (c Client) CreateCardToken(payload CreateCardToken) (*CardTokenResponse, error) { +func (c OAuthClient) CreateCardToken(payload CreateCardToken) (*CardTokenResponse, error) { // TODO: use RoundTripper to avoid rewriting this if c.HasAuthExpired() { _, authErr := c.Authenticate() diff --git a/viva_wallet.go b/vivawallet.go similarity index 65% rename from viva_wallet.go rename to vivawallet.go index d83b777..cba1a2d 100644 --- a/viva_wallet.go +++ b/vivawallet.go @@ -19,13 +19,18 @@ type token struct { expires time.Time } -type Client struct { +type OAuthClient struct { Config Config HTTPClient *http.Client lock sync.RWMutex tokenValue *token } +type BasicAuthClient struct { + Config Config + HTTPClient *http.Client +} + // defaultHTTPTimeout is the default timeout on the http.Client used by the library. const defaultTimeout = 60 * time.Second @@ -33,21 +38,31 @@ var httpClient = &http.Client{ Timeout: defaultTimeout, } -// New creates a new viva client -func New(clientID string, clientSecret string, merchantID string, apiKey string, demo bool) *Client { - return &Client{ +// New creates a new viva client for the oauth apis +func NewOAuth(clientID string, clientSecret string, demo bool) *OAuthClient { + return &OAuthClient{ Config: Config{ Demo: demo, ClientID: clientID, ClientSecret: clientSecret, - MerchantID: merchantID, - APIKey: apiKey, }, HTTPClient: httpClient, tokenValue: &token{}, } } +// New creates a new viva client for the basic auth apis +func NewBasicAuth(merchantID string, apiKey string, demo bool) *BasicAuthClient { + return &BasicAuthClient{ + Config: Config{ + Demo: demo, + MerchantID: merchantID, + APIKey: apiKey, + }, + HTTPClient: httpClient, + } +} + // ApiUri returns the uri of the production or the demo api. func ApiUri(c Config) string { if isDemo(c) { diff --git a/wallet.go b/wallet.go index 180bbbe..a00c079 100644 --- a/wallet.go +++ b/wallet.go @@ -23,7 +23,7 @@ type BalanceTransferResponse struct { CreditTransactionID string `json:"CreditTransactionId"` } -func (c Client) BalanceTranfer(walletID string, targetWalletID string, payload BalanceTransfer) (*BalanceTransferResponse, error) { +func (c BasicAuthClient) BalanceTranfer(walletID string, targetWalletID string, payload BalanceTransfer) (*BalanceTransferResponse, error) { auth := BasicAuth(c.Config) uri := getBalanceTransferUri(c.Config, walletID, targetWalletID) @@ -72,7 +72,7 @@ type Wallet struct { CurrencyCode string `json:"CurrencyCode"` } -func (c Client) GetWallets() ([]Wallet, error) { +func (c BasicAuthClient) GetWallets() ([]Wallet, error) { auth := BasicAuth(c.Config) uri := getWalletsUri(c.Config)