Skip to content

Commit

Permalink
feat!: allow passing of base URL as string (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
Enda authored Jun 3, 2021
1 parent 76ca85f commit 478ac73
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 25 deletions.
5 changes: 2 additions & 3 deletions examples/kafkainstance/kafka_instance.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"github.com/redhat-developer/app-services-sdk-go/kafkainstance/apiv1internal/client"
"context"
"fmt"
"os"
Expand All @@ -19,10 +18,10 @@ func main() {

apiClient := kafkainstanceapi.NewAPIClient(&kafkainstanceapi.Config{
HTTPClient: tc,
Debug: true,
BaseURL: "http://localhost:9000/custom/path/to/rest",
})

x := kafkainstance.Topic{}

res, _, err := apiClient.DefaultApi.GetTopics(context.Background()).Execute()
if err != nil {
panic(err)
Expand Down
9 changes: 4 additions & 5 deletions internal/api_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package internal

import (
"net/http"
"net/url"
)

// APIConfig defines the available configuration options
Expand All @@ -11,7 +10,7 @@ type APIConfig struct {
// HTTPClient is a custom HTTP client
HTTPClient *http.Client
// Debug enables debug-level logging
Debug bool
// ServerURL sets a custom API server base URL
ServerURL *url.URL
}
Debug bool
// BaseURL sets a custom API server base URL
BaseURL string
}
3 changes: 1 addition & 2 deletions kafkainstance/apiv1internal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ kafkas, resp, err := client.DefaultApi.GetTopics(context.Background()).Execute()
You can override the default configuration options:

```go
baseURL, _ := url.Parse("http://localhost:8001")
cfg := kafkainstance.Config{
HTTPClient: yourHTTPClient,
Debug: true,
ServerURL: baseURL,
BaseURL: "http://localhost:8001/rest",
}

client := kafkainstance.NewAPIClient(&cfg)
Expand Down
9 changes: 6 additions & 3 deletions kafkainstance/apiv1internal/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ func NewAPIClient(cfg *Config) *apiv1.APIClient {
if cfg.HTTPClient != nil {
apiCfg.HTTPClient = cfg.HTTPClient
}
if cfg.ServerURL != nil {
apiCfg.Host = cfg.ServerURL.Host
apiCfg.Scheme = cfg.ServerURL.Scheme
if cfg.BaseURL != "" {
apiCfg.Servers = []apiv1.ServerConfiguration{
{
URL: cfg.BaseURL,
},
}
}

apiCfg.Debug = cfg.Debug
Expand Down
3 changes: 1 addition & 2 deletions kafkamgmt/apiv1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ kafkas, resp, err := client.DefaultApi.GetKafkas(context.Background()).Execute()
You can override the default configuration options:

```go
baseURL, _ := url.Parse("http://localhost:8001")
cfg := kafkamgmt.Config{
HTTPClient: yourHTTPClient,
Debug: true,
ServerURL: baseURL,
BaseURL: "http://localhost:8080",
}

client := kafkamgmt.NewAPIClient(&cfg)
Expand Down
11 changes: 7 additions & 4 deletions kafkamgmt/apiv1/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ func NewAPIClient(cfg *Config) *apiv1.APIClient {
if cfg.HTTPClient != nil {
apiCfg.HTTPClient = cfg.HTTPClient
}
if cfg.ServerURL != nil {
apiCfg.Host = cfg.ServerURL.Host
apiCfg.Scheme = cfg.ServerURL.Scheme
if cfg.BaseURL != "" {
apiCfg.Servers = []apiv1.ServerConfiguration{
{
URL: cfg.BaseURL,
},
}
}

apiCfg.Debug = cfg.Debug

client := apiv1.NewAPIClient(apiCfg)

return client
}
}
3 changes: 1 addition & 2 deletions serviceregistrymgmt/apiv1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ registries, resp, err := client.DefaultApi.GetRegistries(context.Background()).E
You can override the default configuration options:

```go
baseURL, _ := url.Parse("http://localhost:8001")
cfg := serviceregistrymgmt.Config{
HTTPClient: yourHTTPClient,
Debug: true,
ServerURL: baseURL,
BaseURL: "http://localhost:8080",
}

client := serviceregistrymgmt.NewAPIClient(&cfg)
Expand Down
11 changes: 7 additions & 4 deletions serviceregistrymgmt/apiv1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ func NewAPIClient(cfg *Config) *apiClient.APIClient {
if cfg.HTTPClient != nil {
apiCfg.HTTPClient = cfg.HTTPClient
}
if cfg.ServerURL != nil {
apiCfg.Host = cfg.ServerURL.Host
apiCfg.Scheme = cfg.ServerURL.Scheme
if cfg.BaseURL != "" {
apiCfg.Servers = []apiClient.ServerConfiguration{
{
URL: cfg.BaseURL,
},
}
}

apiCfg.Debug = cfg.Debug

client := apiClient.NewAPIClient(apiCfg)

return client
}
}

0 comments on commit 478ac73

Please sign in to comment.