Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

split client to oauth and basic #12

Merged
merged 1 commit into from
Feb 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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"
}
Expand Down
9 changes: 5 additions & 4 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion order_payments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
27 changes: 21 additions & 6 deletions viva_wallet.go → vivawallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,50 @@ 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

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) {
Expand Down
4 changes: 2 additions & 2 deletions wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down