Skip to content

Commit

Permalink
Using the struct Environment instead of old fields on options
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Aug 2, 2021
1 parent bb00def commit ff18867
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 67 deletions.
33 changes: 15 additions & 18 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ type Client struct {
// clientOptions holds all the configuration for client requests and default resources
type clientOptions struct {
apiKey string // API key
apiURL string // API endpoint (URL by environment)
environment string // Name of the current environment
env Environment // Environment
customHeaders map[string][]string // Custom headers on outgoing requests
httpTimeout time.Duration // Default timeout in seconds for GET requests
requestTracing bool // If enabled, it will trace the request timing
Expand All @@ -42,24 +41,23 @@ type StandardResponse struct {
// that overwrite default client options.
type ClientOps func(c *clientOptions)

// WithEnvironment will change the environment
func WithEnvironment(e environment) ClientOps {
// WithEnvironment will change the Environment
func WithEnvironment(e Environment) ClientOps {
return func(c *clientOptions) {
c.apiURL = e.apiURL
c.environment = e.name
c.env = e
}
}

// WithCustomEnvironment will set a custom environment
// WithCustomEnvironment will set a custom Environment
func WithCustomEnvironment(name, alias, apiURL string) ClientOps {
return WithEnvironment(environment{
return WithEnvironment(Environment{
alias: alias,
apiURL: apiURL,
name: name,
})
}

// WithEnvironmentString will change the environment
// WithEnvironmentString will change the Environment
func WithEnvironmentString(e string) ClientOps {
e = strings.ToLower(strings.TrimSpace(e))
if e == environmentStagingName || e == environmentStagingAlias {
Expand Down Expand Up @@ -129,9 +127,9 @@ func (c *Client) GetUserAgent() string {
return c.options.userAgent
}

// GetEnvironment will return the environment of the client
func (c *Client) GetEnvironment() string {
return c.options.environment
// GetEnvironment will return the Environment of the client
func (c *Client) GetEnvironment() Environment {
return c.options.env
}

// defaultClientOptions will return a clientOptions struct with the default settings
Expand All @@ -140,8 +138,7 @@ func (c *Client) GetEnvironment() string {
func defaultClientOptions() (opts *clientOptions) {
// Set the default options
opts = &clientOptions{
apiURL: EnvironmentLive.apiURL,
environment: EnvironmentLive.name,
env: EnvironmentLive,
httpTimeout: defaultHTTPTimeout,
requestTracing: false,
retryCount: defaultRetryCount,
Expand Down Expand Up @@ -217,13 +214,13 @@ func (c *Client) Request(httpMethod string, requestEndpoint string,
var resp *resty.Response
switch httpMethod {
case http.MethodPost:
resp, err = req.Post(c.options.apiURL + requestEndpoint)
resp, err = req.Post(c.options.env.URL() + requestEndpoint)
case http.MethodPut:
resp, err = req.Put(c.options.apiURL + requestEndpoint)
resp, err = req.Put(c.options.env.URL() + requestEndpoint)
case http.MethodDelete:
resp, err = req.Delete(c.options.apiURL + requestEndpoint)
resp, err = req.Delete(c.options.env.URL() + requestEndpoint)
case http.MethodGet:
resp, err = req.Get(c.options.apiURL + requestEndpoint)
resp, err = req.Get(c.options.env.URL() + requestEndpoint)
}
if err != nil {
return
Expand Down
66 changes: 32 additions & 34 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func TestNewClient(t *testing.T) {
assert.Equal(t, defaultRetryCount, client.options.retryCount)
assert.Equal(t, defaultUserAgent, client.options.userAgent)
assert.Equal(t, false, client.options.requestTracing)
assert.Equal(t, EnvironmentLive.apiURL, client.options.apiURL)
assert.Equal(t, EnvironmentLive.name, client.options.environment)
assert.Equal(t, EnvironmentLive.apiURL, client.options.env.URL())
assert.Equal(t, EnvironmentLive.name, client.options.env.Name())
})

t.Run("missing api key", func(t *testing.T) {
Expand Down Expand Up @@ -102,39 +102,51 @@ func TestNewClient(t *testing.T) {
assert.Equal(t, "custom user agent", client.GetUserAgent())
})

t.Run("custom environment (staging)", func(t *testing.T) {
t.Run("custom Environment (live)", func(t *testing.T) {
client, err := NewClient(WithAPIKey(testAPIKey), WithEnvironment(EnvironmentLive))
assert.NotNil(t, client)
assert.NoError(t, err)
assert.Equal(t, liveAPIURL, client.options.env.URL())
assert.Equal(t, environmentLiveName, client.options.env.Name())
assert.Equal(t, environmentLiveAlias, client.options.env.Alias())
})

t.Run("custom Environment (staging)", func(t *testing.T) {
client, err := NewClient(WithAPIKey(testAPIKey), WithEnvironment(EnvironmentStaging))
assert.NotNil(t, client)
assert.NoError(t, err)
assert.Equal(t, client.options.apiURL, EnvironmentStaging.apiURL)
assert.Equal(t, client.options.environment, environmentStagingName)
assert.Equal(t, stagingAPIURL, client.options.env.URL())
assert.Equal(t, environmentStagingName, client.options.env.Name())
assert.Equal(t, environmentStagingAlias, client.options.env.Alias())
})

t.Run("custom environment (development)", func(t *testing.T) {
t.Run("custom Environment (development)", func(t *testing.T) {
client, err := NewClient(WithAPIKey(testAPIKey), WithEnvironment(EnvironmentDevelopment))
assert.NotNil(t, client)
assert.NoError(t, err)
assert.Equal(t, client.options.apiURL, EnvironmentDevelopment.apiURL)
assert.Equal(t, client.options.environment, environmentDevelopmentName)
assert.Equal(t, developmentURL, client.options.env.URL())
assert.Equal(t, environmentDevelopmentName, client.options.env.Name())
assert.Equal(t, environmentDevelopmentAlias, client.options.env.Alias())
})

t.Run("custom environment (custom)", func(t *testing.T) {
t.Run("custom Environment (custom)", func(t *testing.T) {
client, err := NewClient(
WithAPIKey(testAPIKey),
WithCustomEnvironment("custom", "alias", "http://localhost:5000"),
)
assert.NotNil(t, client)
assert.NoError(t, err)
assert.Equal(t, client.options.apiURL, "http://localhost:5000")
assert.Equal(t, client.options.environment, "custom")
assert.Equal(t, "http://localhost:5000", client.options.env.URL())
assert.Equal(t, "custom", client.options.env.Name())
assert.Equal(t, "alias", client.options.env.Alias())
})

t.Run("default no environment", func(t *testing.T) {
t.Run("default no Environment", func(t *testing.T) {
client, err := NewClient(WithAPIKey(testAPIKey), WithEnvironmentString(""))
assert.NotNil(t, client)
assert.NoError(t, err)
assert.Equal(t, client.options.apiURL, EnvironmentLive.apiURL)
assert.Equal(t, client.options.environment, environmentLiveName)
assert.Equal(t, liveAPIURL, client.options.env.URL())
assert.Equal(t, environmentLiveName, client.options.env.Name())
})
}

Expand All @@ -151,32 +163,17 @@ func TestClient_GetUserAgent(t *testing.T) {
})
}

// ExampleVersion example using Version()
//
// See more examples in /examples/
func ExampleVersion() {
fmt.Printf("version: %s", Version())
// Output:version: v0.6.6
}

// ExampleUserAgent example using UserAgent()
//
// See more examples in /examples/
func ExampleUserAgent() {
fmt.Printf("user agent: %s", UserAgent())
// Output:user agent: go-tonicpow: v0.6.6
}

// TestClient_GetEnvironment will test the method GetEnvironment()
func TestClient_GetEnvironment(t *testing.T) {
t.Parallel()

t.Run("get client environment", func(t *testing.T) {
t.Run("get client Environment", func(t *testing.T) {
client, err := NewClient(WithAPIKey(testAPIKey))
assert.NoError(t, err)
assert.NotNil(t, client)
env := client.GetEnvironment()
assert.Equal(t, environmentLiveName, env)
assert.Equal(t, environmentLiveName, env.Name())
assert.Equal(t, environmentLiveAlias, env.Alias())
})
}

Expand Down Expand Up @@ -210,9 +207,10 @@ func TestDefaultClientOptions(t *testing.T) {
assert.Equal(t, defaultHTTPTimeout, options.httpTimeout)
assert.Equal(t, defaultRetryCount, options.retryCount)
assert.Equal(t, defaultUserAgent, options.userAgent)
assert.Equal(t, EnvironmentLive.apiURL, options.apiURL)
assert.Equal(t, EnvironmentLive.name, options.environment)
assert.Equal(t, environmentLiveAlias, options.env.Alias())
assert.Equal(t, environmentLiveName, options.env.Name())
assert.Equal(t, false, options.requestTracing)
assert.Equal(t, liveAPIURL, options.env.URL())
}

// BenchmarkDefaultClientOptions benchmarks the method defaultClientOptions()
Expand Down
22 changes: 11 additions & 11 deletions definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,41 +115,41 @@ var (
// FeedType is used for the campaign feeds (rss, atom, json)
type FeedType string

// environment is used for changing the environment for running client requests
type environment struct {
// Environment is used for changing the Environment for running client requests
type Environment struct {
alias string
apiURL string
name string
}

// Alias will return the environment's alias
func (e environment) Alias() string {
// Alias will return the Environment's alias
func (e Environment) Alias() string {
return e.alias
}

// Name will return the environment's name
func (e environment) Name() string {
// Name will return the Environment's name
func (e Environment) Name() string {
return e.name
}

// URL will return the environment's url
func (e environment) URL() string {
// URL will return the Environment's url
func (e Environment) URL() string {
return e.apiURL
}

// Current environments available
var (
EnvironmentLive = environment{
EnvironmentLive = Environment{
apiURL: liveAPIURL,
name: environmentLiveName,
alias: environmentLiveAlias,
}
EnvironmentStaging = environment{
EnvironmentStaging = Environment{
apiURL: stagingAPIURL,
name: environmentStagingName,
alias: environmentStagingAlias,
}
EnvironmentDevelopment = environment{
EnvironmentDevelopment = Environment{
apiURL: developmentURL,
name: environmentDevelopmentName,
alias: environmentDevelopmentAlias,
Expand Down
21 changes: 20 additions & 1 deletion examples/new_client/new_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,32 @@ func main() {
client, err := tonicpow.NewClient(
tonicpow.WithAPIKey(os.Getenv("TONICPOW_API_KEY")),
tonicpow.WithEnvironmentString(os.Getenv("TONICPOW_ENVIRONMENT")),

// Custom options for loading the TonicPow client
// tonicpow.WithCustomEnvironment("customEnv", "customAlias", "https://localhost:3002"),
// tonicpow.WithEnvironment(tonicpow.EnvironmentStaging),
// tonicpow.WithHTTPTimeout(10*time.Second),
// tonicpow.WithRequestTracing(),
// tonicpow.WithRetryCount(3),
// tonicpow.WithUserAgent("my custom user agent v9.0.9"),

/*
// Example adding custom headers to each request
headers := make(map[string][]string)
headers["custom_header_1"] = append(headers["custom_header_1"], "value_1")
headers["custom_header_2"] = append(headers["custom_header_2"], "value_2")
tonicpow.WithCustomHeaders(headers),
*/
)
if err != nil {
log.Fatalf("error in NewClient: %s", err.Error())
}

// Use your own custom Resty
// client.WithCustomHTTPClient(resty.New())

log.Println(
"client: ", client.GetUserAgent(),
"environment: ", client.GetEnvironment(),
"environment: ", client.GetEnvironment().Name(),
)
}
50 changes: 47 additions & 3 deletions tonicpow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tonicpow

import (
"encoding/json"
"fmt"
"net/http"
"testing"

Expand Down Expand Up @@ -34,6 +35,14 @@ func TestVersion(t *testing.T) {
})
}

// ExampleVersion example using Version()
//
// See more examples in /examples/
func ExampleVersion() {
fmt.Printf("version: %s", Version())
// Output:version: v0.6.6
}

// TestUserAgent will test the method UserAgent()
func TestUserAgent(t *testing.T) {
t.Parallel()
Expand All @@ -44,6 +53,14 @@ func TestUserAgent(t *testing.T) {
})
}

// ExampleUserAgent example using UserAgent()
//
// See more examples in /examples/
func ExampleUserAgent() {
fmt.Printf("user agent: %s", UserAgent())
// Output:user agent: go-tonicpow: v0.6.6
}

// TestGetFeedType will test the method GetFeedType()
func TestGetFeedType(t *testing.T) {
t.Run("test valid cases", func(t *testing.T) {
Expand All @@ -59,28 +76,55 @@ func TestEnvironment_Alias(t *testing.T) {
assert.Equal(t, environmentStagingAlias, EnvironmentStaging.Alias())
assert.Equal(t, environmentDevelopmentAlias, EnvironmentDevelopment.Alias())
assert.Equal(t, environmentLiveAlias, EnvironmentLive.Alias())
e := environment{}
e := Environment{}
assert.Equal(t, "", e.Alias())
}

// ExampleEnvironment_Alias example using Alias()
//
// See more examples in /examples/
func ExampleEnvironment_Alias() {
env := EnvironmentLive
fmt.Printf("name: %s alias: %s", env.Name(), env.Alias())
// Output:name: live alias: production
}

// TestEnvironment_Name will test the method Name()
func TestEnvironment_Name(t *testing.T) {
assert.Equal(t, environmentStagingName, EnvironmentStaging.Name())
assert.Equal(t, environmentDevelopmentName, EnvironmentDevelopment.Name())
assert.Equal(t, environmentLiveName, EnvironmentLive.Name())
e := environment{}
e := Environment{}
assert.Equal(t, "", e.Name())
}

// ExampleEnvironment_Name example using Name()
//
// See more examples in /examples/
func ExampleEnvironment_Name() {
env := EnvironmentLive
fmt.Printf("name: %s alias: %s", env.Name(), env.Alias())
// Output:name: live alias: production
}

// TestEnvironment_URL will test the method URL()
func TestEnvironment_URL(t *testing.T) {
assert.Equal(t, stagingAPIURL, EnvironmentStaging.URL())
assert.Equal(t, developmentURL, EnvironmentDevelopment.URL())
assert.Equal(t, liveAPIURL, EnvironmentLive.URL())
e := environment{}
e := Environment{}
assert.Equal(t, "", e.URL())
}

// ExampleEnvironment_URL example using URL()
//
// See more examples in /examples/
func ExampleEnvironment_URL() {
env := EnvironmentLive
fmt.Printf("name: %s url: %s", env.Name(), env.URL())
// Output:name: live url: https://api.tonicpow.com/v1
}

// mockResponseData is used for mocking the response
func mockResponseData(method, endpoint string, statusCode int, model interface{}) error {
httpmock.Reset()
Expand Down

0 comments on commit ff18867

Please sign in to comment.