Skip to content

Backup Api Key support #81

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Test Added
  • Loading branch information
NFEL committed Sep 21, 2023
commit 61ba84f2ba7c022d1046240e658311f940cb9696
20 changes: 20 additions & 0 deletions multi_key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package etherscan

import "testing"

// TestgetKey may fail is you run test parallel
func TestgetKey(t *testing.T) {
countApiKey, countBackupApiKey, k := 0, 0, ""
for i := 0; i < 10; i++ {
k = api.getKey()
if apiKey == k {
countApiKey++
} else if backupApiKey == k {
countBackupApiKey++
}
}
equal := countApiKey == 5 && countBackupApiKey == 5
if !equal {
t.Error("api.getKey not working")
}
}
15 changes: 12 additions & 3 deletions setup_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,39 @@ package etherscan

import (
"fmt"
"log"
"os"
"testing"
"time"
)

const apiKeyEnvName = "ETHERSCAN_API_KEY"
const (
apiKeyEnvName = "ETHERSCAN_API_KEY"
backupApiKeyEnvName = "BACKUP_ETHERSCAN_API_KEY"
)

var (
// api test client for many test cases
api *Client
// bucket default rate limiter
bucket *Bucket
// apiKey etherscan API key
apiKey string
apiKey string
backupApiKey string
)

func init() {
apiKey = os.Getenv(apiKeyEnvName)
if apiKey == "" {
panic(fmt.Sprintf("API key is empty, set env variable %q with a valid API key to proceed.", apiKeyEnvName))
}
backupApiKey = os.Getenv(apiKeyEnvName)
if backupApiKey == "" {
log.Printf("WARN: Backup API key is empty, set env variable %q with a valid API key to proceed.", backupApiKeyEnvName)
}
bucket = NewBucket(500 * time.Millisecond)

api = New(Mainnet, []string{apiKey})
api = New(Mainnet, []string{apiKey, backupApiKey})
api.Verbose = true
api.BeforeRequest = func(module string, action string, param map[string]interface{}) error {
bucket.Take()
Expand Down