Skip to content

Commit

Permalink
Moved remote repo client connection test into client.
Browse files Browse the repository at this point in the history
  • Loading branch information
diamondap committed Nov 21, 2023
1 parent 3916245 commit dcb048e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 28 deletions.
27 changes: 24 additions & 3 deletions core/aptrust_client_v3.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package core

import (
"fmt"
"net/http"
"net/url"

"github.com/APTrust/dart-runner/constants"
"github.com/APTrust/dart-runner/util"
apt_network "github.com/APTrust/preservation-services/network"
)

func init() {
Expand Down Expand Up @@ -75,9 +80,25 @@ func (client *APTrustClientV3) AvailableHTMLReports() []util.NameValuePair {
// TestConnection tests a connection to the remote repo. It returns true
// or false to describe whether the connection succeeded. Check the error
// if the connection did not succeed.
func (client *APTrustClientV3) TestConnection() (bool, error) {

return true, nil
func (client *APTrustClientV3) TestConnection() error {
registryClient, err := apt_network.NewRegistryClient(
client.config.Url,
client.version,
client.config.UserID,
client.config.APIToken,
Dart.Log,
)
if err != nil {
return err
}
params := url.Values{}
params.Add("per_page", "1")
resp := registryClient.WorkItemList(params)
if resp.Response.StatusCode == http.StatusUnauthorized || resp.Response.StatusCode == http.StatusForbidden {
return fmt.Errorf("Server returned status %d. Be sure your user id and API token are correct.", resp.Response.StatusCode)
}
// Other errors should be OK here. They indicate that we did successfully authenticate.
return nil
}

// RunHTMLReport runs the named report and returns HTML suitable for
Expand Down
4 changes: 2 additions & 2 deletions core/lockss_client_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ func (client *LOCKSSClientV2) AvailableHTMLReports() []util.NameValuePair {
// TestConnection tests a connection to the remote repo. It returns true
// or false to describe whether the connection succeeded. Check the error
// if the connection did not succeed.
func (client *LOCKSSClientV2) TestConnection() (bool, error) {
func (client *LOCKSSClientV2) TestConnection() error {

return true, nil
return nil
}

// RunHTMLReport runs the named report and returns HTML suitable for
Expand Down
2 changes: 1 addition & 1 deletion core/remote_repo_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type RemoteRepoClient interface {
Description() string
AvailableHTMLReports() []util.NameValuePair
RunHTMLReport(string) (string, error)
TestConnection() (bool, error)
TestConnection() error
}

// RegisterRepoClient registers a remote repo client, so that
Expand Down
27 changes: 5 additions & 22 deletions core/remote_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ package core

import (
"fmt"
"net/http"
"net/url"
"strings"

"github.com/APTrust/dart-runner/constants"
"github.com/APTrust/dart-runner/util"
apt_network "github.com/APTrust/preservation-services/network"
"github.com/google/uuid"
)

Expand Down Expand Up @@ -115,26 +112,12 @@ func (repo *RemoteRepository) IsDeletable() bool {
}

func (repo *RemoteRepository) TestConnection() error {
// Once we support more than a single client,
// we'll have to look up the PluginId here. For now,
// we'll just use the APTrust client, because that's
// the only one that exists. LOCKSS should be coming later.
client, err := apt_network.NewRegistryClient(
repo.Url,
"v3",
repo.UserID,
repo.APIToken,
Dart.Log,
)
if !util.LooksLikeUUID(repo.PluginID) {
return fmt.Errorf("Please choose a client plugin before testing connection.")
}
client, err := GetRemoteRepoClient(repo)
if err != nil {
return err
}
params := url.Values{}
params.Add("per_page", "1")
resp := client.WorkItemList(params)
if resp.Response.StatusCode == http.StatusUnauthorized || resp.Response.StatusCode == http.StatusForbidden {
return fmt.Errorf("Server returned status %d. Be sure your user id and API token are correct.", resp.Response.StatusCode)
}
// Other errors should be OK here. They indicate that we did successfully authenticate.
return nil
return client.TestConnection()
}

0 comments on commit dcb048e

Please sign in to comment.