Skip to content

Commit

Permalink
refactored ng client
Browse files Browse the repository at this point in the history
  • Loading branch information
micahlmartin committed Mar 10, 2022
1 parent 302126f commit 5c84be7
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 34 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.0
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b
golang.org/x/oauth2 v0.0.0-20220309155454-6242fa91716a
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)

Expand All @@ -31,9 +31,9 @@ require (
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
golang.org/x/sys v0.0.0-20220307203707-22a9840ba4d7 // indirect
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220308174144-ae0e22291548 // indirect
google.golang.org/grpc v1.44.0 // indirect
google.golang.org/grpc v1.45.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,8 @@ golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c/go.mod h1:KelEdhl1UZF7XfJ
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A=
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b h1:clP8eMhB30EHdc0bd2Twtq6kgU7yl5ub2cQLSdrv1Dg=
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b/go.mod h1:DAh4E804XQdzx2j+YRIaUnCqCV2RuMz24cGBJ5QYIrc=
golang.org/x/oauth2 v0.0.0-20220309155454-6242fa91716a h1:qfl7ob3DIEs3Ml9oLuPwY2N04gymzAW04WsUQHIClgM=
golang.org/x/oauth2 v0.0.0-20220309155454-6242fa91716a/go.mod h1:DAh4E804XQdzx2j+YRIaUnCqCV2RuMz24cGBJ5QYIrc=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down Expand Up @@ -1114,6 +1116,8 @@ golang.org/x/sys v0.0.0-20211116061358-0a5406a5449c/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220307203707-22a9840ba4d7 h1:8IVLkfbr2cLhv0a/vKq4UFUcJym8RmDoDboxCFWEjYE=
golang.org/x/sys v0.0.0-20220307203707-22a9840ba4d7/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5 h1:y/woIyUBFbpQGKS0u1aHF/40WUDnek3fPOyD08H5Vng=
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down Expand Up @@ -1319,6 +1323,8 @@ google.golang.org/grpc v1.42.0/go.mod h1:k+4IHHFw41K8+bbowsex27ge2rCb65oeWqe4jJ5
google.golang.org/grpc v1.43.0/go.mod h1:k+4IHHFw41K8+bbowsex27ge2rCb65oeWqe4jJ590SU=
google.golang.org/grpc v1.44.0 h1:weqSxi/TMs1SqFRMHCtBgXRs8k3X39QIDEZ0pRcttUg=
google.golang.org/grpc v1.44.0/go.mod h1:k+4IHHFw41K8+bbowsex27ge2rCb65oeWqe4jJ590SU=
google.golang.org/grpc v1.45.0 h1:NEpgUqV3Z+ZjkqMsxMg11IaDrXY4RY6CQukSGK0uI1M=
google.golang.org/grpc v1.45.0/go.mod h1:lN7owxKUQEqMfSyQikvvk5tf/6zMPsrK+ONuO11+0rQ=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
Expand Down
18 changes: 7 additions & 11 deletions harness/cd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ func DefaultConfig() *Config {
}

cfg := &Config{
AccountId: helpers.EnvVars.AccountId.Get(),
APIKey: helpers.EnvVars.ApiKey.Get(),
Endpoint: helpers.EnvVars.Endpoint.GetWithDefault(utils.BaseUrl),
Logger: logger,
HTTPClient: utils.GetDefaultHttpClient(logger),
UserAgent: fmt.Sprintf("%s-%s", harness.SDKName, harness.SDKVersion),
AccountId: helpers.EnvVars.AccountId.Get(),
APIKey: helpers.EnvVars.ApiKey.Get(),
DefaultHeaders: make(map[string]string),
Endpoint: helpers.EnvVars.Endpoint.GetWithDefault(utils.BaseUrl),
HTTPClient: utils.GetDefaultHttpClient(logger),
Logger: logger,
UserAgent: fmt.Sprintf("%s-%s", harness.SDKName, harness.SDKVersion),
}

return cfg
Expand All @@ -84,11 +85,6 @@ func NewClient(cfg *Config) (*ApiClient, error) {
return nil, cfg.NewInvalidConfigError("Endpoint", nil)
}

// defaultHeaders
if cfg.DefaultHeaders == nil {
cfg.DefaultHeaders = make(map[string]string)
}

// Set default headers for all requests
cfg.DefaultHeaders[helpers.HTTPHeaders.UserAgent.String()] = utils.CoalesceStr(cfg.UserAgent, fmt.Sprintf("%s-%s", harness.SDKName, harness.SDKVersion))
cfg.DefaultHeaders[helpers.HTTPHeaders.Accept.String()] = helpers.HTTPHeaders.ApplicationJson.String()
Expand Down
18 changes: 4 additions & 14 deletions harness/nextgen/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ import (
"time"
"unicode/utf8"

"github.com/harness/harness-go-sdk/harness/utils"
"github.com/harness/harness-go-sdk/logging"
"github.com/hashicorp/go-retryablehttp"
log "github.com/sirupsen/logrus"
"golang.org/x/oauth2"
)

Expand All @@ -48,6 +45,7 @@ type APIClient struct {
common service // Reuse a single struct instead of allocating one for each service on the heap.

AccountId string
ApiKey string
Endpoint string

// API Services
Expand Down Expand Up @@ -172,20 +170,12 @@ type service struct {
// NewAPIClient creates a new API client. Requires a userAgent string describing your application.
// optionally a custom http.Client to allow for advanced features such as caching.
func NewAPIClient(cfg *Configuration) *APIClient {
if cfg.HTTPClient == nil {
cfg.HTTPClient = utils.GetDefaultHttpClient(cfg.Logger)
}

if cfg.Logger == nil {
cfg.Logger = logging.NewLogger()
if cfg.DebugLogging {
cfg.Logger.SetLevel(log.DebugLevel)
}
}

c := &APIClient{}
c.cfg = cfg
c.common.client = c

// Api Config
c.ApiKey = cfg.ApiKey
c.AccountId = cfg.AccountId
c.Endpoint = cfg.BasePath

Expand Down
23 changes: 20 additions & 3 deletions harness/nextgen/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
package nextgen

import (
"fmt"

"github.com/harness/harness-go-sdk/harness"
"github.com/harness/harness-go-sdk/harness/helpers"
"github.com/harness/harness-go-sdk/harness/utils"
"github.com/harness/harness-go-sdk/logging"
"github.com/hashicorp/go-retryablehttp"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -52,6 +58,7 @@ type APIKey struct {

type Configuration struct {
AccountId string `json:"accountId,omitempty"`
ApiKey string `json:"apiKey,omitempty"`
BasePath string `json:"basePath,omitempty"`
Host string `json:"host,omitempty"`
Scheme string `json:"scheme,omitempty"`
Expand All @@ -62,12 +69,22 @@ type Configuration struct {
DebugLogging bool
}

func NewConfiguration() *Configuration {
func DefaultConfiguration() *Configuration {
logger := logging.NewLogger()
if helpers.EnvVars.DebugEnabled.Get() == "true" {
logger.SetLevel(log.DebugLevel)
}

cfg := &Configuration{
BasePath: "https://app.harness.io/gateway",
AccountId: helpers.EnvVars.AccountId.Get(),
ApiKey: helpers.EnvVars.ApiKey.Get(),
BasePath: helpers.EnvVars.Endpoint.GetWithDefault(utils.BaseUrl),
DefaultHeader: make(map[string]string),
UserAgent: "Swagger-Codegen/1.0.0/go",
HTTPClient: utils.GetDefaultHttpClient(logger),
Logger: logger,
UserAgent: fmt.Sprintf("%s-%s", harness.SDKName, harness.SDKVersion),
}

return cfg
}

Expand Down
4 changes: 2 additions & 2 deletions tools/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,12 @@ require (
github.com/urfave/cli/v2 v2.3.0 // indirect
github.com/uudashr/gocognit v1.0.5 // indirect
github.com/yagipy/maintidx v1.0.0 // indirect
github.com/yeya24/promlinter v0.1.1-0.20210918184747-d757024714a1 // indirect
github.com/yeya24/promlinter v0.2.0 // indirect
gitlab.com/bosi/decorder v0.2.1 // indirect
golang.org/x/crypto v0.0.0-20220307211146-efcb8507fb70 // indirect
golang.org/x/mod v0.5.1 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20220307203707-22a9840ba4d7 // indirect
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.9 // indirect
Expand Down
4 changes: 4 additions & 0 deletions tools/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,8 @@ github.com/yagipy/maintidx v1.0.0 h1:h5NvIsCz+nRDapQ0exNv4aJ0yXSI0420omVANTv3GJM
github.com/yagipy/maintidx v1.0.0/go.mod h1:0qNf/I/CCZXSMhsRsrEPDZ+DkekpKLXAJfsTACwgXLk=
github.com/yeya24/promlinter v0.1.1-0.20210918184747-d757024714a1 h1:YAaOqqMTstELMMGblt6yJ/fcOt4owSYuw3IttMnKfAM=
github.com/yeya24/promlinter v0.1.1-0.20210918184747-d757024714a1/go.mod h1:rs5vtZzeBHqqMwXqFScncpCF6u06lezhZepno9AB1Oc=
github.com/yeya24/promlinter v0.2.0 h1:xFKDQ82orCU5jQujdaD8stOHiv8UN68BSdn2a8u8Y3o=
github.com/yeya24/promlinter v0.2.0/go.mod h1:u54lkmBOZrpEbQQ6gox2zWKKLKu2SGe+2KOiextY+IA=
github.com/yudai/gojsondiff v1.0.0/go.mod h1:AY32+k2cwILAkW1fbgxQ5mUmMiZFgLIV+FBNExI05xg=
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82/go.mod h1:lgjkn3NuSvDfVJdfcVVdX+jpBxNmX4rDAzaS45IcYoM=
github.com/yudai/pp v2.0.1+incompatible/go.mod h1:PuxR/8QJ7cyCkFp/aUDS+JY727OFEZkTdatxwunjIkc=
Expand Down Expand Up @@ -1196,6 +1198,8 @@ golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220307203707-22a9840ba4d7 h1:8IVLkfbr2cLhv0a/vKq4UFUcJym8RmDoDboxCFWEjYE=
golang.org/x/sys v0.0.0-20220307203707-22a9840ba4d7/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5 h1:y/woIyUBFbpQGKS0u1aHF/40WUDnek3fPOyD08H5Vng=
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
2 changes: 1 addition & 1 deletion tools/tools.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build tools
//go:build tools

package tools

Expand Down

0 comments on commit 5c84be7

Please sign in to comment.