From ac4788b357bee6a08c0029d22d798a4f830db856 Mon Sep 17 00:00:00 2001 From: Samuel Lorch Date: Mon, 30 Aug 2021 22:14:26 +0200 Subject: [PATCH] Move URL Parsing inside Client Creation --- README.md | 20 ++++---------------- api/client.go | 9 +++++++-- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index af879c9..35635e8 100644 --- a/README.md +++ b/README.md @@ -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----- @@ -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) } @@ -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----- @@ -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) } diff --git a/api/client.go b/api/client.go index e63d562..5a6919d 100644 --- a/api/client.go +++ b/api/client.go @@ -36,7 +36,7 @@ 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 } @@ -44,6 +44,11 @@ func NewClient(BaseURL *url.URL, httpClient *http.Client, UserAgent, UserPrivate 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 { @@ -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,