Skip to content

Commit

Permalink
Refactor to use better names
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Lo-A-Foe <[email protected]>
  • Loading branch information
loafoe committed Sep 19, 2021
1 parent 06af1a7 commit dc44b1e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 36 deletions.
50 changes: 25 additions & 25 deletions ai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ type Config struct {
Region string
Environment string
OrganizationID string `Validate:"required"`
AnalyzeURL string
BaseURL string
Service string `Validate:"required"`
DebugLog string
Retry int
}

// A Client manages communication with HSDP AI Analyze API
// A Client manages communication with HSDP AI APIs
type Client struct {
// HTTP Client used to communicate with IAM API
iamClient *iam.Client
config *Config
analyzeURL *url.URL
iamClient *iam.Client
config *Config
baseURL *url.URL

// User agent used when communicating with the HSDP Notification API
UserAgent string
Expand All @@ -65,7 +65,7 @@ func NewClient(iamClient *iam.Client, config *Config) (*Client, error) {
doAutoconf(config)
c := &Client{iamClient: iamClient, config: config, UserAgent: userAgent, validate: validator.New()}

if err := c.SetAnalyzeURL(config.AnalyzeURL); err != nil {
if err := c.SetBaseURL(config.BaseURL); err != nil {
return nil, err
}

Expand All @@ -81,9 +81,9 @@ func doAutoconf(config *Config) {
autoconf.WithRegion(config.Region),
autoconf.WithEnv(config.Environment))
if err == nil {
analyzeService := c.Service(config.Service)
if analyzeService.URL != "" && config.AnalyzeURL == "" {
config.AnalyzeURL = analyzeService.URL
theService := c.Service(config.Service)
if theService.URL != "" && config.BaseURL == "" {
config.BaseURL = theService.URL
}
}
}
Expand All @@ -97,64 +97,64 @@ func (c *Client) Close() {
}
}

// GetAnalyzeURL returns the base CDL Store base URL as configured
func (c *Client) GetAnalyzeURL() string {
if c.analyzeURL == nil {
// GetBaseURL returns the base URL as configured
func (c *Client) GetBaseURL() string {
if c.baseURL == nil {
return ""
}
return c.analyzeURL.String()
return c.baseURL.String()
}

// SetAnalyzeURL sets the Notification URL for API requests
func (c *Client) SetAnalyzeURL(urlStr string) error {
// SetBaseURL sets the base URL for API requests
func (c *Client) SetBaseURL(urlStr string) error {
if urlStr == "" {
return ErrAnalyzeURLCannotBeEmpty
return ErrBaseURLCannotBeEmpty
}
// Make sure the given URL end with a slash
if !strings.HasSuffix(urlStr, "/") {
urlStr += "/"
}
var err error
c.analyzeURL, err = url.Parse(urlStr)
c.baseURL, err = url.Parse(urlStr)
return err
}

// GetEndpointURL returns the FHIR Store Endpoint URL as configured
func (c *Client) GetEndpointURL() string {
return c.GetAnalyzeURL() + path.Join("analyze", c.config.Service, c.config.OrganizationID)
return c.GetBaseURL() + path.Join("analyze", c.config.Service, c.config.OrganizationID)
}

// SetEndpointURL sets the endpoint URL for API requests to a custom endpoint. urlStr
// should always be specified with a trailing slash.
func (c *Client) SetEndpointURL(urlStr string) error {
if urlStr == "" {
return ErrAnalyzeURLCannotBeEmpty
return ErrBaseURLCannotBeEmpty
}
// Make sure the given URL ends with a slash
if !strings.HasSuffix(urlStr, "/") {
urlStr += "/"
}
var err error
c.analyzeURL, err = url.Parse(urlStr)
c.baseURL, err = url.Parse(urlStr)
if err != nil {
return err
}
parts := strings.Split(c.analyzeURL.Path, "/")
parts := strings.Split(c.baseURL.Path, "/")
if len(parts) == 0 {
return ErrAnalyzeURLCannotBeEmpty
return ErrBaseURLCannotBeEmpty
}
if len(parts) < 5 {
return ErrInvalidEndpointURL
}
c.config.OrganizationID = parts[len(parts)-2]
c.analyzeURL.Path = "/"
c.baseURL.Path = "/"
return nil
}

func (c *Client) NewAIRequest(method, requestPath string, opt interface{}, options ...OptionFunc) (*http.Request, error) {
u := *c.analyzeURL
u := *c.baseURL
// Set the encoded opaque data
u.Opaque = path.Join(c.analyzeURL.Path, "analyze", c.config.Service, c.config.OrganizationID, requestPath)
u.Opaque = path.Join(c.baseURL.Path, "analyze", c.config.Service, c.config.OrganizationID, requestPath)

if opt != nil {
q, err := query.Values(opt)
Expand Down
4 changes: 2 additions & 2 deletions ai/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func setup(t *testing.T) func() {
assert.Nil(t, err)

aiClient, err = ai.NewClient(iamClient, &ai.Config{
AnalyzeURL: serverAI.URL,
BaseURL: serverAI.URL,
OrganizationID: aiTenantID,
Service: "inference",
})
Expand Down Expand Up @@ -169,7 +169,7 @@ func TestDebug(t *testing.T) {
}

aiClient, err = ai.NewClient(iamClient, &ai.Config{
AnalyzeURL: serverAI.URL,
BaseURL: serverAI.URL,
DebugLog: tempFile.Name(),
Service: "inference",
OrganizationID: "xxx",
Expand Down
6 changes: 3 additions & 3 deletions ai/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

// Errors
var (
ErrAnalyzeURLCannotBeEmpty = errors.New("base Inference URL cannot be empty")
ErrEmptyResult = errors.New("empty result")
ErrInvalidEndpointURL = errors.New("invalid endpoint URL")
ErrBaseURLCannotBeEmpty = errors.New("base URL cannot be empty")
ErrEmptyResult = errors.New("empty result")
ErrInvalidEndpointURL = errors.New("invalid endpoint URL")
)
4 changes: 2 additions & 2 deletions ai/inference/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func setup(t *testing.T) func() {
assert.Nil(t, err)

inferenceClient, err = inference.NewClient(iamClient, &ai.Config{
AnalyzeURL: serverInference.URL,
BaseURL: serverInference.URL,
OrganizationID: inferenceTenantID,
})
if !assert.Nilf(t, err, "failed to create notificationClient: %v", err) {
Expand Down Expand Up @@ -204,7 +204,7 @@ func TestDebug(t *testing.T) {
}

inferenceClient, err = inference.NewClient(iamClient, &ai.Config{
AnalyzeURL: serverInference.URL,
BaseURL: serverInference.URL,
DebugLog: tempFile.Name(),
OrganizationID: "xxx",
})
Expand Down
4 changes: 2 additions & 2 deletions ai/training/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func setup(t *testing.T) func() {
assert.Nil(t, err)

trainingClient, err = training.NewClient(iamClient, &ai.Config{
AnalyzeURL: serverTraining.URL,
BaseURL: serverTraining.URL,
OrganizationID: trainingTenantID,
})
if !assert.Nilf(t, err, "failed to create notificationClient: %v", err) {
Expand Down Expand Up @@ -204,7 +204,7 @@ func TestDebug(t *testing.T) {
}

trainingClient, err = training.NewClient(iamClient, &ai.Config{
AnalyzeURL: serverTraining.URL,
BaseURL: serverTraining.URL,
DebugLog: tmpfile.Name(),
OrganizationID: "xxx",
})
Expand Down
4 changes: 2 additions & 2 deletions ai/workspace/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func setup(t *testing.T) func() {
assert.Nil(t, err)

workspaceClient, err = workspace.NewClient(iamClient, &ai.Config{
AnalyzeURL: serverWorkspace.URL,
BaseURL: serverWorkspace.URL,
OrganizationID: workspaceTenantID,
})
if !assert.Nilf(t, err, "failed to create notificationClient: %v", err) {
Expand Down Expand Up @@ -203,7 +203,7 @@ func TestDebug(t *testing.T) {
}

workspaceClient, err = workspace.NewClient(iamClient, &ai.Config{
AnalyzeURL: serverWorkspace.URL,
BaseURL: serverWorkspace.URL,
DebugLog: tmpfile.Name(),
OrganizationID: "xxx",
})
Expand Down

0 comments on commit dc44b1e

Please sign in to comment.