Skip to content

Commit

Permalink
decouple ITA constructor from launchspec definition
Browse files Browse the repository at this point in the history
  • Loading branch information
jessieqliu committed Jan 23, 2025
1 parent 01c5cd7 commit 7b9f778
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 36 deletions.
24 changes: 9 additions & 15 deletions verifier/ita/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,26 @@ type client struct {
apiKey string
}

func urlAndKey(regionAndKey string) (string, string, error) {
if regionAndKey == "" {
return "", "", errors.New("API region and key required to initialize ITA client")
func urlFromRegion(region string) (string, error) {
if region == "" {
return "", errors.New("API region required to initialize ITA client")
}

// Expect format <region>:<api key>.
split := strings.SplitN(regionAndKey, ":", 2)
if len(split) != 2 {
return "", "", errors.New("API region and key not in expected format <region>:<key>")
}
region := strings.ToUpper(split[0])
url, ok := regionalURLs[region]
url, ok := regionalURLs[strings.ToUpper(region)]
if !ok {
// Create list of allowed regions.
keys := []string{}
for k := range regionalURLs {
keys = append(keys, k)
}
return "", "", fmt.Errorf("unsupported region %v, expect one of %v", region, keys)
return "", fmt.Errorf("unsupported region %v, expect one of %v", region, keys)
}

return url, split[1], nil
return url, nil
}

func NewClient(regionAndKey string) (verifier.Client, error) {
url, apiKey, err := urlAndKey(regionAndKey)
func NewClient(region string, key string) (verifier.Client, error) {
url, err := urlFromRegion(region)
if err != nil {
return nil, err
}
Expand All @@ -87,7 +81,7 @@ func NewClient(regionAndKey string) (verifier.Client, error) {
},
},
apiURL: url,
apiKey: apiKey,
apiKey: key,
}, nil
}

Expand Down
29 changes: 8 additions & 21 deletions verifier/ita/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,55 +279,42 @@ func TestConvertRequestToTokenRequest(t *testing.T) {
}
}

func TestURLAndKey(t *testing.T) {
testAPIKey := "testAPIKey"

func TestURLFromRegion(t *testing.T) {
for region, expectedURL := range regionalURLs {
t.Run(region+" region", func(t *testing.T) {
regionAndKey := region + ":" + testAPIKey

url, key, err := urlAndKey(regionAndKey)
url, err := urlFromRegion(region)
if err != nil {
t.Fatalf("urlAndKey returned error: %v", err)
}

if url != expectedURL {
t.Errorf("urlAndKey did not return expected URL: got %v, want %v", url, expectedURL)
}

if key != testAPIKey {
t.Errorf("urlAndKey did not return expected API key: got %v, want %v", url, expectedURL)
}
})
}
}

func TestURLAndKeyError(t *testing.T) {
func TestURLFromRegionError(t *testing.T) {
testcases := []struct {
name string
regionAndKey string
region string
expectedSubstr string
}{
{
name: "No colon separator",
regionAndKey: "notAValidInput",
expectedSubstr: "not in expected format",
},
{
name: "Unsupported region",
regionAndKey: "Narnia:test-api-key",
region: "ANTARCTICA",
expectedSubstr: "unsupported region",
},
{
name: "Empty input",
regionAndKey: "",
expectedSubstr: "region and key required",
region: "",
expectedSubstr: "region required",
},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
_, _, err := urlAndKey(tc.regionAndKey)
_, err := urlFromRegion(tc.region)
if err == nil {
t.Fatal("urlAndKey returned successfully, expected error")
}
Expand Down

0 comments on commit 7b9f778

Please sign in to comment.