Skip to content

Commit

Permalink
refactor: separate api config to pkg/
Browse files Browse the repository at this point in the history
  • Loading branch information
hazcod committed Sep 1, 2022
1 parent f3d5ee8 commit f4495ff
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 31 deletions.
5 changes: 3 additions & 2 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/intigriti/sdk-go/cmd/cli/company"
"github.com/intigriti/sdk-go/cmd/config"
intigriti "github.com/intigriti/sdk-go/pkg/api"
apiConfig "github.com/intigriti/sdk-go/pkg/config"
"github.com/sirupsen/logrus"
"strings"
)
Expand Down Expand Up @@ -33,13 +34,13 @@ func main() {
logger.WithError(err).Fatal("invalid configuration")
}

inti, err := intigriti.New(intigriti.Config{
inti, err := intigriti.New(apiConfig.Config{
Credentials: struct {
ClientID string
ClientSecret string
}{ClientID: cfg.Auth.ClientID, ClientSecret: cfg.Auth.ClientSecret},
OpenBrowser: true,
TokenCache: &intigriti.CachedToken{
TokenCache: &apiConfig.CachedToken{
RefreshToken: cfg.Cache.RefreshToken,
AccessToken: cfg.Cache.AccessToken,
ExpiryDate: cfg.Cache.ExpiryDate,
Expand Down
3 changes: 2 additions & 1 deletion pkg/api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"context"
"fmt"
"github.com/intigriti/sdk-go/pkg/config"
"github.com/intigriti/sdk-go/pkg/ui"
"net/http"
"os"
Expand Down Expand Up @@ -93,7 +94,7 @@ func (e *Endpoint) getToken() (*oauth2.Token, error) {
}

// return the http client which automatically injects the right authentication credentials
func (e *Endpoint) getClient(tc *CachedToken, openBrowser bool) (*http.Client, error) {
func (e *Endpoint) getClient(tc *config.CachedToken, openBrowser bool) (*http.Client, error) {
ctx := context.Background()

conf := e.getOauth2Config()
Expand Down
31 changes: 3 additions & 28 deletions pkg/api/endpoint.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package api

import (
"net/http"
"time"

"github.com/intigriti/sdk-go/pkg/config"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/oauth2"
"net/http"
)

type Endpoint struct {
Expand All @@ -20,33 +19,9 @@ type Endpoint struct {
oauthToken *oauth2.Token
}

type CachedToken struct {
RefreshToken string
AccessToken string
ExpiryDate time.Time
Type string
}

type Config struct {
// required authentication credentials
Credentials struct {
ClientID string
ClientSecret string
}

// optional open a browser to complete authentication if user interaction is required
OpenBrowser bool

// optional token cache if caching previous credentials
TokenCache *CachedToken

// optional logger instance
Logger *logrus.Logger
}

// New creates an Intigriti endpoint object to use
// this is the main object to interact with the SDK
func New(cfg Config) (Endpoint, error) {
func New(cfg config.Config) (Endpoint, error) {
e := Endpoint{
clientID: cfg.Credentials.ClientID,
clientSecret: cfg.Credentials.ClientSecret,
Expand Down
30 changes: 30 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package config

import (
"github.com/sirupsen/logrus"
"time"
)

type CachedToken struct {
RefreshToken string
AccessToken string
ExpiryDate time.Time
Type string
}

type Config struct {
// required authentication credentials
Credentials struct {
ClientID string
ClientSecret string
}

// optional open a browser to complete authentication if user interaction is required
OpenBrowser bool

// optional token cache if caching previous credentials
TokenCache *CachedToken

// optional logger instance
Logger *logrus.Logger
}

0 comments on commit f4495ff

Please sign in to comment.