Skip to content

Commit

Permalink
chore!: added different client services to improve usage
Browse files Browse the repository at this point in the history
  • Loading branch information
dion-gionet committed May 24, 2024
1 parent 1059b5d commit 3168fa4
Show file tree
Hide file tree
Showing 8 changed files with 432 additions and 402 deletions.
27 changes: 21 additions & 6 deletions authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@ type Client struct {
client *http.Client
baseUri string
credential credentials

ClientUser User

common service

Entries *Entries
Vaults *Vaults
}

type service struct {
client *Client
}

type credentials struct {
Expand Down Expand Up @@ -102,6 +110,13 @@ func NewClient(username string, password string, baseUri string) (Client, error)

client.ClientUser = user

client.common.client = &client

client.Entries = &Entries{
UserCredential: (*EntryUserCredentialService)(&client.common),
}
client.Vaults = (*Vaults)(&client.common)

return client, nil
}

Expand All @@ -115,7 +130,7 @@ func (c *Client) login() (User, error) {
}
loginJson, err := json.Marshal(loginBody)
if err != nil {
return User{}, fmt.Errorf("failed to marshall login body. error: %w", err)
return User{}, fmt.Errorf("failed to marshal login body. error: %w", err)
}

reqUrl, err := url.JoinPath(c.baseUri, loginEndpoint)
Expand All @@ -131,7 +146,7 @@ func (c *Client) login() (User, error) {
var loginResponse loginResponse
err = json.Unmarshal(resp.Response, &loginResponse)
if err != nil {
return User{}, fmt.Errorf("failed to unmarshall response body. error: %w", err)
return User{}, fmt.Errorf("failed to unmarshal response body. error: %w", err)
}
if loginResponse.Data.Result != ServerLoginSuccess {
return User{}, fmt.Errorf("failed to refresh token (%s) : %s", loginResponse.Data.Result, loginResponse.Data.Message)
Expand All @@ -140,7 +155,7 @@ func (c *Client) login() (User, error) {
var user User
err = json.Unmarshal(resp.Response, &user)
if err != nil {
return User{}, fmt.Errorf("failed to unmarshall user body. error: %w", err)
return User{}, fmt.Errorf("failed to unmarshal user body. error: %w", err)
}

c.credential.token = loginResponse.Data.TokenId
Expand All @@ -158,7 +173,7 @@ func (c *Client) refreshToken() error {
}
loginJson, err := json.Marshal(loginBody)
if err != nil {
return fmt.Errorf("failed to marshall login body. error: %w", err)
return fmt.Errorf("failed to marshal login body. error: %w", err)
}

reqUrl, err := url.JoinPath(c.baseUri, loginEndpoint)
Expand All @@ -174,7 +189,7 @@ func (c *Client) refreshToken() error {
var loginResponse loginResponse
err = json.Unmarshal(resp.Response, &loginResponse)
if err != nil {
return fmt.Errorf("failed to unmarshall response body. error: %w", err)
return fmt.Errorf("failed to unmarshal response body. error: %w", err)
}
if loginResponse.Data.Result != ServerLoginSuccess {
return fmt.Errorf("failed to refresh token (%s) : %s", loginResponse.Data.Result, loginResponse.Data.Message)
Expand Down
2 changes: 1 addition & 1 deletion dvls.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (c *Client) rawRequest(url string, reqMethod string, reqBody io.Reader) (Re

err = json.Unmarshal(response.Response, &response)
if err != nil {
return response, &RequestError{Err: fmt.Errorf("failed to unmarshall response body. error: %w", err), Url: url}
return response, &RequestError{Err: fmt.Errorf("failed to unmarshal response body. error: %w", err), Url: url}
}

return response, nil
Expand Down
Loading

0 comments on commit 3168fa4

Please sign in to comment.