Skip to content

Commit

Permalink
Move URL Parsing inside Client Creation
Browse files Browse the repository at this point in the history
  • Loading branch information
speatzle committed Aug 30, 2021
1 parent 809ebae commit ac4788b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
20 changes: 4 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ package main
import (
"context"
"fmt"
"net/url"

"github.com/speatzle/go-passbolt/api"
)

const passboltAddress = "https://passbolt.example.com"
const address = "https://passbolt.example.com"
const userPassword = "aStrongPassword"
const userPrivateKey = `
-----BEGIN PGP PRIVATE KEY BLOCK-----
Expand All @@ -40,12 +39,7 @@ klasd...
-----END PGP PRIVATE KEY BLOCK-----`

func main() {
u, err := url.Parse(passboltAddress)
if err != nil {
panic(err)
}

client, err := api.NewClient(u, nil, "", userPrivateKey, userPassword)
client, err := api.NewClient(nil, "", address, userPrivateKey, userPassword)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -250,13 +244,12 @@ package main
import (
"context"
"fmt"
"net/url"

"github.com/speatzle/go-passbolt/api"
"github.com/speatzle/go-passbolt/helper"
)

const passboltAddress = "https://passbolt.example.com"
const address = "https://passbolt.example.com"
const userPassword = "aStrongPassword"
const userPrivateKey = `
-----BEGIN PGP PRIVATE KEY BLOCK-----
Expand All @@ -268,12 +261,7 @@ klasd...
func main() {
ctx := context.TODO()

u, err := url.Parse(passboltAddress)
if err != nil {
panic(err)
}

client, err := api.NewClient(u, nil, "", userPrivateKey, userPassword)
client, err := api.NewClient(nil, "", address, userPrivateKey, userPassword)
if err != nil {
panic(err)
}
Expand Down
9 changes: 7 additions & 2 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,19 @@ type Client struct {
// NewClient Returns a new Passbolt Client.
// if httpClient is nil http.DefaultClient will be used.
// if UserAgent is "" "goPassboltClient/1.0" will be used.
func NewClient(BaseURL *url.URL, httpClient *http.Client, UserAgent, UserPrivateKey, UserPassword string) (*Client, error) {
func NewClient(httpClient *http.Client, UserAgent, BaseURL, UserPrivateKey, UserPassword string) (*Client, error) {
if httpClient == nil {
httpClient = http.DefaultClient
}
if UserAgent == "" {
UserAgent = "goPassboltClient/1.0"
}

u, err := url.Parse(BaseURL)
if err != nil {
return nil, fmt.Errorf("Parsing Base URL: %w", err)
}

// Verify that the Given Privatekey and Password are valid and work Together
privateKeyObj, err := crypto.NewKeyFromArmored(UserPrivateKey)
if err != nil {
Expand All @@ -64,7 +69,7 @@ func NewClient(BaseURL *url.URL, httpClient *http.Client, UserAgent, UserPrivate
// Create Client Object
c := &Client{
httpClient: httpClient,
baseURL: BaseURL,
baseURL: u,
userAgent: UserAgent,
userPassword: []byte(UserPassword),
userPrivateKey: UserPrivateKey,
Expand Down

0 comments on commit ac4788b

Please sign in to comment.