Skip to content

Commit 76e233b

Browse files
committed
split client to oauth and basic
1 parent 91140b7 commit 76e233b

File tree

6 files changed

+37
-21
lines changed

6 files changed

+37
-21
lines changed

auth.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type TokenResponse struct {
2121
// Authenticate retrieves the access token to continue making requests to Viva's API. It
2222
// returns the full response of the API and stores the token and expiration time for
2323
// later use.
24-
func (c Client) Authenticate() (*TokenResponse, error) {
24+
func (c OAuthClient) Authenticate() (*TokenResponse, error) {
2525
uri := c.tokenEndpoint()
2626
auth := AuthBody(c.Config)
2727

@@ -56,7 +56,7 @@ func (c Client) Authenticate() (*TokenResponse, error) {
5656
}
5757

5858
// AuthToken returns the token value
59-
func (c Client) AuthToken() string {
59+
func (c OAuthClient) AuthToken() string {
6060
c.lock.RLock()
6161

6262
t := c.tokenValue.value
@@ -66,7 +66,7 @@ func (c Client) AuthToken() string {
6666
}
6767

6868
// SetToken sets the token value and the expiration time of the token.
69-
func (c Client) SetToken(value string, expires time.Time) {
69+
func (c OAuthClient) SetToken(value string, expires time.Time) {
7070
c.lock.Lock()
7171

7272
c.tokenValue.value = value
@@ -77,7 +77,7 @@ func (c Client) SetToken(value string, expires time.Time) {
7777

7878
// HasAuthExpired returns true if the expiry time of the token has passed and false
7979
// otherwise.
80-
func (c Client) HasAuthExpired() bool {
80+
func (c OAuthClient) HasAuthExpired() bool {
8181
c.lock.RLock()
8282

8383
expires := c.tokenValue.expires
@@ -98,11 +98,11 @@ func BasicAuth(c Config) string {
9898
return base64.StdEncoding.EncodeToString([]byte(auth))
9999
}
100100

101-
func (c Client) tokenEndpoint() string {
101+
func (c OAuthClient) tokenEndpoint() string {
102102
return fmt.Sprintf("%s/%s", c.authUri(), "/connect/token")
103103
}
104104

105-
func (c Client) authUri() string {
105+
func (c OAuthClient) authUri() string {
106106
if isDemo(c.Config) {
107107
return "https://demo-accounts.vivapayments.com"
108108
}

example/main.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ func main() {
1111
clientSecret := "ODX4vwQVmeYo373814yYf2p6Vq85yR"
1212
merchantID := "393969b6-c18e-4770-ba9a-2838c2beafee"
1313
apiKey := "YZ}z>_"
14-
client := vivawallet.New(clientID, clientSecret, merchantID, apiKey, true)
14+
oauthClient := vivawallet.NewOAuth(clientID, clientSecret, true)
15+
basicAuthClient := vivawallet.NewBasicAuth(merchantID, apiKey, true)
1516

16-
token, err := client.Authenticate()
17+
token, err := oauthClient.Authenticate()
1718
if err != nil {
1819
fmt.Printf("Error: %s\n", err.Error())
1920
return
@@ -23,14 +24,14 @@ func main() {
2324
req := vivawallet.CheckoutOrder{
2425
Amount: 1000,
2526
}
26-
op, err2 := client.CreateOrderPayment(req)
27+
op, err2 := oauthClient.CreateOrderPayment(req)
2728
if err2 != nil {
2829
fmt.Printf("err: %s\n", err2.Error())
2930
} else {
3031
fmt.Printf("OrderPayment: %d\n", op.OrderCode)
3132
}
3233

33-
wallets, err3 := client.GetWallets()
34+
wallets, err3 := basicAuthClient.GetWallets()
3435
if err3 != nil {
3536
fmt.Printf("err: %s\n", err3.Error())
3637
} else {

order_payments.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type CheckoutOrderResponse struct {
3737
}
3838

3939
// CreateOrderPayment creates a new order payment and returns the `orderCode`.
40-
func (c Client) CreateOrderPayment(payload CheckoutOrder) (*CheckoutOrderResponse, error) {
40+
func (c OAuthClient) CreateOrderPayment(payload CheckoutOrder) (*CheckoutOrderResponse, error) {
4141
uri := checkoutOrderUri(c.Config)
4242
data, err := json.Marshal(payload)
4343
if err != nil {

transactions.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type TransactionResponse struct {
3131
DigitalWalletID int `json:"digitalWalletId"`
3232
}
3333

34-
func (c Client) GetTransaction(trxID string) (*TransactionResponse, error) {
34+
func (c OAuthClient) GetTransaction(trxID string) (*TransactionResponse, error) {
3535
uri := getTransactionUri(c.Config, trxID)
3636

3737
// TODO: use RoundTripper to avoid rewriting this
@@ -78,7 +78,7 @@ type CardTokenResponse struct {
7878
Token string `json:"token"`
7979
}
8080

81-
func (c Client) CreateCardToken(payload CreateCardToken) (*CardTokenResponse, error) {
81+
func (c OAuthClient) CreateCardToken(payload CreateCardToken) (*CardTokenResponse, error) {
8282
// TODO: use RoundTripper to avoid rewriting this
8383
if c.HasAuthExpired() {
8484
_, authErr := c.Authenticate()

viva_wallet.go vivawallet.go

+21-6
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,50 @@ type token struct {
1919
expires time.Time
2020
}
2121

22-
type Client struct {
22+
type OAuthClient struct {
2323
Config Config
2424
HTTPClient *http.Client
2525
lock sync.RWMutex
2626
tokenValue *token
2727
}
2828

29+
type BasicAuthClient struct {
30+
Config Config
31+
HTTPClient *http.Client
32+
}
33+
2934
// defaultHTTPTimeout is the default timeout on the http.Client used by the library.
3035
const defaultTimeout = 60 * time.Second
3136

3237
var httpClient = &http.Client{
3338
Timeout: defaultTimeout,
3439
}
3540

36-
// New creates a new viva client
37-
func New(clientID string, clientSecret string, merchantID string, apiKey string, demo bool) *Client {
38-
return &Client{
41+
// New creates a new viva client for the oauth apis
42+
func NewOAuth(clientID string, clientSecret string, demo bool) *OAuthClient {
43+
return &OAuthClient{
3944
Config: Config{
4045
Demo: demo,
4146
ClientID: clientID,
4247
ClientSecret: clientSecret,
43-
MerchantID: merchantID,
44-
APIKey: apiKey,
4548
},
4649
HTTPClient: httpClient,
4750
tokenValue: &token{},
4851
}
4952
}
5053

54+
// New creates a new viva client for the basic auth apis
55+
func NewBasicAuth(merchantID string, apiKey string, demo bool) *BasicAuthClient {
56+
return &BasicAuthClient{
57+
Config: Config{
58+
Demo: demo,
59+
MerchantID: merchantID,
60+
APIKey: apiKey,
61+
},
62+
HTTPClient: httpClient,
63+
}
64+
}
65+
5166
// ApiUri returns the uri of the production or the demo api.
5267
func ApiUri(c Config) string {
5368
if isDemo(c) {

wallet.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type BalanceTransferResponse struct {
2323
CreditTransactionID string `json:"CreditTransactionId"`
2424
}
2525

26-
func (c Client) BalanceTranfer(walletID string, targetWalletID string, payload BalanceTransfer) (*BalanceTransferResponse, error) {
26+
func (c BasicAuthClient) BalanceTranfer(walletID string, targetWalletID string, payload BalanceTransfer) (*BalanceTransferResponse, error) {
2727
auth := BasicAuth(c.Config)
2828

2929
uri := getBalanceTransferUri(c.Config, walletID, targetWalletID)
@@ -72,7 +72,7 @@ type Wallet struct {
7272
CurrencyCode string `json:"CurrencyCode"`
7373
}
7474

75-
func (c Client) GetWallets() ([]Wallet, error) {
75+
func (c BasicAuthClient) GetWallets() ([]Wallet, error) {
7676
auth := BasicAuth(c.Config)
7777

7878
uri := getWalletsUri(c.Config)

0 commit comments

Comments
 (0)